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    pub normalizers: Vec<String>,
44    pub invariants: Vec<InvariantSpec>,
45    #[serde(skip_serializing_if = "Option::is_none", default)]
46    pub items: Option<Box<FieldSchema>>,
47    #[serde(skip_serializing_if = "Option::is_none", default)]
48    pub map_keys: Option<Box<FieldSchema>>,
49    #[serde(skip_serializing_if = "Option::is_none", default)]
50    pub map_values: Option<Box<FieldSchema>>,
51}
52
53/// One variant on a non-command enum (events, server frames, replies).
54#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
55pub struct VariantSchema {
56    pub name: String,
57    pub fields: Vec<FieldSchema>,
58    #[serde(default, skip_serializing_if = "is_false")]
59    pub tuple: bool,
60    #[serde(skip_serializing_if = "Vec::is_empty", default)]
61    pub invariants: Vec<InvariantSpec>,
62}
63
64/// One check or constraint the host may discover. Enforcement still runs in Rust.
65#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
66pub struct InvariantSpec {
67    pub code: String,
68    pub field: Option<String>,
69    pub kind: String,
70    pub detail: String,
71    #[serde(skip_serializing_if = "Option::is_none", default)]
72    pub scope: Option<String>,
73}
74
75/// Discoverable command: name, reply type, fields, and invariants.
76#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
77pub struct CommandSpec {
78    /// Wire / catalog name (`sendSomeEmoji`).
79    pub name: String,
80    /// Rust reply type name (`EmojiAck`).
81    pub reply: String,
82    pub fields: Vec<FieldSchema>,
83    pub invariants: Vec<InvariantSpec>,
84}
85
86impl ModelSchema {
87    pub fn struct_model(name: impl Into<String>, fields: Vec<FieldSchema>) -> Self {
88        let invariants = fields
89            .iter()
90            .flat_map(|f| f.invariants.iter().cloned())
91            .collect();
92        Self {
93            name: name.into(),
94            kind: ModelKind::Struct,
95            tag: None,
96            content: None,
97            untagged: false,
98            ts_type: None,
99            fields,
100            invariants,
101            variants: Vec::new(),
102            commands: Vec::new(),
103        }
104    }
105
106    pub fn command_enum(name: impl Into<String>, commands: Vec<CommandSpec>) -> Self {
107        let invariants = commands
108            .iter()
109            .flat_map(|c| c.invariants.iter().cloned())
110            .collect();
111        Self {
112            name: name.into(),
113            kind: ModelKind::CommandEnum,
114            tag: None,
115            content: None,
116            untagged: false,
117            ts_type: None,
118            fields: Vec::new(),
119            invariants,
120            variants: Vec::new(),
121            commands,
122        }
123    }
124}