@@ -29,11 +29,9 @@ impl From<SpotifyIdError> for Error {
2929pub type SpotifyIdResult = Result < SpotifyId , Error > ;
3030
3131const BASE62_DIGITS : & [ u8 ; 62 ] = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
32- const BASE16_DIGITS : & [ u8 ; 16 ] = b"0123456789abcdef" ;
3332
3433impl SpotifyId {
3534 const SIZE : usize = 16 ;
36- const SIZE_BASE16 : usize = 32 ;
3735 const SIZE_BASE62 : usize = 22 ;
3836
3937 /// Parses a base16 (hex) encoded [Spotify ID] into a `SpotifyId`.
@@ -45,20 +43,9 @@ impl SpotifyId {
4543 if src. len ( ) != 32 {
4644 return Err ( SpotifyIdError :: InvalidId . into ( ) ) ;
4745 }
48- let mut dst: u128 = 0 ;
49-
50- for c in src. as_bytes ( ) {
51- let p = match c {
52- b'0' ..=b'9' => c - b'0' ,
53- b'a' ..=b'f' => c - b'a' + 10 ,
54- _ => return Err ( SpotifyIdError :: InvalidId . into ( ) ) ,
55- } as u128 ;
56-
57- dst <<= 4 ;
58- dst += p;
59- }
46+ let id = u128:: from_str_radix ( src, 16 ) . map_err ( |_| SpotifyIdError :: InvalidId ) ?;
6047
61- Ok ( Self { id : dst } )
48+ Ok ( Self { id } )
6249 }
6350
6451 /// Parses a base62 encoded [Spotify ID] into a `u128`.
@@ -99,19 +86,18 @@ impl SpotifyId {
9986 }
10087 }
10188
102- /// Returns the `SpotifyId` as a base16 (hex) encoded, `SpotifyId::SIZE_BASE16` (32)
103- /// character long `String`.
89+ /// Returns the `SpotifyId` as a base16 (hex) encoded, 32-character long `String`.
10490 #[ allow( clippy:: wrong_self_convention) ]
105- pub fn to_base16 ( & self ) -> Result < String , Error > {
106- to_base16 ( & self . to_raw ( ) , & mut [ 0u8 ; Self :: SIZE_BASE16 ] )
91+ pub fn to_base16 ( & self ) -> String {
92+ format ! ( "{:032x}" , self . id )
10793 }
10894
10995 /// Returns the `SpotifyId` as a [canonically] base62 encoded, `SpotifyId::SIZE_BASE62` (22)
11096 /// character long `String`.
11197 ///
11298 /// [canonically]: https://developer.spotify.com/documentation/web-api/concepts/spotify-uris-ids
11399 #[ allow( clippy:: wrong_self_convention) ]
114- pub fn to_base62 ( & self ) -> Result < String , Error > {
100+ pub fn to_base62 ( & self ) -> String {
115101 let mut dst = [ 0u8 ; 22 ] ;
116102 let mut i = 0 ;
117103 let n = self . id ;
@@ -143,13 +129,12 @@ impl SpotifyId {
143129 }
144130 }
145131
146- for b in & mut dst {
147- * b = BASE62_DIGITS [ * b as usize ] ;
132+ let mut s = String :: with_capacity ( dst. len ( ) ) ;
133+ for & b in dst. iter ( ) . rev ( ) {
134+ s. push ( BASE62_DIGITS [ b as usize ] as char ) ;
148135 }
149136
150- dst. reverse ( ) ;
151-
152- String :: from_utf8 ( dst. to_vec ( ) ) . map_err ( |_| SpotifyIdError :: InvalidId . into ( ) )
137+ s
153138 }
154139
155140 /// Returns a copy of the `SpotifyId` as an array of `SpotifyId::SIZE` (16) bytes in
@@ -162,15 +147,13 @@ impl SpotifyId {
162147
163148impl fmt:: Debug for SpotifyId {
164149 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
165- f. debug_tuple ( "SpotifyId" )
166- . field ( & self . to_base62 ( ) . unwrap_or_else ( |_| "invalid uri" . into ( ) ) )
167- . finish ( )
150+ f. debug_tuple ( "SpotifyId" ) . field ( & self . to_base62 ( ) ) . finish ( )
168151 }
169152}
170153
171154impl fmt:: Display for SpotifyId {
172155 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
173- f. write_str ( & self . to_base62 ( ) . unwrap_or_else ( |_| "invalid uri" . into ( ) ) )
156+ f. write_str ( & self . to_base62 ( ) )
174157 }
175158}
176159
@@ -219,17 +202,6 @@ impl TryFrom<&SpotifyUri> for SpotifyId {
219202 }
220203}
221204
222- pub fn to_base16 ( src : & [ u8 ] , buf : & mut [ u8 ] ) -> Result < String , Error > {
223- let mut i = 0 ;
224- for v in src {
225- buf[ i] = BASE16_DIGITS [ ( v >> 4 ) as usize ] ;
226- buf[ i + 1 ] = BASE16_DIGITS [ ( v & 0x0f ) as usize ] ;
227- i += 2 ;
228- }
229-
230- String :: from_utf8 ( buf. to_vec ( ) ) . map_err ( |_| SpotifyIdError :: InvalidId . into ( ) )
231- }
232-
233205#[ cfg( test) ]
234206mod tests {
235207 use super :: * ;
@@ -350,7 +322,7 @@ mod tests {
350322 for c in & CONV_VALID {
351323 let id = SpotifyId { id : c. id } ;
352324
353- assert_eq ! ( id. to_base62( ) . unwrap ( ) , c. base62) ;
325+ assert_eq ! ( id. to_base62( ) , c. base62) ;
354326 }
355327 }
356328
@@ -370,7 +342,7 @@ mod tests {
370342 for c in & CONV_VALID {
371343 let id = SpotifyId { id : c. id } ;
372344
373- assert_eq ! ( id. to_base16( ) . unwrap ( ) , c. base16) ;
345+ assert_eq ! ( id. to_base16( ) , c. base16) ;
374346 }
375347 }
376348
0 commit comments