11use std:: hash:: Hash ;
2- use std:: str:: FromStr ;
32use std:: sync:: Arc ;
43
54use serde:: { de, Deserialize , Deserializer , Serialize , Serializer } ;
@@ -19,45 +18,76 @@ use crate::error::ErrorKind;
1918pub struct Identifier ( Arc < str > ) ;
2019
2120impl Identifier {
22- /// Create a new [`Identifier`] from a [`String`] , if it is valid.
21+ /// Create a new [`Identifier`] from a string , if it is valid.
2322 ///
2423 /// A valid identifier must have between 0 and 100 characters, and each
2524 /// character must be in the printable ASCII range, 0x20 to 0x7E.
26- pub fn new ( s : impl Into < Arc < str > > ) -> Result < Self , ErrorKind > {
27- let string = s. into ( ) ;
28- if is_valid_identifier ( & string) {
29- Ok ( Identifier ( string) )
25+ pub fn new ( string : & str ) -> Result < Self , ErrorKind > {
26+ if is_valid_identifier ( string) {
27+ Ok ( Identifier ( string. into ( ) ) )
3028 } else {
3129 Err ( ErrorKind :: BadIdentifier )
3230 }
3331 }
3432
33+ /// Creates a new `Identifier`, panicking if the given identifier is invalid.
34+ #[ cfg( test) ]
35+ pub ( crate ) fn new_raw ( string : & str ) -> Self {
36+ assert ! ( is_valid_identifier( string) ) ;
37+ Self ( string. into ( ) )
38+ }
39+
3540 /// Create a new [`Identifier`] from a UUID v4 identifier.
3641 pub fn from_uuidv4 ( ) -> Self {
37- Self :: new ( uuid:: Uuid :: new_v4 ( ) . to_string ( ) ) . unwrap ( )
42+ Self :: new ( uuid:: Uuid :: new_v4 ( ) . to_string ( ) . as_ref ( ) ) . unwrap ( )
3843 }
3944
4045 /// Return the raw identifier, as a `&str`.
4146 pub fn as_str ( & self ) -> & str {
42- & self . 0
47+ self . as_ref ( )
4348 }
4449}
4550
46- impl PartialEq < String > for Identifier {
47- fn eq ( & self , other : & String ) -> bool {
48- * self . 0 == * other
51+ fn is_valid_identifier ( s : & str ) -> bool {
52+ s. len ( ) <= 100 && s. bytes ( ) . all ( |b| ( 0x20 ..=0x7E ) . contains ( & b) )
53+ }
54+
55+ impl AsRef < str > for Identifier {
56+ fn as_ref ( & self ) -> & str {
57+ self . 0 . as_ref ( )
4958 }
5059}
5160
52- impl FromStr for Identifier {
53- type Err = ErrorKind ;
54- fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
55- Identifier :: new ( s )
61+ impl std :: ops :: Deref for Identifier {
62+ type Target = str ;
63+ fn deref ( & self ) -> & Self :: Target {
64+ self . 0 . as_ref ( )
5665 }
5766}
5867
59- fn is_valid_identifier ( s : & Arc < str > ) -> bool {
60- s. len ( ) <= 100 && s. bytes ( ) . all ( |b| ( 0x20 ..=0x7E ) . contains ( & b) )
68+ // so that assert_eq! macros work
69+ impl < ' a > PartialEq < & ' a str > for Identifier {
70+ fn eq ( & self , other : & & ' a str ) -> bool {
71+ self . 0 . as_ref ( ) == * other
72+ }
73+ }
74+
75+ impl < ' a > PartialEq < Identifier > for & ' a str {
76+ fn eq ( & self , other : & Identifier ) -> bool {
77+ other == self
78+ }
79+ }
80+
81+ impl std:: fmt:: Display for Identifier {
82+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> Result < ( ) , std:: fmt:: Error > {
83+ std:: fmt:: Display :: fmt ( & self . 0 , f)
84+ }
85+ }
86+
87+ impl std:: borrow:: Borrow < str > for Identifier {
88+ fn borrow ( & self ) -> & str {
89+ self . 0 . as_ref ( )
90+ }
6191}
6292
6393impl Serialize for Identifier {
@@ -79,7 +109,7 @@ impl<'de> Deserialize<'de> for Identifier {
79109 D : Deserializer < ' de > ,
80110 {
81111 let string = String :: deserialize ( deserializer) ?;
82- Identifier :: new ( string) . map_err ( de:: Error :: custom)
112+ Identifier :: new ( string. as_str ( ) ) . map_err ( de:: Error :: custom)
83113 }
84114}
85115
0 commit comments