spotify_id.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. use std;
  2. use std::fmt;
  3. use util::u128;
  4. use byteorder::{BigEndian, ByteOrder};
  5. // Unneeded since 1.21
  6. #[allow(unused_imports)]
  7. use std::ascii::AsciiExt;
  8. #[derive(Debug,Copy,Clone,PartialEq,Eq,Hash)]
  9. pub struct SpotifyId(u128);
  10. const BASE62_DIGITS: &'static [u8] =
  11. b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  12. const BASE16_DIGITS: &'static [u8] = b"0123456789abcdef";
  13. impl SpotifyId {
  14. pub fn from_base16(id: &str) -> SpotifyId {
  15. assert!(id.is_ascii());
  16. let data = id.as_bytes();
  17. let mut n: u128 = u128::zero();
  18. for c in data {
  19. let d = BASE16_DIGITS.iter().position(|e| e == c).unwrap() as u8;
  20. n = n * u128::from(16);
  21. n = n + u128::from(d);
  22. }
  23. SpotifyId(n)
  24. }
  25. pub fn from_base62(id: &str) -> SpotifyId {
  26. assert!(id.is_ascii());
  27. let data = id.as_bytes();
  28. let mut n: u128 = u128::zero();
  29. for c in data {
  30. let d = BASE62_DIGITS.iter().position(|e| e == c).unwrap() as u8;
  31. n = n * u128::from(62);
  32. n = n + u128::from(d);
  33. }
  34. SpotifyId(n)
  35. }
  36. pub fn from_raw(data: &[u8]) -> SpotifyId {
  37. assert_eq!(data.len(), 16);
  38. let high = BigEndian::read_u64(&data[0..8]);
  39. let low = BigEndian::read_u64(&data[8..16]);
  40. SpotifyId(u128::from_parts(high, low))
  41. }
  42. pub fn to_base16(&self) -> String {
  43. let &SpotifyId(ref n) = self;
  44. let (high, low) = n.parts();
  45. let mut data = [0u8; 32];
  46. for i in 0..16 {
  47. data[31 - i] = BASE16_DIGITS[(low.wrapping_shr(4 * i as u32) & 0xF) as usize];
  48. }
  49. for i in 0..16 {
  50. data[15 - i] = BASE16_DIGITS[(high.wrapping_shr(4 * i as u32) & 0xF) as usize];
  51. }
  52. std::str::from_utf8(&data).unwrap().to_owned()
  53. }
  54. pub fn to_raw(&self) -> [u8; 16] {
  55. let &SpotifyId(ref n) = self;
  56. let (high, low) = n.parts();
  57. let mut data = [0u8; 16];
  58. BigEndian::write_u64(&mut data[0..8], high);
  59. BigEndian::write_u64(&mut data[8..16], low);
  60. data
  61. }
  62. }
  63. #[derive(Copy,Clone,PartialEq,Eq,PartialOrd,Ord,Hash)]
  64. pub struct FileId(pub [u8; 20]);
  65. impl FileId {
  66. pub fn to_base16(&self) -> String {
  67. self.0
  68. .iter()
  69. .map(|b| format!("{:02x}", b))
  70. .collect::<Vec<String>>()
  71. .concat()
  72. }
  73. }
  74. impl fmt::Debug for FileId {
  75. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  76. f.debug_tuple("FileId").field(&self.to_base16()).finish()
  77. }
  78. }
  79. impl fmt::Display for FileId {
  80. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  81. f.write_str(&self.to_base16())
  82. }
  83. }