//! Identifier newtypes. //! //! OverDrive has several ids that look interchangeable and are not. A title id //! is a string of digits, a reserve id is a GUID for the same title, and a //! website id is a *string* on sentry but an *integer* on thunder. Newtypes //! make it a compile error to pass one where another is wanted. use std::fmt; use serde::{Deserialize, Deserializer, Serialize}; macro_rules! string_id { ($(#[$meta:meta])* $name:ident) => { $(#[$meta])* #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] #[serde(transparent)] pub struct $name(String); impl $name { /// Wraps a raw id. No validation: the server is the authority. pub fn new(raw: impl Into) -> Self { Self(raw.into()) } /// The raw id, as the server spells it. #[must_use] pub fn as_str(&self) -> &str { &self.0 } } impl fmt::Display for $name { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(&self.0) } } impl From<&str> for $name { fn from(raw: &str) -> Self { Self(raw.to_owned()) } } impl From for $name { fn from(raw: String) -> Self { Self(raw) } } }; } string_id!( /// A title ("media") id: a string of digits such as `"2308988"`. /// /// This is the id used in every sentry action and in thunder lookups. TitleId ); string_id!( /// The GUID form of a title id. Its letter case varies between endpoints, /// so compare with [`ReserveId::eq_ignore_case`] rather than `==`. ReserveId ); string_id!( /// A library card on the signed-in account. CardId ); string_id!( /// A library's catalog key (`"lapl"`), called `advantageKey` on a card and /// `preferredKey`/`id` on thunder. LibraryKey ); string_id!( /// A format id such as `"ebook-epub-open"` or `"audiobook-overdrive"`. FormatId ); impl ReserveId { /// Compares two reserve ids ignoring ASCII case. #[must_use] pub fn eq_ignore_case(&self, other: &Self) -> bool { self.0.eq_ignore_ascii_case(&other.0) } } /// A library's numeric website id. /// /// Sentry sends it as a string (`"78"`), thunder as a number (`78`); both /// deserialize here and it always serializes as a number. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)] #[serde(transparent)] pub struct WebsiteId(u32); impl WebsiteId { /// Wraps a raw website id. #[must_use] pub const fn new(raw: u32) -> Self { Self(raw) } /// The raw website id. #[must_use] pub const fn get(self) -> u32 { self.0 } } impl fmt::Display for WebsiteId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } impl<'de> Deserialize<'de> for WebsiteId { fn deserialize>(deserializer: D) -> Result { #[derive(Deserialize)] #[serde(untagged)] enum Raw { Number(u32), Text(String), } match Raw::deserialize(deserializer)? { Raw::Number(n) => Ok(Self(n)), Raw::Text(s) => s.trim().parse().map(Self).map_err(|_| { serde::de::Error::custom(format!("website id is not a number: {s:?}")) }), } } } #[cfg(test)] mod tests { use super::*; #[test] fn website_id_accepts_both_spellings() { let from_text: WebsiteId = serde_json::from_str("\"78\"").expect("string form"); let from_number: WebsiteId = serde_json::from_str("78").expect("number form"); assert_eq!(from_text, from_number); assert_eq!(serde_json::to_string(&from_text).expect("serialize"), "78"); } #[test] fn website_id_rejects_garbage_loudly() { assert!(serde_json::from_str::("\"seventy\"").is_err()); } #[test] fn reserve_ids_compare_case_insensitively() { let lower = ReserveId::new("a1b2c3d4-0000-4000-8000-00000000abcd"); let upper = ReserveId::new("A1B2C3D4-0000-4000-8000-00000000ABCD"); assert_ne!(lower, upper); assert!(lower.eq_ignore_case(&upper)); } }