Clean up architecture

This commit is contained in:
2026-03-25 17:53:44 +01:00
parent 17ddf9071e
commit d4872a1a04
31 changed files with 1490 additions and 1104 deletions

View File

@@ -0,0 +1,214 @@
use crate::ids::{AnnotationId, ReferenceId};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
/// A point in PDF coordinate space.
/// Origin is bottom-left; units are points (1/72 inch), matching ISO 32000.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Point {
pub x: f64,
pub y: f64,
}
/// A rectangle in PDF coordinate space.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Rect {
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
}
/// A quadrilateral for text markup annotations (highlight, underline, etc.).
/// Four points define one region, typically one line of text.
/// Matches the PDF spec QuadPoints representation (4 vertices per quad).
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Quad {
pub points: [Point; 4],
}
/// RGBA color.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
impl Color {
pub const YELLOW: Color = Color { r: 255, g: 255, b: 0, a: 128 };
pub const RED: Color = Color { r: 255, g: 0, b: 0, a: 128 };
pub const GREEN: Color = Color { r: 0, g: 255, b: 0, a: 128 };
}
/// The four text markup annotation types defined in ISO 32000.
/// All share the same QuadPoints-based geometry.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TextMarkupType {
Highlight,
Underline,
Squiggly,
StrikeOut,
}
/// The kind of annotation and its type-specific geometry/data.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum AnnotationType {
/// Text markup (highlight, underline, squiggly, strikeout).
/// Uses QuadPoints per PDF spec for precise multi-line region selection.
TextMarkup {
markup_type: TextMarkupType,
quads: Vec<Quad>,
color: Color,
/// The selected text, stored for search and export without re-reading the PDF.
selected_text: Option<String>,
},
/// Sticky note (popup comment).
Note { position: Point },
/// Inline text box.
FreeText { rect: Rect },
/// Freehand ink drawing (e.g., circling a diagram).
Ink {
/// Multiple strokes, each a sequence of connected points.
paths: Vec<Vec<Point>>,
color: Color,
/// Stroke width in points.
width: f64,
},
/// Area/image selection for extracting figures from PDFs.
Area { rect: Rect },
}
/// A single annotation on a PDF page.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Annotation {
pub id: AnnotationId,
pub reference_id: ReferenceId,
/// 0-indexed physical page number.
pub page: u32,
/// Display page label (e.g., "iv", "23") — may differ from the physical page index.
pub page_label: Option<String>,
pub annotation_type: AnnotationType,
/// Free-form text: note body, comment on a highlight, etc.
pub content: Option<String>,
pub created_at: DateTime<Utc>,
pub modified_at: DateTime<Utc>,
}
impl Annotation {
pub fn new(reference_id: ReferenceId, page: u32, annotation_type: AnnotationType) -> Self {
let now = Utc::now();
Self {
id: AnnotationId::new(),
reference_id,
page,
page_label: None,
annotation_type,
content: None,
created_at: now,
modified_at: now,
}
}
}
/// All annotations for a single reference, stored as one file.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AnnotationSet {
pub reference_id: ReferenceId,
pub annotations: Vec<Annotation>,
}
impl AnnotationSet {
pub fn new(reference_id: ReferenceId) -> Self {
Self {
reference_id,
annotations: Vec::new(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn make_highlight() -> AnnotationType {
AnnotationType::TextMarkup {
markup_type: TextMarkupType::Highlight,
quads: vec![Quad {
points: [
Point { x: 10.0, y: 20.0 },
Point { x: 100.0, y: 20.0 },
Point { x: 10.0, y: 30.0 },
Point { x: 100.0, y: 30.0 },
],
}],
color: Color::YELLOW,
selected_text: Some("important text".into()),
}
}
#[test]
fn annotation_serde_round_trip_highlight() {
let ref_id = ReferenceId::new();
let set = AnnotationSet {
reference_id: ref_id,
annotations: vec![Annotation::new(ref_id, 3, make_highlight())],
};
let json = serde_json::to_string(&set).expect("serialize");
let set2: AnnotationSet = serde_json::from_str(&json).expect("deserialize");
assert_eq!(set.reference_id, set2.reference_id);
assert_eq!(set.annotations.len(), set2.annotations.len());
assert_eq!(set.annotations[0].page, set2.annotations[0].page);
}
#[test]
fn annotation_serde_round_trip_ink() {
let ref_id = ReferenceId::new();
let ink = AnnotationType::Ink {
paths: vec![vec![Point { x: 0.0, y: 0.0 }, Point { x: 10.0, y: 10.0 }]],
color: Color::RED,
width: 2.0,
};
let set = AnnotationSet {
reference_id: ref_id,
annotations: vec![Annotation::new(ref_id, 0, ink)],
};
let json = serde_json::to_string(&set).expect("serialize");
let set2: AnnotationSet = serde_json::from_str(&json).expect("deserialize");
assert_eq!(set, set2);
}
#[test]
fn all_markup_types_serialize() {
let ref_id = ReferenceId::new();
for markup_type in [
TextMarkupType::Highlight,
TextMarkupType::Underline,
TextMarkupType::Squiggly,
TextMarkupType::StrikeOut,
] {
let ann = Annotation::new(
ref_id,
0,
AnnotationType::TextMarkup {
markup_type,
quads: vec![],
color: Color::GREEN,
selected_text: None,
},
);
let set = AnnotationSet {
reference_id: ref_id,
annotations: vec![ann],
};
let json = serde_json::to_string(&set).expect("serialize");
let _: AnnotationSet = serde_json::from_str(&json).expect("deserialize");
}
}
}

67
brittle-model/src/ids.rs Normal file
View File

@@ -0,0 +1,67 @@
use serde::{Deserialize, Serialize};
use std::fmt;
use uuid::Uuid;
macro_rules! define_id {
($name:ident) => {
#[derive(
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
pub struct $name(pub Uuid);
impl $name {
pub fn new() -> Self {
Self(Uuid::now_v7())
}
}
impl Default for $name {
fn default() -> Self {
Self::new()
}
}
impl From<Uuid> for $name {
fn from(uuid: Uuid) -> Self {
Self(uuid)
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
};
}
define_id!(ReferenceId);
define_id!(LibraryId);
define_id!(AnnotationId);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_generates_unique_ids() {
let a = ReferenceId::new();
let b = ReferenceId::new();
assert_ne!(a, b);
}
#[test]
fn display_is_uuid_format() {
let id = ReferenceId::new();
let s = id.to_string();
assert_eq!(s.len(), 36); // UUID hyphenated format
}
#[test]
fn serde_round_trip() {
let id = LibraryId::new();
let json = serde_json::to_string(&id).unwrap();
let id2: LibraryId = serde_json::from_str(&json).unwrap();
assert_eq!(id, id2);
}
}

137
brittle-model/src/lib.rs Normal file
View File

@@ -0,0 +1,137 @@
//! `brittle-model` — shared data types for the Brittle reference manager.
//!
//! This crate contains all model types that cross the IPC boundary between the
//! Tauri backend (`brittle-core`) and the Leptos/WASM frontend. It has no
//! native-only dependencies and compiles to both `wasm32-unknown-unknown` and
//! native targets.
//!
//! Both `brittle-core` and `brittle-ui` depend on this crate.
pub mod annotation;
pub mod ids;
pub mod library;
pub mod reference;
pub mod snapshot;
pub use annotation::{
Annotation, AnnotationSet, AnnotationType, Color, Point, Quad, Rect, TextMarkupType,
};
pub use ids::{AnnotationId, LibraryId, ReferenceId};
pub use library::Library;
pub use reference::{EntryType, PdfAttachment, Person, Reference};
pub use snapshot::Snapshot;
use serde::Serialize;
/// A lightweight summary of a reference, suitable for list views.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, serde::Deserialize)]
pub struct ReferenceSummary {
pub id: ReferenceId,
pub cite_key: String,
pub entry_type: EntryType,
pub title: Option<String>,
pub authors: Vec<Person>,
pub year: Option<String>,
}
impl ReferenceSummary {
/// Title or "[no title]" fallback.
pub fn title_display(&self) -> &str {
self.title.as_deref().unwrap_or("[no title]")
}
/// Compact author string: "Family", "A & B", or "A et al."
pub fn author_display(&self) -> String {
match self.authors.len() {
0 => "".into(),
1 => self.authors[0].family.clone(),
2 => format!("{} & {}", self.authors[0].family, self.authors[1].family),
_ => format!("{} et al.", self.authors[0].family),
}
}
}
impl From<&Reference> for ReferenceSummary {
fn from(r: &Reference) -> Self {
Self {
id: r.id,
cite_key: r.cite_key.clone(),
entry_type: r.entry_type.clone(),
title: r.title().map(str::to_owned),
authors: r.authors.clone(),
year: r.year().map(str::to_owned),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn p(family: &str) -> Person {
Person { family: family.into(), given: None, prefix: None, suffix: None }
}
fn base_summary() -> ReferenceSummary {
ReferenceSummary {
id: ReferenceId::new(),
cite_key: "x".into(),
entry_type: EntryType::Article,
title: None,
authors: vec![],
year: None,
}
}
#[test]
fn title_display_fallback() {
let rs = base_summary();
assert_eq!(rs.title_display(), "[no title]");
}
#[test]
fn title_display_with_title() {
let rs = ReferenceSummary { title: Some("My Paper".into()), ..base_summary() };
assert_eq!(rs.title_display(), "My Paper");
}
#[test]
fn author_display_empty() {
assert_eq!(base_summary().author_display(), "");
}
#[test]
fn author_display_one() {
let rs = ReferenceSummary { authors: vec![p("Smith")], ..base_summary() };
assert_eq!(rs.author_display(), "Smith");
}
#[test]
fn author_display_two() {
let rs = ReferenceSummary { authors: vec![p("Smith"), p("Jones")], ..base_summary() };
assert_eq!(rs.author_display(), "Smith & Jones");
}
#[test]
fn author_display_many() {
let rs = ReferenceSummary {
authors: vec![p("Smith"), p("Jones"), p("Brown")],
..base_summary()
};
assert_eq!(rs.author_display(), "Smith et al.");
}
#[test]
fn summary_from_reference() {
let mut r = Reference::new("einstein1905", EntryType::Article);
r.fields.insert("title".into(), "On the Electrodynamics of Moving Bodies".into());
r.fields.insert("year".into(), "1905".into());
r.authors.push(Person::new("Einstein"));
let s = ReferenceSummary::from(&r);
assert_eq!(s.cite_key, "einstein1905");
assert_eq!(s.title_display(), "On the Electrodynamics of Moving Bodies");
assert_eq!(s.author_display(), "Einstein");
assert_eq!(s.year.as_deref(), Some("1905"));
}
}

View File

@@ -0,0 +1,66 @@
use crate::ids::{LibraryId, ReferenceId};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;
/// A named collection of references. Forms a tree via `parent_id`.
///
/// References are not "owned" by a library — they exist in a flat pool.
/// A reference can appear in multiple libraries (multi-membership).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Library {
pub id: LibraryId,
pub name: String,
/// `None` means this is a root library (no parent).
pub parent_id: Option<LibraryId>,
/// The set of references that are members of this library.
/// BTreeSet for deterministic serialization order.
pub members: BTreeSet<ReferenceId>,
pub created_at: DateTime<Utc>,
pub modified_at: DateTime<Utc>,
}
impl Library {
pub fn new(name: impl Into<String>, parent_id: Option<LibraryId>) -> Self {
let now = Utc::now();
Self {
id: LibraryId::new(),
name: name.into(),
parent_id,
members: BTreeSet::new(),
created_at: now,
modified_at: now,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn library_serde_round_trip() {
let mut lib = Library::new("Machine Learning", None);
let ref_id = ReferenceId::new();
lib.members.insert(ref_id);
let json = serde_json::to_string(&lib).expect("serialize to JSON");
let lib2: Library = serde_json::from_str(&json).expect("deserialize from JSON");
assert_eq!(lib.id, lib2.id);
assert_eq!(lib.name, lib2.name);
assert_eq!(lib.members, lib2.members);
assert_eq!(lib.parent_id, lib2.parent_id);
}
#[test]
fn nested_library_serde_round_trip() {
let parent = Library::new("Science", None);
let child = Library::new("Physics", Some(parent.id));
let json = serde_json::to_string(&child).expect("serialize to JSON");
let child2: Library = serde_json::from_str(&json).expect("deserialize from JSON");
assert_eq!(child2.parent_id, Some(parent.id));
}
}

View File

@@ -0,0 +1,275 @@
use crate::ids::ReferenceId;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fmt;
use std::path::PathBuf;
/// A person (author, editor, translator, etc.).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Person {
pub family: String,
pub given: Option<String>,
/// Name prefix: "von", "de", "van der", etc.
pub prefix: Option<String>,
/// Name suffix: "Jr.", "III", etc.
pub suffix: Option<String>,
}
impl Person {
pub fn new(family: impl Into<String>) -> Self {
Self {
family: family.into(),
given: None,
prefix: None,
suffix: None,
}
}
/// Format for display: "Given prefix Family, Suffix" — natural reading order.
pub fn display_name(&self) -> String {
let mut parts = Vec::new();
if let Some(given) = &self.given {
parts.push(given.as_str());
}
if let Some(prefix) = &self.prefix {
parts.push(prefix.as_str());
}
parts.push(self.family.as_str());
let mut name = parts.join(" ");
if let Some(suffix) = &self.suffix {
name.push_str(", ");
name.push_str(suffix);
}
name
}
/// Format as BibTeX expects: "{prefix} {family}, {suffix}, {given}".
/// Falls back gracefully when optional parts are absent.
pub fn to_bibtex(&self) -> String {
let mut family_part = String::new();
if let Some(prefix) = &self.prefix {
family_part.push_str(prefix);
family_part.push(' ');
}
family_part.push_str(&self.family);
match (&self.suffix, &self.given) {
(Some(suffix), Some(given)) => {
format!("{family_part}, {suffix}, {given}")
}
(Some(suffix), None) => format!("{family_part}, {suffix}"),
(None, Some(given)) => format!("{family_part}, {given}"),
(None, None) => family_part,
}
}
}
impl fmt::Display for Person {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.display_name())
}
}
/// Standard BibTeX and common BibLaTeX entry types.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum EntryType {
Article,
Book,
Booklet,
InBook,
InCollection,
InProceedings,
Manual,
MastersThesis,
Misc,
PhdThesis,
Proceedings,
TechReport,
Unpublished,
Online,
}
impl EntryType {
/// The BibTeX entry type name as it appears in `.bib` files.
pub fn bibtex_name(&self) -> &'static str {
match self {
EntryType::Article => "article",
EntryType::Book => "book",
EntryType::Booklet => "booklet",
EntryType::InBook => "inbook",
EntryType::InCollection => "incollection",
EntryType::InProceedings => "inproceedings",
EntryType::Manual => "manual",
EntryType::MastersThesis => "mastersthesis",
EntryType::Misc => "misc",
EntryType::PhdThesis => "phdthesis",
EntryType::Proceedings => "proceedings",
EntryType::TechReport => "techreport",
EntryType::Unpublished => "unpublished",
EntryType::Online => "online",
}
}
}
impl fmt::Display for EntryType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.bibtex_name())
}
}
/// A PDF file stored inside the Brittle repository.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PdfAttachment {
/// Path relative to the repository root (e.g., `"pdfs/550e8400-....pdf"`).
pub stored_path: PathBuf,
/// SHA-256 hex digest of the file contents for integrity verification.
pub content_hash: String,
}
/// A citable work. The core entity of Brittle.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Reference {
pub id: ReferenceId,
/// The BibTeX cite key (e.g., `"knuth1984texbook"`). User-facing and mutable.
pub cite_key: String,
pub entry_type: EntryType,
/// Authors listed in order.
pub authors: Vec<Person>,
/// Editors (for edited books, proceedings, etc.).
pub editors: Vec<Person>,
/// All other fields (title, year, journal, volume, etc.) as plain strings.
/// BTreeMap for deterministic serialization order (important for git diffs).
pub fields: BTreeMap<String, String>,
pub pdf: Option<PdfAttachment>,
pub created_at: DateTime<Utc>,
pub modified_at: DateTime<Utc>,
}
impl Reference {
pub fn new(cite_key: impl Into<String>, entry_type: EntryType) -> Self {
let now = Utc::now();
Self {
id: ReferenceId::new(),
cite_key: cite_key.into(),
entry_type,
authors: Vec::new(),
editors: Vec::new(),
fields: BTreeMap::new(),
pdf: None,
created_at: now,
modified_at: now,
}
}
/// Returns the value of the `title` field, if present.
pub fn title(&self) -> Option<&str> {
self.fields.get("title").map(String::as_str)
}
/// Returns the value of the `year` field, if present.
pub fn year(&self) -> Option<&str> {
self.fields.get("year").map(String::as_str)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn person_bibtex_full() {
let p = Person {
family: "Dijkstra".into(),
given: Some("Edsger W.".into()),
prefix: None,
suffix: None,
};
assert_eq!(p.to_bibtex(), "Dijkstra, Edsger W.");
}
#[test]
fn person_bibtex_with_prefix() {
let p = Person {
family: "Beethoven".into(),
given: Some("Ludwig".into()),
prefix: Some("van".into()),
suffix: None,
};
assert_eq!(p.to_bibtex(), "van Beethoven, Ludwig");
}
#[test]
fn person_bibtex_with_suffix() {
let p = Person {
family: "King".into(),
given: Some("Martin Luther".into()),
prefix: None,
suffix: Some("Jr.".into()),
};
assert_eq!(p.to_bibtex(), "King, Jr., Martin Luther");
}
#[test]
fn person_bibtex_family_only() {
let p = Person::new("Aristotle");
assert_eq!(p.to_bibtex(), "Aristotle");
}
#[test]
fn person_display_name_full() {
let p = Person {
family: "Turing".into(),
given: Some("Alan".into()),
prefix: None,
suffix: None,
};
assert_eq!(p.display_name(), "Alan Turing");
}
#[test]
fn person_display_name_with_prefix_and_suffix() {
let p = Person {
family: "King".into(),
given: Some("Martin Luther".into()),
prefix: None,
suffix: Some("Jr.".into()),
};
assert_eq!(p.display_name(), "Martin Luther King, Jr.");
}
#[test]
fn reference_serde_round_trip() {
let mut r = Reference::new("doe2024", EntryType::Article);
r.authors.push(Person {
family: "Doe".into(),
given: Some("Jane".into()),
prefix: None,
suffix: None,
});
r.fields.insert("title".into(), "A Great Paper".into());
r.fields.insert("year".into(), "2024".into());
let json = serde_json::to_string(&r).expect("serialize to JSON");
let r2: Reference = serde_json::from_str(&json).expect("deserialize from JSON");
assert_eq!(r.id, r2.id);
assert_eq!(r.cite_key, r2.cite_key);
assert_eq!(r.authors, r2.authors);
assert_eq!(r.fields, r2.fields);
}
#[test]
fn entry_type_bibtex_names() {
assert_eq!(EntryType::Article.bibtex_name(), "article");
assert_eq!(EntryType::InProceedings.bibtex_name(), "inproceedings");
assert_eq!(EntryType::PhdThesis.bibtex_name(), "phdthesis");
}
#[test]
fn entry_type_display() {
assert_eq!(EntryType::Article.to_string(), "article");
assert_eq!(EntryType::InProceedings.to_string(), "inproceedings");
}
}

View File

@@ -0,0 +1,12 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
/// Metadata about a stored snapshot (git commit).
/// Not serialized to files — read directly from git history.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Snapshot {
/// Git commit SHA (hex string).
pub id: String,
pub message: String,
pub timestamp: DateTime<Utc>,
}