diff options
Diffstat (limited to 'packages/doc/src/doc.rs')
| -rw-r--r-- | packages/doc/src/doc.rs | 67 |
1 files changed, 50 insertions, 17 deletions
diff --git a/packages/doc/src/doc.rs b/packages/doc/src/doc.rs index fcca1d8..22ef703 100644 --- a/packages/doc/src/doc.rs +++ b/packages/doc/src/doc.rs @@ -1,14 +1,17 @@ use serde::{Deserialize, Serialize}; use thiserror::Error; +use tsify::Tsify; use uuid::Uuid; use crate::op::{ChangeSubdivisions, CreateGrid, Op, OpKind}; /// An deterministically derived ID, e.g. a grid ID derived from the /// op ID which creates it. +#[derive(Tsify)] +#[tsify(type = "string")] pub struct DerivedId { base: String, - tag: &'static str, + tag: String, index: usize, } @@ -19,24 +22,24 @@ impl ToString for DerivedId { } trait DerivableId { - fn derive_id(&self, tag: &'static str, index: usize) -> DerivedId; + fn derive_id(&self, tag: &str, index: usize) -> DerivedId; } impl DerivableId for Uuid { - fn derive_id(&self, tag: &'static str, index: usize) -> DerivedId { + fn derive_id(&self, tag: &str, index: usize) -> DerivedId { DerivedId { base: self.to_string(), - tag, + tag: tag.to_string(), index, } } } impl DerivableId for DerivedId { - fn derive_id(&self, tag: &'static str, index: usize) -> DerivedId { + fn derive_id(&self, tag: &str, index: usize) -> DerivedId { DerivedId { base: self.to_string(), - tag, + tag: tag.to_string(), index, } } @@ -51,26 +54,56 @@ impl Serialize for DerivedId { } } -#[derive(Default, Serialize)] +impl<'de> Deserialize<'de> for DerivedId { + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> + where + D: serde::Deserializer<'de>, + { + let s = String::deserialize(deserializer)?; + let parts: Vec<&str> = s.split(':').collect(); + if parts.len() != 2 { + return Err(serde::de::Error::custom("Invalid DerivedId format")); + } + + let base = parts[0].to_string(); + let tag_index: Vec<&str> = parts[1].split('=').collect(); + if tag_index.len() != 2 { + return Err(serde::de::Error::custom("Invalid DerivedId format")); + } + + let tag = tag_index[0].to_string(); + let index = tag_index[1] + .parse() + .map_err(|_| serde::de::Error::custom("Invalid index"))?; + + Ok(DerivedId { base, tag, index }) + } +} + +#[derive(Default, Serialize, Deserialize, Tsify)] +#[tsify(into_wasm_abi)] pub struct Doc { - pub(crate) grids: Vec<Grid>, + pub grids: Vec<Grid>, } -#[derive(Serialize)] +#[derive(Serialize, Deserialize, Tsify)] +#[tsify(into_wasm_abi)] pub struct Grid { - pub(crate) id: DerivedId, - pub(crate) rows: Vec<Row>, + pub id: DerivedId, + pub rows: Vec<Row>, } -#[derive(Serialize)] +#[derive(Serialize, Deserialize, Tsify)] +#[tsify(into_wasm_abi)] pub struct Row { - pub(crate) id: DerivedId, - pub(crate) cells: Vec<Cell>, + pub id: DerivedId, + pub cells: Vec<Cell>, } -#[derive(Serialize)] +#[derive(Serialize, Deserialize, Tsify)] +#[tsify(into_wasm_abi)] pub struct Cell { - pub(crate) id: DerivedId, + pub id: DerivedId, } #[derive(Error, Debug)] @@ -109,6 +142,6 @@ fn apply_create_grid(doc: &mut Doc, op_id: &Uuid, data: &CreateGrid) -> ApplyOpR Ok(()) } -fn apply_change_subdivisions(doc: &mut Doc, data: &ChangeSubdivisions) -> ApplyOpResult { +fn apply_change_subdivisions(_doc: &mut Doc, _data: &ChangeSubdivisions) -> ApplyOpResult { todo!() } |
