85 lines
2.4 KiB
Rust
85 lines
2.4 KiB
Rust
//! Liber's library (docs in the app repo, `docs/01_books.md`): the model, the event log and its merge, stats,
|
|
//! Goodreads/StoryGraph import and the `liber-export` format. Pure: time, ids and devices are parameters.
|
|
//!
|
|
//! Every change is an [`Event`]; the [`Library`] is a fold of the events in their total order
|
|
//! (hybrid logical clock, then device, then id). Two devices holding the same events hold the same library,
|
|
//! whatever order the events arrived in: that is the whole sync guarantee.
|
|
|
|
pub mod event;
|
|
pub mod export;
|
|
pub mod import;
|
|
pub mod library;
|
|
pub mod stats;
|
|
|
|
pub use event::{Clock, Event, Hlc, Op};
|
|
pub use library::{Book, Highlight, Library, Loan, Possession, Read, ReadStatus, Text};
|
|
|
|
pub use liber_format::text::isbn13;
|
|
|
|
/// StoryGraph's moods: Liber's fixed mood list (own tags are separate).
|
|
pub const MOODS: &[&str] = &[
|
|
"adventurous",
|
|
"challenging",
|
|
"dark",
|
|
"emotional",
|
|
"funny",
|
|
"hopeful",
|
|
"informative",
|
|
"inspiring",
|
|
"lighthearted",
|
|
"mysterious",
|
|
"reflective",
|
|
"relaxing",
|
|
"sad",
|
|
"tense",
|
|
];
|
|
|
|
/// Days since 1970-01-01 for a civil date (Howard Hinnant's algorithm).
|
|
pub fn days_from_civil(y: i64, m: u32, d: u32) -> i64 {
|
|
let y = if m <= 2 { y - 1 } else { y };
|
|
let era = if y >= 0 { y } else { y - 399 } / 400;
|
|
let yoe = y - era * 400;
|
|
let mp = (m as i64 + 9) % 12;
|
|
let doy = (153 * mp + 2) / 5 + d as i64 - 1;
|
|
let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
|
|
era * 146_097 + doe - 719_468
|
|
}
|
|
|
|
/// `YYYY-MM-DD` (or `YYYY/MM/DD`) → `YYYY-MM-DD`, checked.
|
|
pub fn parse_date(s: &str) -> Option<String> {
|
|
let s = s.trim();
|
|
let parts: Vec<&str> = s.split(['-', '/']).collect();
|
|
if parts.len() != 3 {
|
|
return None;
|
|
}
|
|
let y: i32 = parts[0].parse().ok()?;
|
|
let m: u32 = parts[1].parse().ok()?;
|
|
let d: u32 = parts[2].parse().ok()?;
|
|
if !(1..=9999).contains(&y) || !(1..=12).contains(&m) || !(1..=31).contains(&d) {
|
|
return None;
|
|
}
|
|
let dim = [
|
|
31,
|
|
if y % 4 == 0 && (y % 100 != 0 || y % 400 == 0) { 29 } else { 28 },
|
|
31,
|
|
30,
|
|
31,
|
|
30,
|
|
31,
|
|
31,
|
|
30,
|
|
31,
|
|
30,
|
|
31,
|
|
];
|
|
if d > dim[(m - 1) as usize] {
|
|
return None;
|
|
}
|
|
Some(format!("{y:04}-{m:02}-{d:02}"))
|
|
}
|
|
|
|
/// The year of a `YYYY-MM-DD` date.
|
|
pub fn year_of(date: &str) -> Option<i32> {
|
|
date.get(..4)?.parse().ok()
|
|
}
|