catcolab_document_types/v2/
document.rs

1use crate::v0::AnalysisType;
2use crate::v1;
3pub use crate::v1::DocumentType;
4
5use super::analysis::Analysis;
6use super::api::Link;
7use super::notebook::Notebook;
8
9use serde::{Deserialize, Serialize};
10use tsify::Tsify;
11
12/// This is the content of a model document. For legacy reasons, we reserve
13/// the name "ModelDocument" for `Document & { type: "model" }`.
14#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Tsify)]
15#[tsify(into_wasm_abi, from_wasm_abi)]
16pub struct ModelDocumentContent {
17    pub name: String,
18    pub theory: String,
19    #[serde(
20        default,
21        skip_serializing_if = "Option::is_none",
22        rename = "editorVariant"
23    )]
24    pub editor_variant: Option<String>,
25    pub notebook: Notebook<super::model_judgment::ModelJudgment>,
26    pub version: String,
27}
28
29#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Tsify)]
30#[tsify(into_wasm_abi, from_wasm_abi)]
31pub struct DiagramDocumentContent {
32    pub name: String,
33    #[serde(rename = "diagramIn")]
34    pub diagram_in: Link,
35    pub notebook: Notebook<super::diagram_judgment::DiagramJudgment>,
36    pub version: String,
37}
38
39/// This is the content of an instance document, presenting an instance of the
40/// model that the document's `instanceOf` link points to.
41///
42/// Not yet a variant of [`Document`]: wiring into the document enum (and the
43/// frontend) is deferred until instance notebooks elaborate.
44#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Tsify)]
45#[tsify(into_wasm_abi, from_wasm_abi)]
46pub struct InstanceDocumentContent {
47    pub name: String,
48    #[serde(rename = "instanceOf")]
49    pub instance_of: Link,
50    pub notebook: Notebook<super::instance_judgment::InstanceJudgment>,
51    pub version: String,
52}
53
54#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Tsify)]
55#[tsify(into_wasm_abi, from_wasm_abi)]
56pub struct AnalysisDocumentContent {
57    pub name: String,
58    #[serde(rename = "analysisType")]
59    pub analysis_type: AnalysisType,
60    #[serde(rename = "analysisOf")]
61    pub analysis_of: Link,
62    pub notebook: Notebook<Analysis>,
63    pub version: String,
64}
65
66#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Tsify)]
67#[serde(tag = "type")]
68#[tsify(into_wasm_abi, from_wasm_abi)]
69pub enum Document {
70    #[serde(rename = "model")]
71    Model(ModelDocumentContent),
72    #[serde(rename = "diagram")]
73    Diagram(DiagramDocumentContent),
74    #[serde(rename = "analysis")]
75    Analysis(AnalysisDocumentContent),
76}
77
78impl Document {
79    pub fn migrate_from_v1(old: v1::Document) -> Self {
80        match old {
81            v1::Document::Model(old) => Document::Model(ModelDocumentContent {
82                name: old.name,
83                theory: old.theory,
84                editor_variant: old.editor_variant,
85                notebook: Notebook::migrate_from_v1(old.notebook),
86                version: "2".to_string(),
87            }),
88
89            v1::Document::Diagram(old) => Document::Diagram(DiagramDocumentContent {
90                name: old.name,
91                diagram_in: old.diagram_in,
92                notebook: Notebook::migrate_from_v1(old.notebook),
93                version: "2".to_string(),
94            }),
95
96            v1::Document::Analysis(old) => Document::Analysis(AnalysisDocumentContent {
97                name: old.name,
98                analysis_type: old.analysis_type,
99                analysis_of: old.analysis_of,
100                notebook: Notebook::migrate_from_v1(old.notebook),
101                version: "2".to_string(),
102            }),
103        }
104    }
105}