From 1696d665f01c6e8bff04946c69ad0258bf72b5eb Mon Sep 17 00:00:00 2001 From: Josh Kingsley Date: Sat, 29 Nov 2025 22:16:44 +0200 Subject: feat: export proper types from doc --- packages/doc/src/doc.rs | 67 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 17 deletions(-) (limited to 'packages/doc/src/doc.rs') 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(deserializer: D) -> Result + 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, + pub grids: Vec, } -#[derive(Serialize)] +#[derive(Serialize, Deserialize, Tsify)] +#[tsify(into_wasm_abi)] pub struct Grid { - pub(crate) id: DerivedId, - pub(crate) rows: Vec, + pub id: DerivedId, + pub rows: Vec, } -#[derive(Serialize)] +#[derive(Serialize, Deserialize, Tsify)] +#[tsify(into_wasm_abi)] pub struct Row { - pub(crate) id: DerivedId, - pub(crate) cells: Vec, + pub id: DerivedId, + pub cells: Vec, } -#[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!() } -- cgit v1.2.3