nautilus_model/identifiers/macros.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3// https://nautechsystems.io
4//
5// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6// You may not use this file except in compliance with the License.
7// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Provides macros for generating identifier functionality.
17
18macro_rules! impl_serialization_for_identifier {
19 ($ty:ty) => {
20 impl Serialize for $ty {
21 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
22 where
23 S: Serializer,
24 {
25 self.inner().serialize(serializer)
26 }
27 }
28
29 impl<'de> Deserialize<'de> for $ty {
30 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
31 where
32 D: Deserializer<'de>,
33 {
34 let value_str: &str = Deserialize::deserialize(deserializer)?;
35 let value: $ty = value_str.into();
36 Ok(value)
37 }
38 }
39 };
40}
41
42macro_rules! impl_from_str_for_identifier {
43 ($ty:ty) => {
44 impl From<&str> for $ty {
45 fn from(value: &str) -> Self {
46 Self::new(value)
47 }
48 }
49
50 impl From<String> for $ty {
51 fn from(value: String) -> Self {
52 Self::new(value)
53 }
54 }
55 };
56}
57
58macro_rules! impl_as_ref_for_identifier {
59 ($ty:ty) => {
60 impl AsRef<str> for $ty {
61 fn as_ref(&self) -> &str {
62 self.as_str()
63 }
64 }
65 };
66}