Something went wrong. Try again.
Rust CLI for tangled
Something went wrong. Try again.
4.2 kB · 173 lines
Rust
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174//! Serde model for the subset of the AT Protocol lexicon schema language that//! the codegen understands. Anything it does not understand degrades to//! `serde_json::Value` in the generated code rather than failing.
use std::collections::BTreeMap;
use serde::Deserialize;
#[derive(Debug, Deserialize)]pub struct LexiconDoc { #[allow(dead_code)] pub lexicon: u32, pub id: String, #[serde(default)] pub defs: BTreeMap<String, LexDef>,}
#[derive(Debug, Deserialize)]#[serde(tag = "type", rename_all = "kebab-case")]pub enum LexDef { Record(RecordDef), Query(XrpcDef), Procedure(XrpcDef), Subscription(SubscriptionDef), Object(ObjectDef), Token(TokenDef), /// A def that is a bare non-object type (rare); emitted as a type alias. #[serde(untagged)] Type(LexType),}
#[derive(Debug, Deserialize)]pub struct RecordDef { #[serde(default)] pub description: Option<String>, #[allow(dead_code)] #[serde(default)] pub key: Option<String>, pub record: ObjectDef,}
#[derive(Debug, Deserialize)]pub struct XrpcDef { #[serde(default)] pub description: Option<String>, #[serde(default)] pub parameters: Option<ParamsDef>, #[serde(default)] pub input: Option<BodyDef>, #[serde(default)] pub output: Option<BodyDef>, #[serde(default)] pub errors: Vec<ErrorDef>,}
#[derive(Debug, Deserialize)]pub struct SubscriptionDef { #[serde(default)] pub description: Option<String>,}
#[derive(Debug, Deserialize)]pub struct BodyDef { pub encoding: String, #[serde(default)] pub schema: Option<LexType>,}
#[derive(Debug, Deserialize)]pub struct ErrorDef { pub name: String, #[serde(default)] pub description: Option<String>,}
#[derive(Debug, Deserialize)]pub struct ParamsDef { #[serde(default)] pub description: Option<String>, #[serde(default)] pub required: Vec<String>, #[serde(default)] pub properties: BTreeMap<String, LexType>,}
#[derive(Debug, Deserialize)]pub struct ObjectDef { #[serde(default)] pub description: Option<String>, #[serde(default)] pub required: Vec<String>, #[serde(default)] pub nullable: Vec<String>, #[serde(default)] pub properties: BTreeMap<String, LexType>,}
#[derive(Debug, Deserialize)]pub struct TokenDef { #[serde(default)] pub description: Option<String>,}
#[derive(Debug, Deserialize)]#[serde(tag = "type", rename_all = "kebab-case")]pub enum LexType { String { #[serde(default)] description: Option<String>, #[serde(default)] format: Option<String>, #[serde(default, rename = "knownValues")] known_values: Option<Vec<String>>, }, Integer { #[serde(default)] description: Option<String>, }, Boolean { #[serde(default)] description: Option<String>, }, Array { #[serde(default)] description: Option<String>, items: Box<LexType>, }, Object(ObjectDef), Ref { #[serde(rename = "ref")] target: String, }, Union { #[serde(default)] refs: Vec<String>, }, Blob { #[serde(default)] description: Option<String>, }, Unknown { #[serde(default)] description: Option<String>, }, Bytes { #[serde(default)] description: Option<String>, }, CidLink { #[serde(default)] description: Option<String>, }, /// Catch-all for lexicon types the codegen does not model. #[serde(untagged)] Other(serde_json::Value),}
impl LexType { pub fn description(&self) -> Option<&str> { match self { LexType::String { description, .. } | LexType::Integer { description } | LexType::Boolean { description } | LexType::Array { description, .. } | LexType::Blob { description } | LexType::Unknown { description } | LexType::Bytes { description } | LexType::CidLink { description } => description.as_deref(), LexType::Object(o) => o.description.as_deref(), _ => None, } }}