Skip to main content

soothfast_sdk/
model.rs

1//! The language-neutral SDK model every emitter renders from.
2
3/// A wire type, lowered from JSON Schema.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum Ty {
6    Str,
7    Int,
8    Float,
9    Bool,
10    /// An open schema — a gap, `serde_json::Value`, or anything else the
11    /// source could not pin down.
12    Any,
13    /// No body at all.
14    Unit,
15    List(Box<Ty>),
16    /// String-keyed map with uniform values.
17    Map(Box<Ty>),
18    Optional(Box<Ty>),
19    /// A named component model.
20    Named(String),
21    /// `oneOf` arms, matched structurally when decoding.
22    Union(Vec<Ty>),
23    /// A closed set of string values.
24    Literal(Vec<String>),
25}
26
27/// One field of a [`Model`].
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub struct Field {
30    /// Attribute name in the target language.
31    pub attr: String,
32    /// Property name on the wire.
33    pub wire: String,
34    pub ty: Ty,
35    pub required: bool,
36    pub doc: Option<String>,
37}
38
39/// A named object schema, emitted as a class/interface.
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub struct Model {
42    pub name: String,
43    pub doc: Option<String>,
44    pub fields: Vec<Field>,
45}
46
47/// Where a request parameter travels.
48#[derive(Debug, Clone, Copy, PartialEq, Eq)]
49pub enum ParamLoc {
50    Query,
51    Path,
52    Header,
53}
54
55/// One parameter of a [`Method`].
56#[derive(Debug, Clone, PartialEq, Eq)]
57pub struct Param {
58    /// Argument name in the target language.
59    pub attr: String,
60    /// Parameter name on the wire.
61    pub wire: String,
62    pub location: ParamLoc,
63    pub ty: Ty,
64    pub required: bool,
65    pub doc: Option<String>,
66}
67
68/// One client method, lowered from an operation.
69#[derive(Debug, Clone, PartialEq, Eq)]
70pub struct Method {
71    /// Method name in the target language.
72    pub name: String,
73    pub operation_id: String,
74    pub http_method: String,
75    /// Route template with `{placeholder}` segments.
76    pub path: String,
77    pub doc: Option<String>,
78    pub params: Vec<Param>,
79    /// Request body type, when the operation takes one.
80    pub body: Option<Ty>,
81    /// Success response type.
82    pub ok: Ty,
83    pub ok_status: u16,
84    /// Whether the response switches to a page envelope when the
85    /// pagination parameters are sent.
86    pub paginated: bool,
87}
88
89/// The whole lowered SDK.
90#[derive(Debug, Clone, Default)]
91pub struct Sdk {
92    /// Component models, sorted by name.
93    pub models: Vec<Model>,
94    /// Non-object components, as named aliases sorted by name.
95    pub aliases: Vec<(String, Ty)>,
96    /// Methods, sorted by operation id.
97    pub methods: Vec<Method>,
98    /// What lowering could not express precisely, deduplicated.
99    pub notes: Vec<String>,
100}