Skip to main content

pactum_annotations/
schema.rs

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