#![doc(html_root_url = "https://docs.rs/incipit/0.0.1")]
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
pub mod curation;
#[cfg(feature = "view")]
pub mod view;
#[cfg(feature = "arrangement")]
pub mod forme;
#[cfg(feature = "graph")]
pub mod navigation;
#[cfg(feature = "graph")]
pub mod roster;
#[cfg(feature = "graph")]
pub mod trail;
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const STAGE: &str = "pre-alpha";
pub const SHELFMARK_V1_SCHEMA: &str = "mere.shelfmark/1";
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ShelfmarkAuthorityV1 {
pub adapter: String,
pub record: String,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ShelfmarkInputV1 {
pub authority: ShelfmarkAuthorityV1,
pub reading: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reading_parameters: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub arrangement: Option<String>,
pub expects_generation: String,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ShelfmarkV1 {
pub schema: String,
pub projection: String,
pub inputs: BTreeMap<String, ShelfmarkInputV1>,
#[serde(default)]
pub delta: BTreeMap<String, String>,
}
impl ShelfmarkV1 {
pub fn new(projection: impl Into<String>) -> Self {
Self {
schema: SHELFMARK_V1_SCHEMA.to_owned(),
projection: projection.into(),
inputs: BTreeMap::new(),
delta: BTreeMap::new(),
}
}
pub fn validate(&self) -> Result<(), ShelfmarkError> {
if self.schema != SHELFMARK_V1_SCHEMA {
return Err(ShelfmarkError::UnsupportedSchema(self.schema.clone()));
}
if self.projection.trim().is_empty() {
return Err(ShelfmarkError::MissingProjection);
}
if self.inputs.is_empty() {
return Err(ShelfmarkError::MissingInputs);
}
for (role, input) in &self.inputs {
if role.trim().is_empty()
|| input.authority.adapter.trim().is_empty()
|| input.authority.record.trim().is_empty()
|| input.reading.trim().is_empty()
|| input
.reading_parameters
.as_ref()
.is_some_and(|parameters| parameters.trim().is_empty())
|| input.expects_generation.trim().is_empty()
{
return Err(ShelfmarkError::InvalidInput(role.clone()));
}
}
if let Some(section) = self.delta.keys().find(|name| name.trim().is_empty()) {
return Err(ShelfmarkError::InvalidDeltaSection(section.clone()));
}
Ok(())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ShelfmarkError {
UnsupportedSchema(String),
MissingProjection,
MissingInputs,
InvalidInput(String),
InvalidDeltaSection(String),
}
pub use catchment::{GraphId, SessionId};
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn composed_shelfmark_keeps_each_input_separately_checkable() {
let mut shelfmark = ShelfmarkV1::new("matrix");
shelfmark.inputs.insert(
"columns".into(),
ShelfmarkInputV1 {
authority: ShelfmarkAuthorityV1 {
adapter: "mer3ly.dataset".into(),
record: "specimen@authored".into(),
},
reading: "changes".into(),
reading_parameters: None,
arrangement: None,
expects_generation: "22".into(),
},
);
shelfmark.inputs.insert(
"rows".into(),
ShelfmarkInputV1 {
authority: ShelfmarkAuthorityV1 {
adapter: "mer3ly.dataset".into(),
record: "live@648bf19".into(),
},
reading: "neighbors".into(),
reading_parameters: Some("{\"focus\":\"mere\"}".into()),
arrangement: None,
expects_generation: "11".into(),
},
);
shelfmark.delta.insert(
"selection".into(),
"{\"resolution\":\"crossfilter\"}".into(),
);
shelfmark.validate().expect("valid composed shelfmark");
let wire = serde_json::to_string(&shelfmark).expect("serialize shelfmark");
assert!(wire.find("columns").unwrap() < wire.find("rows").unwrap());
let far_side: ShelfmarkV1 = serde_json::from_str(&wire).expect("decode shelfmark");
assert_eq!(far_side, shelfmark);
assert_eq!(far_side.inputs["rows"].expects_generation, "11");
assert_eq!(far_side.inputs["columns"].expects_generation, "22");
}
#[test]
fn shelfmark_refuses_an_uncheckable_input() {
let mut shelfmark = ShelfmarkV1::new("matrix");
shelfmark.inputs.insert(
"rows".into(),
ShelfmarkInputV1 {
authority: ShelfmarkAuthorityV1 {
adapter: "mer3ly.dataset".into(),
record: "live".into(),
},
reading: "neighbors".into(),
reading_parameters: None,
arrangement: None,
expects_generation: String::new(),
},
);
assert_eq!(
shelfmark.validate(),
Err(ShelfmarkError::InvalidInput("rows".into()))
);
}
}