Skip to main content

remodel_core/format/
mod.rs

1//! On-disk serialization helpers.
2//!
3//! The `.remodel` file format is a JSON document containing a writer tag, a
4//! conceptual model, and (optionally) a cached logical model. This module
5//! exposes thin helpers around `serde_json` so callers don't have to assemble
6//! the envelope by hand.
7
8use serde::{Deserialize, Serialize};
9
10use crate::error::Result;
11use crate::models::conceptual::ConceptualModel;
12use crate::models::logical::LogicalModel;
13
14/// On-disk envelope for a `.remodel` file.
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct RemodelFile {
17    /// Writer tag, typically `"remodel-core/<version>"`. Useful for migrations.
18    pub writer: String,
19    /// The conceptual (ER) model — the source of truth.
20    pub conceptual: ConceptualModel,
21    /// Cached logical model, if any. May be regenerated from `conceptual`.
22    #[serde(skip_serializing_if = "Option::is_none", default)]
23    pub logical: Option<LogicalModel>,
24}
25
26impl RemodelFile {
27    /// Wrap the given conceptual model in a fresh envelope tagged with the
28    /// current crate version.
29    pub fn new(conceptual: ConceptualModel) -> Self {
30        Self {
31            writer: format!("remodel-core/{}", crate::VERSION),
32            conceptual,
33            logical: None,
34        }
35    }
36
37    /// Serialize to a pretty JSON string.
38    pub fn to_json(&self) -> Result<String> {
39        serde_json::to_string_pretty(self)
40            .map_err(|e| crate::Error::Internal(format!("serialize: {e}")))
41    }
42
43    /// Parse from a JSON string.
44    pub fn from_json(s: &str) -> Result<Self> {
45        serde_json::from_str(s).map_err(|e| crate::Error::Internal(format!("parse: {e}")))
46    }
47}