Skip to main content

tsurara_backend/
manifest.rs

1//! What a backend generated, by name: for each declaration of the model and each
2//! operation, the identifiers the backend chose and the file that declares them. Plugins of the
3//! `extend` stage read it to write files that refer to generated code without re-deriving its names.
4
5use std::collections::BTreeMap;
6use tsurara_sem::naming::NameId;
7
8/// Version of the manifest's JSON form. It changes only when the form changes incompatibly.
9pub const MANIFEST_VERSION: u32 = 1;
10
11#[derive(Debug, Clone, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
12#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
13#[serde(rename_all = "camelCase")]
14pub struct Manifest {
15    /// `MANIFEST_VERSION`.
16    pub manifest_version: u32,
17    /// The backend's `id`.
18    pub language: String,
19    /// The declarations of the model (`Model::schemas`) the backend emitted, in model order.
20    /// Declarations the backend adds on its own (hoisted types) are not listed.
21    pub types: Vec<TypeEntry>,
22    /// The operations, then the webhooks, in model order.
23    pub operations: Vec<OperationEntry>,
24}
25
26impl Manifest {
27    pub fn new(language: &str) -> Self {
28        Self {
29            manifest_version: MANIFEST_VERSION,
30            language: language.to_owned(),
31            types: Vec::new(),
32            operations: Vec::new(),
33        }
34    }
35}
36
37/// An identifier and the file that declares it.
38#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
39#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
40#[serde(rename_all = "camelCase")]
41pub struct Export {
42    pub identifier: String,
43    /// Path of the file relative to the output root (`/`-separated), as in `OutputFile::path`.
44    pub file: String,
45}
46
47impl Export {
48    pub fn new(identifier: impl Into<String>, file: impl Into<String>) -> Self {
49        Self {
50            identifier: identifier.into(),
51            file: file.into(),
52        }
53    }
54}
55
56#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
57#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
58#[serde(rename_all = "camelCase")]
59pub struct TypeEntry {
60    /// The declaration's `TypeDecl::id`.
61    pub name: NameId,
62    #[serde(flatten)]
63    pub export: Export,
64}
65
66#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
67#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
68#[serde(rename_all = "camelCase")]
69pub enum OperationKind {
70    Operation,
71    Webhook,
72}
73
74#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
75#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
76#[serde(rename_all = "camelCase")]
77pub struct OperationEntry {
78    /// `Operation::id`.
79    pub operation_id: String,
80    pub kind: OperationKind,
81    /// The call function, if the backend emits one.
82    #[serde(default, skip_serializing_if = "Option::is_none")]
83    pub function: Option<Export>,
84    /// The OperationDef value, if the backend emits one.
85    #[serde(default, skip_serializing_if = "Option::is_none")]
86    pub operation_def: Option<Export>,
87    /// Types derived from the operation, by role: `data`, `result`, `responses`, `errors`, `options`,
88    /// `event`, `item`, `request`, ... Each language lists the roles it has (docs/guide/plugins.md).
89    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
90    pub types: BTreeMap<String, Export>,
91    /// Other values generated for the operation, by role: with TypeScript's
92    /// `query: tanstack`, `queryKey`, `queryOptions`, `queryFilter`, `setQueriesData` and
93    /// `getQueriesData` (reads), `infiniteQueryKey` and `infiniteQueryOptions` (paginated reads) and
94    /// `mutationOptions` (writes); with `mocks`, `mockHandler`.
95    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
96    pub helpers: BTreeMap<String, Export>,
97}
98
99/// `entries` sorted in the order of `model.schemas`, for backends whose declarations come in another
100/// order. Entries of names the model does not declare go last, in their order.
101pub fn in_model_order(model: &tsurara_sem::Model, mut entries: Vec<TypeEntry>) -> Vec<TypeEntry> {
102    let index = |id: NameId| {
103        model
104            .schemas
105            .iter()
106            .position(|d| d.id == id)
107            .unwrap_or(usize::MAX)
108    };
109    entries.sort_by_key(|e| index(e.name));
110    entries
111}