Skip to main content

appstruct_schema/schema/
snapshot.rs

1use super::{DatabaseSchema, SCHEMA_VERSION};
2
3/// Serialize a canonical schema snapshot.
4///
5/// # Errors
6///
7/// Returns an error if JSON serialization unexpectedly fails.
8pub fn to_json(schema: &DatabaseSchema) -> Result<String, serde_json::Error> {
9    let mut value = serde_json::to_string_pretty(schema)?;
10    value.push('\n');
11    Ok(value)
12}
13
14/// Parse a schema snapshot.
15///
16/// # Errors
17///
18/// Returns an error if the snapshot is invalid or incompatible JSON.
19pub fn from_json(source: &str) -> Result<DatabaseSchema, serde_json::Error> {
20    let schema: DatabaseSchema = serde_json::from_str(source)?;
21    match schema.schema_version {
22        SCHEMA_VERSION => Ok(schema),
23        found => Err(serde_json::Error::io(std::io::Error::new(
24            std::io::ErrorKind::InvalidData,
25            format!("unsupported schema snapshot version {found}; expected {SCHEMA_VERSION}"),
26        ))),
27    }
28}