use serde::{Deserialize, Serialize};
use std::{borrow::Cow, fmt};
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub struct Error<'a> {
pub name: &'a str,
pub description: &'a str,
pub declaration: &'a str,
}
impl fmt::Display for Error<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.declaration)
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub struct Event<'a> {
pub name: &'a str,
pub description: &'a str,
pub declaration: &'a str,
}
impl fmt::Display for Event<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.declaration)
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub struct Enum<'a> {
pub name: &'a str,
pub description: &'a str,
#[serde(borrow)]
pub variants: Cow<'a, [EnumVariant<'a>]>,
}
impl fmt::Display for Enum<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "enum {} {{ ", self.name)?;
for (i, variant) in self.variants.iter().enumerate() {
if i > 0 {
f.write_str(", ")?;
}
f.write_str(variant.name)?;
}
f.write_str(" }")
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub struct EnumVariant<'a> {
pub name: &'a str,
pub description: &'a str,
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub struct Struct<'a> {
pub name: &'a str,
pub description: &'a str,
#[serde(borrow)]
pub fields: Cow<'a, [StructField<'a>]>,
}
impl fmt::Display for Struct<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "struct {} {{ ", self.name)?;
for field in self.fields.iter() {
write!(f, "{} {}; ", field.ty, field.name)?;
}
f.write_str("}")
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub struct StructField<'a> {
pub name: &'a str,
pub ty: &'a str,
pub description: &'a str,
}