[format][library] Books: EPUB/FB2/TXT/MOBI/AZW3 into one sanitized model, CFI anchors, the event-log library, stats, CSV import, export

This commit is contained in:
2026-11-17 01:36:21 +01:00
parent 0b0e35d727
commit 8729119984
35 changed files with 7956 additions and 0 deletions
+160
View File
@@ -0,0 +1,160 @@
//! Events: what a device did, with a hybrid logical clock. The order of events is total and the same everywhere.
use crate::library::{BookFile, BookInfo, Colour, Field, GoalKind, Mark, Outcome, Person};
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
/// A hybrid logical clock value: wall milliseconds, then a counter.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Hlc {
pub wall: u64,
pub logical: u32,
}
/// One device's clock. `now_ms` is always a parameter.
#[derive(Clone, Debug, Default)]
pub struct Clock {
last: Hlc,
}
impl Clock {
pub fn tick(&mut self, now_ms: u64) -> Hlc {
self.last = if now_ms > self.last.wall {
Hlc { wall: now_ms, logical: 0 }
} else {
Hlc { wall: self.last.wall, logical: self.last.logical + 1 }
};
self.last
}
/// Takes in a clock value seen on another device, so later local events order after it.
pub fn observe(&mut self, seen: Hlc) {
if seen > self.last {
self.last = seen;
}
}
pub fn last(&self) -> Hlc {
self.last
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Event {
/// Unique: made by the device (random, or derived from the device and a counter).
pub id: String,
pub hlc: Hlc,
pub device: String,
pub op: Op,
}
impl Event {
/// The total order: clock, then device, then id.
pub fn order(&self, other: &Event) -> Ordering {
(self.hlc, &self.device, &self.id).cmp(&(other.hlc, &other.device, &other.id))
}
}
/// Every change Liber records. Unknown fields are refused, so an event from a newer schema is rejected rather than
/// half-applied.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "op", rename_all = "snake_case", deny_unknown_fields)]
pub enum Op {
/// `restore`: from an export, where two books may share an identity on purpose: never merge.
BookAdd {
book: String,
info: BookInfo,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
restore: bool,
},
/// `base`: the id of the event whose value the editor saw (`Library::version`). For text fields, a different
/// base means a concurrent edit: both versions are kept.
BookSet {
book: String,
field: Field,
value: serde_json::Value,
base: Option<String>,
},
BookRemove {
book: String,
},
BookFileAdd {
book: String,
file: BookFile,
},
BookFileRemove {
book: String,
sha256: String,
},
ShelfAdd {
book: String,
shelf: String,
},
ShelfRemove {
book: String,
shelf: String,
},
ReadStart {
book: String,
read: String,
date: Option<String>,
},
ReadEnd {
read: String,
date: Option<String>,
outcome: Outcome,
},
ReadRemove {
read: String,
},
Lend {
loan: String,
book: String,
person: Person,
since: String,
due: Option<String>,
},
Return {
loan: String,
date: String,
},
Position {
book: String,
cfi: String,
chapter: u32,
progress: f64,
label: Option<String>,
},
HighlightAdd {
id: String,
book: String,
kind: Mark,
anchor: serde_json::Value,
colour: Colour,
note: String,
created: u64,
},
HighlightSet {
id: String,
colour: Option<Colour>,
note: Option<String>,
base: Option<String>,
},
HighlightRemove {
id: String,
},
Session {
id: String,
book: String,
start: u64,
end: u64,
active_ms: u64,
pages: u32,
date: String,
},
Goal {
year: i32,
kind: GoalKind,
target: u32,
},
}