strict_encoding/
ident.rs

1// Strict encoding library for deterministic binary serialization.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Designed in 2019-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
6// Written in 2024-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
7//
8// Copyright (C) 2019-2022 LNP/BP Standards Association.
9// Copyright (C) 2022-2025 Laboratories for Ubiquitous Deterministic Computing (UBIDECO),
10//                         Institute for Distributed and Cognitive Systems (InDCS), Switzerland.
11// Copyright (C) 2019-2025 Dr Maxim Orlovsky.
12// All rights under the above copyrights are reserved.
13//
14// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
15// in compliance with the License. You may obtain a copy of the License at
16//
17//        http://www.apache.org/licenses/LICENSE-2.0
18//
19// Unless required by applicable law or agreed to in writing, software distributed under the License
20// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
21// or implied. See the License for the specific language governing permissions and limitations under
22// the License.
23
24use std::str::FromStr;
25
26use amplify::Wrapper;
27
28use crate::stl::{AlphaCapsLodash, AlphaLodash, AlphaNumLodash, AlphaSmallLodash};
29use crate::{RString, STRICT_TYPES_LIB};
30
31pub const IDENT_MAX_LEN: usize = 100;
32
33#[macro_export]
34macro_rules! impl_ident_type {
35    ($ty:ty) => {
36        impl From<$ty> for String {
37            #[inline]
38            fn from(ident: $ty) -> String { ident.0.into() }
39        }
40
41        impl From<&'static str> for $ty {
42            #[inline]
43            fn from(ident: &'static str) -> Self { Self(RString::from(ident)) }
44        }
45
46        impl TryFrom<String> for $ty {
47            type Error = $crate::InvalidRString;
48
49            #[inline]
50            fn try_from(s: String) -> Result<Self, Self::Error> { Self::from_str(&s) }
51        }
52
53        impl ::core::fmt::Debug for $ty {
54            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
55                f.debug_tuple(&$crate::type_name::<Self>()).field(&self.as_str()).finish()
56            }
57        }
58
59        impl ::core::borrow::Borrow<str> for $ty {
60            #[inline]
61            fn borrow(&self) -> &str { self.as_str() }
62        }
63
64        impl AsRef<str> for $ty {
65            #[inline]
66            fn as_ref(&self) -> &str { self.as_str() }
67        }
68
69        impl $ty {
70            /// Returns string reference.
71            #[inline]
72            pub fn as_str(&self) -> &str { self.0.as_str() }
73        }
74    };
75}
76
77#[macro_export]
78macro_rules! impl_ident_subtype {
79    ($ty:ty) => {
80        impl From<$ty> for $crate::Ident {
81            #[inline]
82            fn from(name: $ty) -> Self {
83                $crate::Ident::from_str(name.as_str()).expect("ident is a superset")
84            }
85        }
86
87        impl $ty {
88            /// Converts to identifier name.
89            #[inline]
90            pub fn to_ident(&self) -> $crate::Ident { self.clone().into() }
91            /// Converts into identifier name.
92            #[inline]
93            pub fn into_ident(self) -> $crate::Ident { self.into() }
94        }
95    };
96}
97
98#[derive(Wrapper, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
99#[wrapper(Deref, Display, FromStr)]
100#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
101pub struct Ident(RString<AlphaLodash, AlphaNumLodash, 1, IDENT_MAX_LEN>);
102
103impl_ident_type!(Ident);
104impl_strict_newtype!(Ident, STRICT_TYPES_LIB);
105
106#[derive(Wrapper, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
107#[wrapper(Deref, Display, FromStr)]
108#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
109pub struct TypeName(RString<AlphaCapsLodash, AlphaNumLodash, 1, IDENT_MAX_LEN>);
110
111impl_ident_type!(TypeName);
112impl_ident_subtype!(TypeName);
113impl_strict_newtype!(TypeName, STRICT_TYPES_LIB);
114
115#[derive(Wrapper, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
116#[wrapper(Deref, Display, FromStr)]
117#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
118pub struct FieldName(RString<AlphaSmallLodash, AlphaNumLodash, 1, IDENT_MAX_LEN>);
119
120impl_ident_type!(FieldName);
121impl_ident_subtype!(FieldName);
122impl_strict_newtype!(FieldName, STRICT_TYPES_LIB);
123
124#[derive(Wrapper, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
125#[wrapper(Deref, Display, FromStr)]
126#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
127pub struct VariantName(RString<AlphaSmallLodash, AlphaNumLodash, 1, IDENT_MAX_LEN>);
128
129impl_ident_type!(VariantName);
130impl_ident_subtype!(VariantName);
131impl_strict_newtype!(VariantName, STRICT_TYPES_LIB);
132
133#[derive(Wrapper, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
134#[wrapper(Deref, Display, FromStr)]
135#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
136pub struct LibName(RString<AlphaCapsLodash, AlphaNumLodash, 1, IDENT_MAX_LEN>);
137
138impl_ident_type!(LibName);
139impl_ident_subtype!(LibName);
140impl_strict_newtype!(LibName, STRICT_TYPES_LIB);