Something went wrong. Try again.
A terminal UI for Libby libbui.vinnymac.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230//! 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<String>, /// How the server fulfils it (`bifocal`, `odm`, `epub`, …); informational. #[serde(default)] pub fulfillment_type: Option<String>, /// ISBNs and similar. #[serde(default)] pub identifiers: Vec<Identifier>, /// The format's ISBN, when the server pulls it out. #[serde(default)] pub isbn: Option<String>, /// Size in bytes. #[serde(default)] pub file_size: Option<u64>, /// Running time (audiobooks). #[serde(default, with = "crate::time::option_hms")] pub duration: Option<Duration>, /// Number of parts (audiobooks). #[serde(default)] pub part_count: Option<u32>, /// When this format went on sale. #[serde(default, with = "crate::time::option_timestamp")] pub on_sale_date_utc: Option<Timestamp>, /// A preview, when the publisher offers one. #[serde(default)] pub sample: Option<Link>, /// 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<DownloadKind> { 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) ); } }}