Files
brittle/brittle-model/src/lib.rs

138 lines
4.1 KiB
Rust

//! `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"));
}
}