Add local document JSON state commands

This commit is contained in:
Eric Wendland 2026-05-17 19:59:03 +02:00
commit e039a4f81f
11 changed files with 153 additions and 10 deletions

View file

@ -7,5 +7,6 @@ license.workspace = true
[dependencies]
serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true
geth-types = { path = "../geth-types" }

View file

@ -1,4 +1,4 @@
use geth_types::{DocumentId, ResourceId};
use geth_types::{DocumentId, ResourceId, UnixMillis};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
@ -10,10 +10,19 @@ pub struct DocumentResource {
pub state_bytes: u64,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DocumentState {
pub document: DocumentResource,
pub state_json: String,
pub updated_at: UnixMillis,
}
#[derive(Debug, thiserror::Error)]
pub enum DocumentError {
#[error("invalid document name: {0}")]
InvalidName(String),
#[error("invalid document JSON state: {0}")]
InvalidState(#[from] serde_json::Error),
}
pub fn validate_document_name(name: &str) -> Result<(), DocumentError> {
@ -27,6 +36,11 @@ pub fn validate_document_name(name: &str) -> Result<(), DocumentError> {
Ok(())
}
pub fn normalize_document_state(state_json: &str) -> Result<String, DocumentError> {
let value: serde_json::Value = serde_json::from_str(state_json)?;
Ok(serde_json::to_string(&value)?)
}
#[must_use]
pub fn automerge_roadmap() -> &'static str {
"future documents use Automerge sync over Iroh with resource-local authorization"
@ -45,4 +59,13 @@ mod tests {
assert!(validate_document_name("notes/main").is_err());
assert!(validate_document_name("notes main").is_err());
}
#[test]
fn document_state_is_validated_and_normalized_json() {
assert_eq!(
normalize_document_state(r#"{ "title": "notes", "done": false }"#).expect("normalize"),
r#"{"done":false,"title":"notes"}"#
);
assert!(normalize_document_state("{").is_err());
}
}