1
- use std:: convert:: TryFrom ;
2
1
use std:: fmt;
3
2
use std:: str:: FromStr ;
4
3
@@ -8,88 +7,45 @@ use thiserror::Error;
8
7
/// Raised if a project ID cannot be parsed from a string.
9
8
#[ derive( Debug , Error , PartialEq , Eq , PartialOrd , Ord ) ]
10
9
pub enum ParseProjectIdError {
11
- /// Raised if the value is not an integer in the supported range.
12
- #[ error( "invalid value for project id" ) ]
13
- InvalidValue ,
14
10
/// Raised if an empty value is parsed.
15
11
#[ error( "empty or missing project id" ) ]
16
12
EmptyValue ,
17
13
}
18
14
19
15
/// Represents a project ID.
20
- #[ derive( Copy , Clone , Debug , PartialEq , Eq , Ord , PartialOrd , Hash , Deserialize , Serialize ) ]
21
- pub struct ProjectId ( u64 ) ;
16
+ #[ derive( Clone , Debug , PartialEq , Eq , Ord , PartialOrd , Hash , Deserialize , Serialize ) ]
17
+ pub struct ProjectId ( String ) ;
22
18
23
19
impl ProjectId {
24
- /// Creates a new project ID from its numeric value.
20
+ /// Creates a new project ID from its string representation.
21
+ /// This assumes that the string is already well-formed and URL
22
+ /// encoded/decoded.
25
23
#[ inline]
26
- pub fn new ( id : u64 ) -> Self {
27
- Self ( id)
24
+ pub fn new ( id : & str ) -> Self {
25
+ Self ( id. to_string ( ) )
28
26
}
29
27
30
- /// Returns the numeric value of this project id .
28
+ /// Returns the string representation of the project ID .
31
29
#[ inline]
32
- pub fn value ( self ) -> u64 {
33
- self . 0
30
+ pub fn value ( & self ) -> & str {
31
+ self . 0 . as_ref ( )
34
32
}
35
33
}
36
34
37
35
impl fmt:: Display for ProjectId {
38
36
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
39
- write ! ( f, "{}" , self . value ( ) )
37
+ write ! ( f, "{}" , self . 0 )
40
38
}
41
39
}
42
40
43
- macro_rules! impl_from {
44
- ( $ty: ty) => {
45
- impl From <$ty> for ProjectId {
46
- #[ inline]
47
- fn from( val: $ty) -> Self {
48
- Self :: new( val as u64 )
49
- }
50
- }
51
- } ;
52
- }
53
-
54
- impl_from ! ( u8 ) ;
55
- impl_from ! ( u16 ) ;
56
- impl_from ! ( u32 ) ;
57
- impl_from ! ( u64 ) ;
58
-
59
- macro_rules! impl_try_from {
60
- ( $ty: ty) => {
61
- impl TryFrom <$ty> for ProjectId {
62
- type Error = ParseProjectIdError ;
63
-
64
- #[ inline]
65
- fn try_from( val: $ty) -> Result <Self , Self :: Error > {
66
- match u64 :: try_from( val) {
67
- Ok ( id) => Ok ( Self :: new( id) ) ,
68
- Err ( _) => Err ( ParseProjectIdError :: InvalidValue ) ,
69
- }
70
- }
71
- }
72
- } ;
73
- }
74
-
75
- impl_try_from ! ( usize ) ;
76
- impl_try_from ! ( i8 ) ;
77
- impl_try_from ! ( i16 ) ;
78
- impl_try_from ! ( i32 ) ;
79
- impl_try_from ! ( i64 ) ;
80
-
81
41
impl FromStr for ProjectId {
82
42
type Err = ParseProjectIdError ;
83
43
84
44
fn from_str ( s : & str ) -> Result < ProjectId , ParseProjectIdError > {
85
45
if s. is_empty ( ) {
86
46
return Err ( ParseProjectIdError :: EmptyValue ) ;
87
47
}
88
-
89
- match s. parse :: < u64 > ( ) {
90
- Ok ( val) => Ok ( ProjectId :: new ( val) ) ,
91
- Err ( _) => Err ( ParseProjectIdError :: InvalidValue ) ,
92
- }
48
+ Ok ( ProjectId :: new ( s) )
93
49
}
94
50
}
95
51
@@ -100,21 +56,21 @@ mod test {
100
56
#[ test]
101
57
fn test_basic_api ( ) {
102
58
let id: ProjectId = "42" . parse ( ) . unwrap ( ) ;
103
- assert_eq ! ( id, ProjectId :: new( 42 ) ) ;
104
- assert_eq ! (
105
- "42xxx" . parse:: <ProjectId >( ) ,
106
- Err ( ParseProjectIdError :: InvalidValue )
107
- ) ;
59
+ assert_eq ! ( id, ProjectId :: new( "42" ) ) ;
60
+ assert_eq ! ( "42xxx" . parse:: <ProjectId >( ) . unwrap( ) . value( ) , "42xxx" ) ;
108
61
assert_eq ! (
109
62
"" . parse:: <ProjectId >( ) ,
110
63
Err ( ParseProjectIdError :: EmptyValue )
111
64
) ;
112
- assert_eq ! ( ProjectId :: new( 42 ) . to_string( ) , "42" ) ;
65
+ assert_eq ! ( ProjectId :: new( "42" ) . to_string( ) , "42" ) ;
113
66
114
- assert_eq ! ( serde_json:: to_string( & ProjectId :: new( 42 ) ) . unwrap( ) , "42" ) ;
115
67
assert_eq ! (
116
- serde_json:: from_str:: <ProjectId >( "42" ) . unwrap( ) ,
117
- ProjectId :: new( 42 )
68
+ serde_json:: to_string( & ProjectId :: new( "42" ) ) . unwrap( ) ,
69
+ "\" 42\" "
70
+ ) ;
71
+ assert_eq ! (
72
+ serde_json:: from_str:: <ProjectId >( "\" 42\" " ) . unwrap( ) ,
73
+ ProjectId :: new( "42" )
118
74
) ;
119
75
}
120
76
}
0 commit comments