Files

196 lines
5.2 KiB
Rust

//! 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, HabitGoal, Mark, Outcome, Person, WordMark};
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,
/// The device's UTC offset at `start`, in minutes (Day 2); `None` for older sessions.
#[serde(default, skip_serializing_if = "Option::is_none")]
offset_min: Option<i32>,
},
Goal {
year: i32,
kind: GoalKind,
target: u32,
},
/// The daily habit (Day 2): what counts as a day read, and when a day rolls over. Follows the person.
HabitGoal {
goal: HabitGoal,
},
/// A medal was shown (Day 2). Medals themselves are computed from the log, never stored.
MedalSeen {
medal: String,
},
/// Day 3: the reader marked a word (a folded lemma, see `liber_lang::fold`). Last writer wins.
Word {
lang: String,
lemma: String,
mark: WordMark,
},
/// Day 3: the band of a frequency list the reader knows.
KnownBand {
lang: String,
list: String,
upto: u32,
},
/// Day 4: where an audiobook file is, to the millisecond (the latest write wins, so any device resumes there).
AudioPosition {
book: String,
sha256: String,
ms: u64,
},
/// Day 3: a book file is an edition in another language (`None`: it is the book's own language again).
Edition {
book: String,
sha256: String,
language: Option<String>,
},
}