//! Formats and their currently supported fulfilment paths. //! //! Open EPUB/PDF formats download as content files, Adobe formats yield an //! `.acsm` token, and Kindle hands off to Amazon. Formats without an //! implemented download path use an "open in Libby" action. use std::time::Duration; use jiff::Timestamp; use serde::{Deserialize, Serialize}; use super::catalog::Link; use crate::ids::FormatId; /// One format a title is available in (or a loan was made in). #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Format { /// Format id, e.g. `ebook-epub-open`. pub id: FormatId, /// Display name, e.g. `"OverDrive Listen audiobook"`. #[serde(default)] pub name: Option, /// How the server fulfils it (`bifocal`, `odm`, `epub`, …); informational. #[serde(default)] pub fulfillment_type: Option, /// ISBNs and similar. #[serde(default)] pub identifiers: Vec, /// The format's ISBN, when the server pulls it out. #[serde(default)] pub isbn: Option, /// Size in bytes. #[serde(default)] pub file_size: Option, /// Running time (audiobooks). #[serde(default, with = "crate::time::option_hms")] pub duration: Option, /// Number of parts (audiobooks). #[serde(default)] pub part_count: Option, /// When this format went on sale. #[serde(default, with = "crate::time::option_timestamp")] pub on_sale_date_utc: Option, /// A preview, when the publisher offers one. #[serde(default)] pub sample: Option, /// On a loan: whether the loan is locked into this format. #[serde(default)] pub is_locked_in: bool, } impl Format { /// The currently supported fulfilment path for this format. #[must_use] pub fn access(&self) -> FormatAccess { FormatAccess::of(&self.id) } } /// A typed identifier such as `{"type": "ISBN", "value": "978…"}`. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct Identifier { /// The identifier scheme (`ISBN`, `ASIN`, …). #[serde(rename = "type")] pub kind: String, /// The identifier. pub value: String, } /// What libbui can do with a format. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum FormatAccess { /// The library serves a file for it. Download(DownloadKind), /// Delivered to Kindle via Amazon: open the returned link in a browser. Kindle, /// No download path is implemented. Offer "open in Libby". LibbyOnly, } impl FormatAccess { /// Classifies a format id. Ids this client does not know are /// [`FormatAccess::LibbyOnly`]. #[must_use] pub fn of(format: &FormatId) -> Self { match format.as_str() { "ebook-epub-open" => Self::Download(DownloadKind::EpubOpen), "ebook-overdrive" => Self::Download(DownloadKind::EpubReader), "audiobook-overdrive" => Self::Download(DownloadKind::AudioReader), "magazine-overdrive" => Self::Download(DownloadKind::MagazineReader), "ebook-pdf-open" => Self::Download(DownloadKind::PdfOpen), "ebook-epub-adobe" => Self::Download(DownloadKind::EpubAcsm), "ebook-pdf-adobe" => Self::Download(DownloadKind::PdfAcsm), "ebook-kindle" => Self::Kindle, _ => Self::LibbyOnly, } } /// The download kind, when there is one. #[must_use] pub fn download_kind(self) -> Option { match self { Self::Download(kind) => Some(kind), Self::Kindle | Self::LibbyOnly => None, } } } /// The kinds of file the library will hand over. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum DownloadKind { /// An open EPUB (`ebook-epub-open`). EpubOpen, /// An EPUB assembled from the authorized reader (`ebook-overdrive`). EpubReader, /// An audiobook assembled from the authorized reader. AudioReader, /// A magazine EPUB assembled from the authorized reader. MagazineReader, /// An open PDF (`ebook-pdf-open`). PdfOpen, /// An Adobe `.acsm` fulfilment token for an EPUB. EpubAcsm, /// An Adobe `.acsm` fulfilment token for a PDF. PdfAcsm, } impl DownloadKind { /// Extension of the fulfilment artifact. Adobe fulfilment yields an ACSM /// licence; a downstream processor produces the final EPUB or PDF. #[must_use] pub fn file_extension(self) -> &'static str { match self { Self::EpubOpen | Self::EpubReader | Self::MagazineReader => "epub", Self::PdfOpen => "pdf", Self::AudioReader => "mp3", Self::EpubAcsm | Self::PdfAcsm => "acsm", } } /// Whether fulfilment produces content rather than a licence that needs /// further processing. #[must_use] pub fn is_content_file(self) -> bool { matches!( self, Self::EpubOpen | Self::EpubReader | Self::AudioReader | Self::MagazineReader | Self::PdfOpen ) } /// The format id this kind is fulfilled through. #[must_use] pub fn format_id(self) -> FormatId { FormatId::new(match self { Self::EpubOpen => "ebook-epub-open", Self::EpubReader => "ebook-overdrive", Self::AudioReader => "audiobook-overdrive", Self::MagazineReader => "magazine-overdrive", Self::PdfOpen => "ebook-pdf-open", Self::EpubAcsm => "ebook-epub-adobe", Self::PdfAcsm => "ebook-pdf-adobe", }) } } #[cfg(test)] mod tests { use super::*; #[test] fn classification_is_conservative() { let cases = [ ( "ebook-epub-open", FormatAccess::Download(DownloadKind::EpubOpen), ), ( "ebook-pdf-open", FormatAccess::Download(DownloadKind::PdfOpen), ), ( "ebook-epub-adobe", FormatAccess::Download(DownloadKind::EpubAcsm), ), ( "ebook-pdf-adobe", FormatAccess::Download(DownloadKind::PdfAcsm), ), ("ebook-kindle", FormatAccess::Kindle), ( "ebook-overdrive", FormatAccess::Download(DownloadKind::EpubReader), ), ( "audiobook-overdrive", FormatAccess::Download(DownloadKind::AudioReader), ), ("audiobook-mp3", FormatAccess::LibbyOnly), ( "magazine-overdrive", FormatAccess::Download(DownloadKind::MagazineReader), ), ("ebook-kobo", FormatAccess::LibbyOnly), ("something-new", FormatAccess::LibbyOnly), ]; for (id, expected) in cases { assert_eq!(FormatAccess::of(&FormatId::new(id)), expected, "{id}"); } for kind in [ DownloadKind::EpubOpen, DownloadKind::EpubReader, DownloadKind::AudioReader, DownloadKind::MagazineReader, DownloadKind::PdfOpen, DownloadKind::EpubAcsm, DownloadKind::PdfAcsm, ] { assert_eq!( FormatAccess::of(&kind.format_id()), FormatAccess::Download(kind) ); } } }