Add local document JSON state commands
This commit is contained in:
parent
b102e07204
commit
e039a4f81f
11 changed files with 153 additions and 10 deletions
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue