use std::collections::BTreeMap;
use tsurara_sem::naming::NameId;
pub const MANIFEST_VERSION: u32 = 1;
#[derive(Debug, Clone, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub struct Manifest {
pub manifest_version: u32,
pub language: String,
pub types: Vec<TypeEntry>,
pub operations: Vec<OperationEntry>,
}
impl Manifest {
pub fn new(language: &str) -> Self {
Self {
manifest_version: MANIFEST_VERSION,
language: language.to_owned(),
types: Vec::new(),
operations: Vec::new(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub struct Export {
pub identifier: String,
pub file: String,
}
impl Export {
pub fn new(identifier: impl Into<String>, file: impl Into<String>) -> Self {
Self {
identifier: identifier.into(),
file: file.into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub struct TypeEntry {
pub name: NameId,
#[serde(flatten)]
pub export: Export,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub enum OperationKind {
Operation,
Webhook,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub struct OperationEntry {
pub operation_id: String,
pub kind: OperationKind,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub function: Option<Export>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub operation_def: Option<Export>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub types: BTreeMap<String, Export>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub helpers: BTreeMap<String, Export>,
}
pub fn in_model_order(model: &tsurara_sem::Model, mut entries: Vec<TypeEntry>) -> Vec<TypeEntry> {
let index = |id: NameId| {
model
.schemas
.iter()
.position(|d| d.id == id)
.unwrap_or(usize::MAX)
};
entries.sort_by_key(|e| index(e.name));
entries
}