44use serde:: { Deserialize , Serialize } ;
55use std:: { fmt, str:: FromStr } ;
66
7+ use crate :: http:: headers:: HeaderValue ;
8+
79/// Represents an ETag for versioned resources.
810#[ derive( Debug , Clone , PartialEq , Eq , Serialize , Deserialize ) ]
911pub struct Etag ( String ) ;
1012
11- impl < T > From < T > for Etag
12- where
13- T : Into < String > ,
14- {
15- fn from ( t : T ) -> Self {
16- Self ( t. into ( ) )
13+ // Implementation for common string types
14+ impl From < & str > for Etag {
15+ fn from ( s : & str ) -> Self {
16+ Self ( s. to_string ( ) )
17+ }
18+ }
19+
20+ impl From < String > for Etag {
21+ fn from ( s : String ) -> Self {
22+ Self ( s)
1723 }
1824}
1925
@@ -35,3 +41,59 @@ impl fmt::Display for Etag {
3541 fmt:: Display :: fmt ( & self . 0 , f)
3642 }
3743}
44+
45+ impl From < Etag > for String {
46+ fn from ( etag : Etag ) -> Self {
47+ etag. 0
48+ }
49+ }
50+
51+ impl From < Etag > for HeaderValue {
52+ fn from ( etag : Etag ) -> Self {
53+ HeaderValue :: from ( String :: from ( etag) )
54+ }
55+ }
56+
57+ #[ cfg( test) ]
58+ mod tests {
59+ use super :: * ;
60+ use crate :: http:: headers:: HeaderValue ;
61+
62+ #[ test]
63+ fn from_string ( ) {
64+ let etag = Etag :: from ( "test-etag" ) ;
65+ assert_eq ! ( "test-etag" , etag. 0 ) ;
66+ }
67+
68+ #[ test]
69+ fn from_str ( ) {
70+ let etag = "test-etag" . parse :: < Etag > ( ) . unwrap ( ) ;
71+ assert_eq ! ( "test-etag" , etag. 0 ) ;
72+ }
73+
74+ #[ test]
75+ fn as_ref ( ) {
76+ let etag = Etag :: from ( "test-etag" ) ;
77+ assert_eq ! ( "test-etag" , etag. as_ref( ) ) ;
78+ }
79+
80+ #[ test]
81+ fn display ( ) {
82+ let etag = Etag :: from ( "test-etag" ) ;
83+ assert_eq ! ( "test-etag" , etag. to_string( ) ) ;
84+ }
85+
86+ #[ test]
87+ fn to_string ( ) {
88+ let etag = Etag :: from ( "test-etag" ) ;
89+ let s: String = etag. into ( ) ;
90+ assert_eq ! ( "test-etag" , s) ;
91+ }
92+
93+ #[ test]
94+ fn to_header_value ( ) {
95+ let etag = Etag :: from ( "test-etag" ) ;
96+ let header_value: HeaderValue = etag. into ( ) ;
97+ assert_eq ! ( "test-etag" , header_value. as_str( ) ) ;
98+ }
99+ }
0 commit comments