Files
faunosteo/crates/core/src/domain/sheet/loaded.rs
T
bchanot 22fea8736d feat(domain): Rust sheet index and loaded sheet (bundle payload → document, migration map, positions)
Lookups follow the TypeScript index: by-id maps keep the last duplicate as
new Map does, spine annotations and finding paths take the first match as
Array.find does; a document without a main view cannot be indexed.
2026-10-14 00:05:26 +02:00

107 lines
3.2 KiB
Rust

//! The sheet the application works on for a session (`apps/app/src/sheets/provider.ts`):
//! read once from the bundled payload, then indexed. Its document and migration map pass the
//! strict schemas; the provenance fields come from the build and are taken as they are.
use std::collections::HashMap;
use serde::Deserialize;
use super::index::{IndexedDocument, NoMainView, SheetIndex};
use super::schema::AnatomicalSheetDocument;
use crate::domain::archive::SheetStatus;
use crate::domain::js::JsInt;
use crate::domain::report::{ReportMigrationMap, SheetRef};
/// The payload of `virtual:faunosteo-sheet` (`BundledSheetData`).
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BundledSheetData {
pub status: SheetStatus,
#[serde(rename = "ref")]
pub reference: BundledRef,
pub document: serde_json::Value,
pub migration_map: serde_json::Value,
pub assets: HashMap<String, String>,
pub asset_hashes: HashMap<String, String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct BundledRef {
pub sheet_id: String,
pub version_id: Option<String>,
pub document_sha256: String,
pub schema_version: JsInt,
pub species_id: String,
}
/// A loaded, indexed sheet.
#[derive(Debug, Clone)]
pub struct LoadedSheet {
pub status: SheetStatus,
pub reference: SheetRef,
pub species_id: String,
/// Document schema version of the sheet (`ref.schema_version`).
pub schema_version: JsInt,
pub document: AnatomicalSheetDocument,
/// SVG text by `svg_asset_id`.
pub assets: HashMap<String, String>,
/// sha256 by `svg_asset_id`; carried into an archive as provenance.
pub asset_hashes: HashMap<String, String>,
pub migration_map: ReportMigrationMap,
index: SheetIndex,
}
/// Why a bundled sheet cannot be loaded.
#[derive(Debug)]
pub enum LoadError {
/// The payload, the document or the migration map fails its schema.
Shape(serde_json::Error),
/// The document has no main view.
NoMainView,
}
impl From<serde_json::Error> for LoadError {
fn from(error: serde_json::Error) -> Self {
Self::Shape(error)
}
}
impl From<NoMainView> for LoadError {
fn from(_: NoMainView) -> Self {
Self::NoMainView
}
}
impl LoadedSheet {
/// `parseBundledSheet` then `buildSheetIndex`.
pub fn from_bundle(data: BundledSheetData) -> Result<Self, LoadError> {
let document: AnatomicalSheetDocument = serde_json::from_value(data.document)?;
let migration_map: ReportMigrationMap = serde_json::from_value(data.migration_map)?;
let index = SheetIndex::build(&document)?;
let r = data.reference;
Ok(Self {
status: data.status,
reference: SheetRef {
sheet_id: r.sheet_id,
version_id: r.version_id,
document_sha256: r.document_sha256,
},
species_id: r.species_id,
schema_version: r.schema_version,
document,
assets: data.assets,
asset_hashes: data.asset_hashes,
migration_map,
index,
})
}
pub fn indexed(&self) -> IndexedDocument<'_> {
self.index.over(&self.document)
}
pub fn index(&self) -> &SheetIndex {
&self.index
}
}