catcolab_document_types/v1/
document.rs

1use crate::v0;
2use crate::v0::AnalysisType;
3pub use crate::v0::document::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#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Tsify)]
40#[tsify(into_wasm_abi, from_wasm_abi)]
41pub struct AnalysisDocumentContent {
42    pub name: String,
43    #[serde(rename = "analysisType")]
44    pub analysis_type: AnalysisType,
45    #[serde(rename = "analysisOf")]
46    pub analysis_of: Link,
47    pub notebook: Notebook<Analysis>,
48    pub version: String,
49}
50
51#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Tsify)]
52#[serde(tag = "type")]
53#[tsify(into_wasm_abi, from_wasm_abi)]
54pub enum Document {
55    #[serde(rename = "model")]
56    Model(ModelDocumentContent),
57    #[serde(rename = "diagram")]
58    Diagram(DiagramDocumentContent),
59    #[serde(rename = "analysis")]
60    Analysis(AnalysisDocumentContent),
61}
62
63impl Document {
64    pub fn migrate_from_v0(old: v0::Document) -> Self {
65        match old {
66            v0::Document::Model(old) => Document::Model(ModelDocumentContent {
67                name: old.name,
68                theory: old.theory,
69                editor_variant: None,
70                notebook: Notebook::migrate_from_v0(old.notebook),
71                version: "1".to_string(),
72            }),
73
74            v0::Document::Diagram(old) => Document::Diagram(DiagramDocumentContent {
75                name: old.name,
76                diagram_in: old.diagram_in,
77                notebook: Notebook::migrate_from_v0(old.notebook),
78                version: "1".to_string(),
79            }),
80
81            v0::Document::Analysis(old) => Document::Analysis(AnalysisDocumentContent {
82                name: old.name,
83                analysis_type: old.analysis_type,
84                analysis_of: old.analysis_of,
85                notebook: Notebook::migrate_from_v0(old.notebook),
86                version: "1".to_string(),
87            }),
88        }
89    }
90}