Skip to main content

pactum_annotations/
schema.rs

1use serde::{Deserialize, Serialize};
2
3fn is_false(value: &bool) -> bool {
4    !*value
5}
6
7/// Static description of a model for the contract catalog and IDL.
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9pub struct ModelSchema {
10    pub name: String,
11    pub kind: ModelKind,
12    #[serde(skip_serializing_if = "Option::is_none", default)]
13    pub tag: Option<String>,
14    #[serde(skip_serializing_if = "Option::is_none", default)]
15    pub content: Option<String>,
16    #[serde(default, skip_serializing_if = "is_false")]
17    pub untagged: bool,
18    #[serde(skip_serializing_if = "Option::is_none", default)]
19    pub ts_type: Option<String>,
20    pub fields: Vec<FieldSchema>,
21    pub invariants: Vec<InvariantSpec>,
22    /// For non-command enums: variant names and fields.
23    #[serde(skip_serializing_if = "Vec::is_empty", default)]
24    pub variants: Vec<VariantSchema>,
25    /// For command enums: one entry per variant.
26    pub commands: Vec<CommandSpec>,
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
30#[serde(rename_all = "camelCase")]
31pub enum ModelKind {
32    Struct,
33    Enum,
34    CommandEnum,
35}
36
37/// One field on a struct or enum variant.
38#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
39pub struct FieldSchema {
40    pub name: String,
41    pub type_name: String,
42    pub optional: bool,
43    #[serde(skip_serializing_if = "Option::is_none", default)]
44    pub ts_type: Option<String>,
45    pub normalizers: Vec<String>,
46    pub invariants: Vec<InvariantSpec>,
47    #[serde(skip_serializing_if = "Option::is_none", default)]
48    pub items: Option<Box<FieldSchema>>,
49    #[serde(skip_serializing_if = "Option::is_none", default)]
50    pub map_keys: Option<Box<FieldSchema>>,
51    #[serde(skip_serializing_if = "Option::is_none", default)]
52    pub map_values: Option<Box<FieldSchema>>,
53}
54
55/// One variant on a non-command enum (events, server frames, replies).
56#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
57pub struct VariantSchema {
58    pub name: String,
59    pub fields: Vec<FieldSchema>,
60    #[serde(default, skip_serializing_if = "is_false")]
61    pub tuple: bool,
62    #[serde(skip_serializing_if = "Vec::is_empty", default)]
63    pub invariants: Vec<InvariantSpec>,
64}
65
66/// One check or constraint the host may discover. Enforcement still runs in Rust.
67#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
68pub struct InvariantSpec {
69    pub code: String,
70    pub field: Option<String>,
71    pub kind: String,
72    pub detail: String,
73    #[serde(skip_serializing_if = "Option::is_none", default)]
74    pub scope: Option<String>,
75}
76
77/// Discoverable command: name, reply type, fields, and invariants.
78#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
79pub struct CommandSpec {
80    /// Wire / catalog name (`sendSomeEmoji`).
81    pub name: String,
82    /// Rust reply type name (`EmojiAck`).
83    pub reply: String,
84    pub fields: Vec<FieldSchema>,
85    pub invariants: Vec<InvariantSpec>,
86}
87
88impl ModelSchema {
89    pub fn struct_model(name: impl Into<String>, fields: Vec<FieldSchema>) -> Self {
90        let invariants = fields
91            .iter()
92            .flat_map(|f| f.invariants.iter().cloned())
93            .collect();
94        Self {
95            name: name.into(),
96            kind: ModelKind::Struct,
97            tag: None,
98            content: None,
99            untagged: false,
100            ts_type: None,
101            fields,
102            invariants,
103            variants: Vec::new(),
104            commands: Vec::new(),
105        }
106    }
107
108    pub fn command_enum(name: impl Into<String>, commands: Vec<CommandSpec>) -> Self {
109        let invariants = commands
110            .iter()
111            .flat_map(|c| c.invariants.iter().cloned())
112            .collect();
113        Self {
114            name: name.into(),
115            kind: ModelKind::CommandEnum,
116            tag: None,
117            content: None,
118            untagged: false,
119            ts_type: None,
120            fields: Vec::new(),
121            invariants,
122            variants: Vec::new(),
123            commands,
124        }
125    }
126}