diff --git a/Cargo.lock b/Cargo.lock index 9f2c1b9..f742687 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1248,6 +1248,7 @@ dependencies = [ "glob", "heck", "insta", + "stef-compiler", "stef-meta", "stef-parser", ] @@ -1291,7 +1292,7 @@ dependencies = [ name = "stef-meta" version = "0.1.0" dependencies = [ - "stef-parser", + "stef-compiler", ] [[package]] diff --git a/crates/stef-build/src/decode.rs b/crates/stef-build/src/decode.rs index 39118ba..daf3d8f 100644 --- a/crates/stef-build/src/decode.rs +++ b/crates/stef-build/src/decode.rs @@ -9,10 +9,10 @@ use crate::{BytesType, Opts}; pub(super) fn compile_struct( opts: &Opts, Struct { - comment: _, name, generics, fields, + .. }: &Struct<'_>, ) -> TokenStream { let name = Ident::new(name, Span::call_site()); @@ -54,10 +54,10 @@ pub(super) fn compile_struct( pub(super) fn compile_enum( opts: &Opts, Enum { - comment: _, name, generics, variants, + .. }: &Enum<'_>, ) -> TokenStream { let name = Ident::new(name, Span::call_site()); diff --git a/crates/stef-build/src/definition.rs b/crates/stef-build/src/definition.rs index 212c9ab..b3d0300 100644 --- a/crates/stef-build/src/definition.rs +++ b/crates/stef-build/src/definition.rs @@ -62,6 +62,7 @@ fn compile_module( comment, name, definitions, + .. }: &Module<'_>, ) -> TokenStream { let comment = compile_comment(comment); @@ -86,6 +87,7 @@ fn compile_struct( name, generics, fields, + .. }: &Struct<'_>, ) -> TokenStream { let comment = compile_comment(comment); @@ -109,6 +111,7 @@ fn compile_enum( name, generics, variants, + .. }: &Enum<'_>, ) -> TokenStream { let comment = compile_comment(comment); @@ -152,6 +155,7 @@ fn compile_alias( name, generics, target, + .. }: &TypeAlias<'_>, ) -> TokenStream { let comment = compile_comment(comment); @@ -172,6 +176,7 @@ fn compile_const( name, ty, value, + .. }: &Const<'_>, ) -> TokenStream { let comment = compile_comment(comment); diff --git a/crates/stef-build/src/encode.rs b/crates/stef-build/src/encode.rs index 53b9ff5..c18cb7e 100644 --- a/crates/stef-build/src/encode.rs +++ b/crates/stef-build/src/encode.rs @@ -7,10 +7,10 @@ use crate::{BytesType, Opts}; pub(super) fn compile_struct( opts: &Opts, Struct { - comment: _, name, generics, fields, + .. }: &Struct<'_>, ) -> TokenStream { let names = fields @@ -48,10 +48,10 @@ pub(super) fn compile_struct( pub(super) fn compile_enum( opts: &Opts, Enum { - comment: _, name, generics, variants, + .. }: &Enum<'_>, ) -> TokenStream { let name = Ident::new(name, Span::call_site()); @@ -79,11 +79,7 @@ pub(super) fn compile_enum( fn compile_variant( opts: &Opts, Variant { - comment: _, - name, - fields, - id, - .. + name, fields, id, .. }: &Variant<'_>, ) -> TokenStream { let id = proc_macro2::Literal::u32_unsuffixed(*id); diff --git a/crates/stef-build/src/size.rs b/crates/stef-build/src/size.rs index e9b7938..d90534a 100644 --- a/crates/stef-build/src/size.rs +++ b/crates/stef-build/src/size.rs @@ -7,10 +7,10 @@ use crate::{BytesType, Opts}; pub(super) fn compile_struct( opts: &Opts, Struct { - comment: _, name, generics, fields, + .. }: &Struct<'_>, ) -> TokenStream { let names = fields @@ -74,10 +74,10 @@ fn compile_struct_fields(opts: &Opts, fields: &Fields<'_>) -> TokenStream { pub(super) fn compile_enum( opts: &Opts, Enum { - comment: _, name, generics, variants, + .. }: &Enum<'_>, ) -> TokenStream { let name = Ident::new(name, Span::call_site()); diff --git a/crates/stef-compiler/src/simplify.rs b/crates/stef-compiler/src/simplify.rs index b81ac50..d1766a8 100644 --- a/crates/stef-compiler/src/simplify.rs +++ b/crates/stef-compiler/src/simplify.rs @@ -7,6 +7,9 @@ use std::borrow::Cow; /// Uppermost element, describing a single schema file. #[cfg_attr(feature = "json", derive(schemars::JsonSchema, serde::Serialize))] pub struct Schema<'a> { + /// Original parser element. + #[cfg_attr(feature = "json", serde(skip))] + pub source: &'a stef_parser::Schema<'a>, /// Optional schema-level comment. pub comment: Box<[&'a str]>, /// List of all the definitions that make up the schema. @@ -48,6 +51,9 @@ pub enum Definition<'a> { /// Scoping mechanism to categorize elements. #[cfg_attr(feature = "json", derive(schemars::JsonSchema, serde::Serialize))] pub struct Module<'a> { + /// Original parser element. + #[cfg_attr(feature = "json", serde(skip))] + pub source: &'a stef_parser::Module<'a>, /// Optional module-level comment. pub comment: Box<[&'a str]>, /// Unique name of the module, within the current scope. @@ -59,6 +65,9 @@ pub struct Module<'a> { /// Rust-ish struct. #[cfg_attr(feature = "json", derive(schemars::JsonSchema, serde::Serialize))] pub struct Struct<'a> { + /// Original parser element. + #[cfg_attr(feature = "json", serde(skip))] + pub source: &'a stef_parser::Struct<'a>, /// Optional struct-level comment. pub comment: Box<[&'a str]>, /// Unique name for this struct (within its scope). @@ -72,6 +81,9 @@ pub struct Struct<'a> { /// Rust-ish enum. #[cfg_attr(feature = "json", derive(schemars::JsonSchema, serde::Serialize))] pub struct Enum<'a> { + /// Original parser element. + #[cfg_attr(feature = "json", serde(skip))] + pub source: &'a stef_parser::Enum<'a>, /// Optional enum-level comment. pub comment: Box<[&'a str]>, /// Unique name for this enum, within its current scope. @@ -85,6 +97,9 @@ pub struct Enum<'a> { /// Single variant of an enum. #[cfg_attr(feature = "json", derive(schemars::JsonSchema, serde::Serialize))] pub struct Variant<'a> { + /// Original parser element. + #[cfg_attr(feature = "json", serde(skip))] + pub source: &'a stef_parser::Variant<'a>, /// Optional variant-level comment. pub comment: Box<[&'a str]>, /// Unique for this variant, within the enum it belongs to. @@ -98,6 +113,9 @@ pub struct Variant<'a> { /// Fields of a struct or enum that define its structure. #[cfg_attr(feature = "json", derive(schemars::JsonSchema, serde::Serialize))] pub struct Fields<'a> { + /// Original parser element. + #[cfg_attr(feature = "json", serde(skip))] + pub source: &'a stef_parser::Fields<'a>, /// List of contained fields. pub fields: Box<[Field<'a>]>, /// The way how the fields are defined, like named or unnamed. @@ -107,6 +125,9 @@ pub struct Fields<'a> { /// Single unified field that might be named or unnamed. #[cfg_attr(feature = "json", derive(schemars::JsonSchema, serde::Serialize))] pub struct Field<'a> { + /// Original parser element. + #[cfg_attr(feature = "json", serde(skip))] + pub source: ParserField<'a>, /// Optional field-level comment. pub comment: Box<[&'a str]>, /// Unique name for this field, within the current element. @@ -117,6 +138,14 @@ pub struct Field<'a> { pub id: u32, } +/// Field from the [`stef_parser`] create, where a [`Field`] structure originates from. +pub enum ParserField<'a> { + /// Named field. + Named(&'a stef_parser::NamedField<'a>), + /// Unnamed field + Unnamed(&'a stef_parser::UnnamedField<'a>), +} + /// Possible kinds in which the fields of a struct or enum variant can be represented. #[derive(Eq, PartialEq)] #[cfg_attr(feature = "json", derive(schemars::JsonSchema, serde::Serialize))] @@ -132,6 +161,9 @@ pub enum FieldKind { /// Alias (re-name) from one type to another. #[cfg_attr(feature = "json", derive(schemars::JsonSchema, serde::Serialize))] pub struct TypeAlias<'a> { + /// Original parser element. + #[cfg_attr(feature = "json", serde(skip))] + pub source: &'a stef_parser::TypeAlias<'a>, /// Optional element-level comment. pub comment: Box<[&'a str]>, /// Unique name of the type alias within the current scope. @@ -145,6 +177,9 @@ pub struct TypeAlias<'a> { /// Declaration of a constant value. #[cfg_attr(feature = "json", derive(schemars::JsonSchema, serde::Serialize))] pub struct Const<'a> { + /// Original parser element. + #[cfg_attr(feature = "json", serde(skip))] + pub source: &'a stef_parser::Const<'a>, /// Optional element-level comment. pub comment: Box<[&'a str]>, /// Unique identifier of this constant. @@ -173,6 +208,9 @@ pub enum Literal { /// Import declaration for an external schema. #[cfg_attr(feature = "json", derive(schemars::JsonSchema, serde::Serialize))] pub struct Import<'a> { + /// Original parser element. + #[cfg_attr(feature = "json", serde(skip))] + pub source: &'a stef_parser::Import<'a>, /// Individual elements that form the import path. pub segments: Box<[&'a str]>, /// Optional final element that allows to fully import the type, making it look as it would be @@ -259,6 +297,7 @@ pub struct ExternalType<'a> { #[must_use] pub fn schema<'a>(schema: &'a stef_parser::Schema<'_>) -> Schema<'a> { Schema { + source: schema, comment: comment(&schema.comment), definitions: definitions(&schema.definitions), } @@ -292,6 +331,7 @@ fn definition<'a>(item: &'a stef_parser::Definition<'_>) -> Definition<'a> { fn simplify_module<'a>(item: &'a stef_parser::Module<'_>) -> Module<'a> { Module { + source: item, comment: comment(&item.comment), name: item.name.get(), definitions: definitions(&item.definitions), @@ -300,6 +340,7 @@ fn simplify_module<'a>(item: &'a stef_parser::Module<'_>) -> Module<'a> { fn simplify_struct<'a>(item: &'a stef_parser::Struct<'_>) -> Struct<'a> { Struct { + source: item, comment: comment(&item.comment), name: item.name.get(), generics: generics(&item.generics), @@ -309,6 +350,7 @@ fn simplify_struct<'a>(item: &'a stef_parser::Struct<'_>) -> Struct<'a> { fn simplify_enum<'a>(item: &'a stef_parser::Enum<'_>) -> Enum<'a> { Enum { + source: item, comment: comment(&item.comment), name: item.name.get(), generics: generics(&item.generics), @@ -322,6 +364,7 @@ fn simplify_enum<'a>(item: &'a stef_parser::Enum<'_>) -> Enum<'a> { fn simplify_variant<'a>(item: &'a stef_parser::Variant<'_>) -> Variant<'a> { Variant { + source: item, comment: comment(&item.comment), name: item.name.get(), fields: simplify_fields(&item.fields), @@ -332,9 +375,11 @@ fn simplify_variant<'a>(item: &'a stef_parser::Variant<'_>) -> Variant<'a> { fn simplify_fields<'a>(item: &'a stef_parser::Fields<'_>) -> Fields<'a> { match item { stef_parser::Fields::Named(named) => Fields { + source: item, fields: named .iter() .map(|field| Field { + source: ParserField::Named(field), comment: comment(&field.comment), name: field.name.get().into(), ty: simplify_type(&field.ty), @@ -344,10 +389,12 @@ fn simplify_fields<'a>(item: &'a stef_parser::Fields<'_>) -> Fields<'a> { kind: FieldKind::Named, }, stef_parser::Fields::Unnamed(unnamed) => Fields { + source: item, fields: unnamed .iter() .enumerate() .map(|(i, field)| Field { + source: ParserField::Unnamed(field), comment: Box::default(), name: format!("n{i}").into(), ty: simplify_type(&field.ty), @@ -357,6 +404,7 @@ fn simplify_fields<'a>(item: &'a stef_parser::Fields<'_>) -> Fields<'a> { kind: FieldKind::Unnamed, }, stef_parser::Fields::Unit => Fields { + source: item, fields: Box::default(), kind: FieldKind::Unit, }, @@ -405,6 +453,7 @@ fn simplify_type<'a>(item: &'a stef_parser::Type<'_>) -> Type<'a> { fn simplify_alias<'a>(item: &'a stef_parser::TypeAlias<'_>) -> TypeAlias<'a> { TypeAlias { + source: item, comment: comment(&item.comment), name: item.name.get(), generics: generics(&item.generics), @@ -414,6 +463,7 @@ fn simplify_alias<'a>(item: &'a stef_parser::TypeAlias<'_>) -> TypeAlias<'a> { fn simplify_const<'a>(item: &'a stef_parser::Const<'_>) -> Const<'a> { Const { + source: item, comment: comment(&item.comment), name: item.name.get(), ty: simplify_type(&item.ty), @@ -433,6 +483,7 @@ fn simplify_literal(item: &stef_parser::Literal) -> Literal { fn simplify_import<'a>(item: &'a stef_parser::Import<'_>) -> Import<'a> { Import { + source: item, segments: item.segments.iter().map(stef_parser::Name::get).collect(), element: item.element.as_ref().map(|element| element.get().into()), } diff --git a/crates/stef-doc/Cargo.toml b/crates/stef-doc/Cargo.toml index 3481f70..197d046 100644 --- a/crates/stef-doc/Cargo.toml +++ b/crates/stef-doc/Cargo.toml @@ -13,12 +13,13 @@ license.workspace = true anyhow.workspace = true askama = { version = "0.12.1", default-features = false, features = ["markdown"] } heck = "0.4.1" +stef-compiler = { path = "../stef-compiler" } stef-meta = { path = "../stef-meta" } -stef-parser = { path = "../stef-parser" } [dev-dependencies] glob.workspace = true insta.workspace = true +stef-parser = { path = "../stef-parser" } [lints] workspace = true diff --git a/crates/stef-doc/examples/render.rs b/crates/stef-doc/examples/render.rs index 90310e3..4a24bbf 100644 --- a/crates/stef-doc/examples/render.rs +++ b/crates/stef-doc/examples/render.rs @@ -31,6 +31,7 @@ fn main() { let input = fs::read_to_string(&path).unwrap(); let value = Schema::parse(input.as_str(), Some(name)).unwrap(); + let value = stef_compiler::simplify_schema(&value); let value = stef_doc::render_schema(&Opts {}, &value).unwrap(); let out = out.join(name).with_extension(""); diff --git a/crates/stef-doc/input.css b/crates/stef-doc/input.css index c936f9a..d1bf263 100644 --- a/crates/stef-doc/input.css +++ b/crates/stef-doc/input.css @@ -65,6 +65,14 @@ @apply text-orange-600 dark:text-orange-400; } + .variant-name { + @apply text-teal-600 dark:text-teal-400; + } + + .variant-id { + @apply text-pink-600 dark:text-pink-400; + } + .field-definition { @apply mt-2 p-2 bg-main-300/50 dark:bg-main-700/50 rounded-sm; } diff --git a/crates/stef-doc/src/lib.rs b/crates/stef-doc/src/lib.rs index 141b0dd..942f11c 100644 --- a/crates/stef-doc/src/lib.rs +++ b/crates/stef-doc/src/lib.rs @@ -4,7 +4,7 @@ use std::rc::Rc; use anyhow::Result; use askama::Template; -use stef_parser::{Const, Definition, Enum, Module, Schema, Struct, TypeAlias}; +use stef_compiler::simplify::{Const, Definition, Enum, Module, Schema, Struct, TypeAlias}; mod templates; @@ -38,13 +38,13 @@ pub struct Output<'a> { pub fn render_schema<'a>( _opts: &'a Opts, Schema { - path, + source, comment, definitions, - .. }: &'a Schema<'_>, ) -> Result> { - let name = path + let name = source + .path .as_ref() .and_then(|p| p.file_stem()) .and_then(|p| p.to_str()) @@ -86,7 +86,7 @@ fn render_definition<'a>( fn render_module<'a>(item: &'a Module<'_>, path: &Rc<[Rc]>) -> Result> { let path = { let mut path = path.to_vec(); - path.push(item.name.get().into()); + path.push(item.name.into()); Rc::from(path) }; @@ -97,7 +97,7 @@ fn render_module<'a>(item: &'a Module<'_>, path: &Rc<[Rc]>) -> Result>()?, - name: item.name.get(), + name: item.name, path, file: format!("{}/index.html", item.name), }) @@ -105,7 +105,7 @@ fn render_module<'a>(item: &'a Module<'_>, path: &Rc<[Rc]>) -> Result(item: &'a Struct<'_>, path: &Rc<[Rc]>) -> Result> { Ok(Output { - name: item.name.get(), + name: item.name, path: Rc::clone(path), file: format!("struct.{}.html", item.name), content: templates::StructDetail { path, item }.render()?, @@ -115,7 +115,7 @@ fn render_struct<'a>(item: &'a Struct<'_>, path: &Rc<[Rc]>) -> Result(item: &'a Enum<'_>, path: &Rc<[Rc]>) -> Result> { Ok(Output { - name: item.name.get(), + name: item.name, path: Rc::clone(path), file: format!("enum.{}.html", item.name), content: templates::EnumDetail { path, item }.render()?, @@ -125,7 +125,7 @@ fn render_enum<'a>(item: &'a Enum<'_>, path: &Rc<[Rc]>) -> Result(item: &'a TypeAlias<'_>, path: &Rc<[Rc]>) -> Result> { Ok(Output { - name: item.name.get(), + name: item.name, path: Rc::clone(path), file: format!("alias.{}.html", item.name), content: templates::AliasDetail { path, item }.render()?, @@ -135,7 +135,7 @@ fn render_alias<'a>(item: &'a TypeAlias<'_>, path: &Rc<[Rc]>) -> Result(item: &'a Const<'_>, path: &Rc<[Rc]>) -> Result> { Ok(Output { - name: item.name.get(), + name: item.name, path: Rc::clone(path), file: format!("constant.{}.html", item.name), content: templates::ConstDetail { path, item }.render()?, diff --git a/crates/stef-doc/src/templates.rs b/crates/stef-doc/src/templates.rs index 938fcd0..1e6ba4f 100644 --- a/crates/stef-doc/src/templates.rs +++ b/crates/stef-doc/src/templates.rs @@ -1,18 +1,21 @@ use std::{ - fmt::{self, Display}, + fmt::{self, Display, Write}, rc::Rc, }; use askama::Template; +use stef_compiler::simplify::{ + Const, Definition, Enum, ExternalType, Field, FieldKind, Literal, Module, Struct, Type, + TypeAlias, +}; use stef_meta::WireSize; -use stef_parser::{Comment, Const, Definition, Enum, Fields, Module, Struct, TypeAlias}; #[derive(Template)] #[template(path = "index.html")] pub struct Index<'a> { pub name: &'a str, pub path: &'a [Rc], - pub comment: &'a Comment<'a>, + pub comment: &'a [&'a str], pub definitions: &'a [Definition<'a>], } @@ -30,6 +33,46 @@ pub struct StructDetail<'a> { pub item: &'a Struct<'a>, } +impl StructDetail<'_> { + fn print_schema(&self) -> String { + let mut buf = format!("struct {}", self.item.name); + + if !self.item.generics.is_empty() { + buf.push('<'); + for (i, gen) in self.item.generics.iter().enumerate() { + if i > 0 { + buf.push_str(", "); + } + buf.push_str(gen); + } + buf.push('>'); + } + + match self.item.fields.kind { + FieldKind::Named => { + buf.push_str(" {\n"); + for field in &*self.item.fields.fields { + let _ = writeln!(&mut buf, " {},", PrintField(field, FieldKind::Named)); + } + buf.push('}'); + } + FieldKind::Unnamed => { + buf.push('('); + for (i, field) in self.item.fields.fields.iter().enumerate() { + if i > 0 { + buf.push_str(", "); + } + let _ = write!(&mut buf, "{}", PrintField(field, FieldKind::Unnamed)); + } + buf.push(')'); + } + FieldKind::Unit => {} + } + + buf + } +} + #[derive(Template)] #[template(path = "detail/enum.html")] pub struct EnumDetail<'a> { @@ -37,6 +80,55 @@ pub struct EnumDetail<'a> { pub item: &'a Enum<'a>, } +impl EnumDetail<'_> { + fn print_schema(&self) -> String { + let mut buf = format!("enum {}", self.item.name); + + if !self.item.generics.is_empty() { + buf.push('<'); + for (i, gen) in self.item.generics.iter().enumerate() { + if i > 0 { + buf.push_str(", "); + } + buf.push_str(gen); + } + buf.push('>'); + } + + buf.push_str(" {\n"); + + for variant in &self.item.variants { + let _ = write!(&mut buf, " {}", variant.name); + match variant.fields.kind { + FieldKind::Named => { + buf.push_str(" {\n"); + for field in &*variant.fields.fields { + let _ = + writeln!(&mut buf, " {},", PrintField(field, FieldKind::Named)); + } + let _ = writeln!(&mut buf, " }} @{},", variant.id); + } + FieldKind::Unnamed => { + buf.push('('); + for (i, field) in variant.fields.fields.iter().enumerate() { + if i > 0 { + buf.push_str(", "); + } + let _ = write!(&mut buf, "{}", PrintField(field, FieldKind::Unnamed)); + } + let _ = writeln!(&mut buf, ") @{},", variant.id); + } + FieldKind::Unit => { + let _ = writeln!(&mut buf, " @{},", variant.id); + } + } + } + + buf.push('}'); + buf + } +} + #[derive(Template)] #[template(path = "detail/alias.html")] pub struct AliasDetail<'a> { @@ -44,6 +136,26 @@ pub struct AliasDetail<'a> { pub item: &'a TypeAlias<'a>, } +impl AliasDetail<'_> { + fn print_schema(&self) -> String { + let mut buf = format!("type {}", self.item.name); + + if !self.item.generics.is_empty() { + buf.push('<'); + for (i, gen) in self.item.generics.iter().enumerate() { + if i > 0 { + buf.push_str(", "); + } + buf.push_str(gen); + } + buf.push('>'); + } + + let _ = write!(&mut buf, " = {};", PrintType(&self.item.target)); + buf + } +} + #[derive(Template)] #[template(path = "detail/const.html")] pub struct ConstDetail<'a> { @@ -51,6 +163,17 @@ pub struct ConstDetail<'a> { pub item: &'a Const<'a>, } +impl ConstDetail<'_> { + fn print_schema(&self) -> String { + format!( + "const {}: {} = {};", + self.item.name, + PrintType(&self.item.ty), + PrintLiteral(&self.item.value) + ) + } +} + fn render_wire_size(size: &WireSize) -> String { let mut buf = String::new(); size.print(&mut buf, 0); @@ -75,32 +198,130 @@ impl Display for PathUp { } } -fn first_comment(item: &Comment<'_>) -> String { - item.0 - .iter() - .take_while(|line| !line.value.trim().is_empty()) +fn first_comment(item: &[&str]) -> String { + item.iter() + .take_while(|line| !line.trim().is_empty()) .fold(String::new(), |mut acc, line| { - acc.push_str(line.value); + acc.push_str(line); acc.push('\n'); acc }) } -fn merge_comments(item: &Comment<'_>) -> String { - item.0.iter().fold(String::new(), |mut acc, line| { - acc.push_str(line.value); +fn merge_comments(item: &[&str]) -> String { + item.iter().fold(String::new(), |mut acc, line| { + acc.push_str(line); acc.push('\n'); acc }) } -struct MergeComments<'a>(&'a Comment<'a>); +struct MergeComments<'a>(&'a [&'a str]); impl Display for MergeComments<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - for line in &self.0 .0 { - writeln!(f, "{}", line.value)?; + for line in self.0 { + writeln!(f, "{line}")?; } Ok(()) } } + +struct PrintField<'a>(&'a Field<'a>, FieldKind); + +impl Display for PrintField<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self.1 { + FieldKind::Named => write!( + f, + "{}: {} @{}", + self.0.name, + PrintType(&self.0.ty), + self.0.id + ), + FieldKind::Unnamed => write!(f, "{} @{}", PrintType(&self.0.ty), self.0.id), + FieldKind::Unit => Ok(()), + } + } +} + +struct PrintType<'a>(&'a Type<'a>); + +impl Display for PrintType<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self.0 { + Type::Bool => f.write_str("bool"), + Type::U8 => f.write_str("u8"), + Type::U16 => f.write_str("u16"), + Type::U32 => f.write_str("u32"), + Type::U64 => f.write_str("u64"), + Type::U128 => f.write_str("u128"), + Type::I8 => f.write_str("i8"), + Type::I16 => f.write_str("i16"), + Type::I32 => f.write_str("i32"), + Type::I64 => f.write_str("i64"), + Type::I128 => f.write_str("i128"), + Type::F32 => f.write_str("f32"), + Type::F64 => f.write_str("f64"), + Type::String => f.write_str("string"), + Type::StringRef => f.write_str("&string"), + Type::Bytes => f.write_str("bytes"), + Type::BytesRef => f.write_str("&bytes"), + Type::Vec(t) => write!(f, "vec<{}>", Self(t)), + Type::HashMap(kv) => write!(f, "hash_map<{}, {}>", Self(&kv.0), Self(&kv.1)), + Type::HashSet(t) => write!(f, "hash_set<{}>", Self(t)), + Type::Option(t) => write!(f, "option<{}>", Self(t)), + Type::NonZero(t) => write!(f, "non_zero<{}>", Self(t)), + Type::BoxString => f.write_str("box"), + Type::BoxBytes => f.write_str("box"), + Type::Tuple(types) => { + f.write_char('(')?; + for (i, ty) in types.iter().enumerate() { + if i > 0 { + f.write_str(", ")?; + } + write!(f, "{}", PrintType(ty))?; + } + f.write_char(')') + } + Type::Array(t, size) => write!(f, "[{}; {size}]", Self(t)), + Type::External(ExternalType { + path, + name, + generics, + }) => { + for seg in &**path { + write!(f, "{seg}::")?; + } + f.write_str(name)?; + + if !generics.is_empty() { + f.write_char('<')?; + for (i, gen) in generics.iter().enumerate() { + if i > 0 { + f.write_str(", ")?; + } + write!(f, "{}", PrintType(gen))?; + } + f.write_char('>')?; + } + + Ok(()) + } + } + } +} + +struct PrintLiteral<'a>(&'a Literal); + +impl Display for PrintLiteral<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self.0 { + Literal::Bool(v) => v.fmt(f), + Literal::Int(v) => v.fmt(f), + Literal::Float(v) => v.fmt(f), + Literal::String(v) => write!(f, "{v:?}"), + Literal::Bytes(v) => write!(f, "{v:?}"), + } + } +} diff --git a/crates/stef-doc/templates/detail/alias.html b/crates/stef-doc/templates/detail/alias.html index 614f635..c6ab05e 100644 --- a/crates/stef-doc/templates/detail/alias.html +++ b/crates/stef-doc/templates/detail/alias.html @@ -11,7 +11,7 @@

{%- endfor -%} {{ item.name }}

-
{{ item }}
+
{{ self.print_schema() }}
{{ self::merge_comments(item.comment)|markdown|trim|safe }}
diff --git a/crates/stef-doc/templates/detail/const.html b/crates/stef-doc/templates/detail/const.html index 1cc24b0..d8a8f13 100644 --- a/crates/stef-doc/templates/detail/const.html +++ b/crates/stef-doc/templates/detail/const.html @@ -11,7 +11,7 @@

{%- endfor -%} {{ item.name }}

-
{{ item }}
+
{{ self.print_schema() }}
{{ self::merge_comments(item.comment)|markdown|trim|safe }}
diff --git a/crates/stef-doc/templates/detail/enum.html b/crates/stef-doc/templates/detail/enum.html index f17efd9..bdf01e5 100644 --- a/crates/stef-doc/templates/detail/enum.html +++ b/crates/stef-doc/templates/detail/enum.html @@ -11,7 +11,7 @@

{%- endfor -%} {{ item.name }}

-
{{ item }}
+
{{ self.print_schema() }}
{{ self::merge_comments(item.comment)|markdown|trim|safe }}
@@ -21,27 +21,29 @@

Variants

    {%- for variant in item.variants %}
  • -
    {{ variant.name }}
    + + {{ variant.name }} + @{{ variant.id }} +
    {{ self::merge_comments(variant.comment)|markdown|trim|safe }}
    + {%- if variant.fields.kind != FieldKind::Unit %}
    - {%- match variant.fields %} - {%- when Fields::Named(fields) %}

    Fields

      - {%- for field in fields %} + {%- for field in variant.fields.fields %}
    • {{ field.name }}: - {{ field.ty }} - {{ field.id }} + {{ PrintType(field.ty) }} + @{{ field.id }}
      {{ self::merge_comments(field.comment)|markdown|trim|safe }}
      - {%- match stef_meta::wire_size(field.ty.value) %} + {%- match stef_meta::wire_size(field.ty) %} {%- when Some(size) %}

      Metadata

      @@ -58,35 +60,8 @@

      Metadata

    • {%- endfor %}
    - {%- when Fields::Unnamed(fields) %} -

    Fields

    -
      - {%- for (i, field) in fields.iter().enumerate() %} -
    • - - {{ i }}: - {{ field.ty }} - {{ field.id }} - -
    • - - {%- match stef_meta::wire_size(field.ty.value) %} - {%- when Some(size) %} -

      Metadata

      -
      -

      The size range is:

      -
      - {{ self::render_wire_size(size)|markdown|trim|indent(14)|safe }} -
      -
      - {%- when None %} - {%- endmatch %} - {%- endfor %} -
    - {%- when Fields::Unit %} - {%- endmatch %}
    + {%- endif %}
  • {%- endfor %}
diff --git a/crates/stef-doc/templates/detail/struct.html b/crates/stef-doc/templates/detail/struct.html index 7fc9926..7f9190a 100644 --- a/crates/stef-doc/templates/detail/struct.html +++ b/crates/stef-doc/templates/detail/struct.html @@ -11,7 +11,7 @@

{%- endfor -%} {{ item.name }}

-
{{ item }}
+
{{ self.print_schema() }}
{{ self::merge_comments(item.comment)|markdown|trim|safe }}
@@ -28,27 +28,27 @@

Metadata

Fields

- {%- match item.fields %} - {%- when Fields::Named(fields) %}
    - {%- for field in fields %} + {%- for field in item.fields.fields %}
  • {{ field.name }}: - {{ field.ty }} - {{ field.id }} + {{ PrintType(field.ty) }} + @{{ field.id }} -
    +
    {{ self::merge_comments(field.comment)|markdown|trim|safe }}
    - {%- match stef_meta::wire_size(field.ty.value) %} + {%- match stef_meta::wire_size(field.ty) %} {%- when Some(size) %} -

    Metadata

    -
    -

    The size range is:

    -
    - {{ self::render_wire_size(size)|markdown|trim|indent(10)|safe }} +
    +

    Metadata

    +
    +

    The size range is:

    +
    + {{ self::render_wire_size(size)|markdown|trim|indent(10)|safe }} +
    {%- when None %} @@ -56,32 +56,5 @@

    Metadata

  • {%- endfor %}
- {%- when Fields::Unnamed(fields) %} -
    - {%- for (i, field) in fields.iter().enumerate() %} -
  • - - {{ i }}: - {{ field.ty }} - {{ field.id }} - -
  • - - {%- match stef_meta::wire_size(field.ty.value) %} - {%- when Some(size) %} -

    Metadata

    -
    -

    The size range is:

    -
    - {{ self::render_wire_size(size)|markdown|trim|indent(8)|safe }} -
    -
    - {%- when None %} - {%- endmatch %} - {%- endfor %} -
- {%- when Fields::Unit %}
-{%- endmatch %} {% endblock %} diff --git a/crates/stef-doc/tests/render.rs b/crates/stef-doc/tests/render.rs index 9b96e98..401401c 100644 --- a/crates/stef-doc/tests/render.rs +++ b/crates/stef-doc/tests/render.rs @@ -29,6 +29,7 @@ fn render_schema() { glob!("inputs/*.stef", |path| { let input = fs::read_to_string(path).unwrap(); let value = Schema::parse(input.as_str(), Some(&strip_path(path))).unwrap(); + let value = stef_compiler::simplify_schema(&value); let value = stef_doc::render_schema(&Opts {}, &value).unwrap(); let mut merged = String::new(); diff --git a/crates/stef-doc/tests/snapshots/render__render@alias_basic.stef.snap b/crates/stef-doc/tests/snapshots/render__render@alias_basic.stef.snap index 1c7ee91..c7d3dd2 100644 --- a/crates/stef-doc/tests/snapshots/render__render@alias_basic.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@alias_basic.stef.snap @@ -85,8 +85,7 @@ input_file: crates/stef-parser/tests/inputs/alias_basic.stef Alias alias_basic::Sample -
/// Sample type alias.
-type Sample = u32;
+
type Sample = u32;

Sample type alias.

diff --git a/crates/stef-doc/tests/snapshots/render__render@attribute_multi.stef.snap b/crates/stef-doc/tests/snapshots/render__render@attribute_multi.stef.snap index 02b3e2d..cd56ea3 100644 --- a/crates/stef-doc/tests/snapshots/render__render@attribute_multi.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@attribute_multi.stef.snap @@ -85,9 +85,7 @@ input_file: crates/stef-parser/tests/inputs/attribute_multi.stef Struct attribute_multi::Sample -
#[validate(min = 1, max = 100)]
-struct Sample
-
+
struct Sample
@@ -99,6 +97,8 @@ struct Sample

Fields

+
    +
diff --git a/crates/stef-doc/tests/snapshots/render__render@attribute_single.stef.snap b/crates/stef-doc/tests/snapshots/render__render@attribute_single.stef.snap index 66e12d5..aa59243 100644 --- a/crates/stef-doc/tests/snapshots/render__render@attribute_single.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@attribute_single.stef.snap @@ -85,9 +85,7 @@ input_file: crates/stef-parser/tests/inputs/attribute_single.stef Struct attribute_single::Sample -
#[deprecated = "don't use"]
-struct Sample
-
+
struct Sample
@@ -99,6 +97,8 @@ struct Sample

Fields

+
    +
diff --git a/crates/stef-doc/tests/snapshots/render__render@attribute_unit.stef.snap b/crates/stef-doc/tests/snapshots/render__render@attribute_unit.stef.snap index de58c62..afba5f5 100644 --- a/crates/stef-doc/tests/snapshots/render__render@attribute_unit.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@attribute_unit.stef.snap @@ -85,9 +85,7 @@ input_file: crates/stef-parser/tests/inputs/attribute_unit.stef Struct attribute_unit::Sample -
#[deprecated]
-struct Sample
-
+
struct Sample
@@ -99,6 +97,8 @@ struct Sample

Fields

+
    +
diff --git a/crates/stef-doc/tests/snapshots/render__render@attributes.stef.snap b/crates/stef-doc/tests/snapshots/render__render@attributes.stef.snap index bae05e8..ab1d3a2 100644 --- a/crates/stef-doc/tests/snapshots/render__render@attributes.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@attributes.stef.snap @@ -85,9 +85,7 @@ input_file: crates/stef-parser/tests/inputs/attributes.stef Struct attributes::Sample -
#[deprecated = "don't use", compress, validate(in_range(min = 100, max = 200), non_empty)]
-struct Sample
-
+
struct Sample
@@ -99,6 +97,8 @@ struct Sample

Fields

+
    +
diff --git a/crates/stef-doc/tests/snapshots/render__render@attributes_min_ws.stef.snap b/crates/stef-doc/tests/snapshots/render__render@attributes_min_ws.stef.snap index fdd603c..a306b94 100644 --- a/crates/stef-doc/tests/snapshots/render__render@attributes_min_ws.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@attributes_min_ws.stef.snap @@ -85,9 +85,7 @@ input_file: crates/stef-parser/tests/inputs/attributes_min_ws.stef Struct attributes_min_ws::Sample -
#[deprecated = "don't use", compress, validate(in_range(min = 100, max = 200), non_empty)]
-struct Sample
-
+
struct Sample
@@ -99,6 +97,8 @@ struct Sample

Fields

+
    +
diff --git a/crates/stef-doc/tests/snapshots/render__render@enum_basic.stef.snap b/crates/stef-doc/tests/snapshots/render__render@enum_basic.stef.snap index 35dea78..2fa6094 100644 --- a/crates/stef-doc/tests/snapshots/render__render@enum_basic.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@enum_basic.stef.snap @@ -85,18 +85,14 @@ input_file: crates/stef-parser/tests/inputs/enum_basic.stef Enum enum_basic::Sample -
/// Sample enum.
-enum Sample {
+  
enum Sample {
     One @1,
-    /// Second variant
     Two(u32 @1, u64 @2) @2,
     Three {
         field1: u32 @1,
-        /// Second field of third variant
         field2: bool @2,
     } @3,
-}
-
+}

Sample enum.

@@ -105,15 +101,19 @@ enum Sample {

Variants

  • -
    One
    + + One + @1 +
    -
    -
  • -
    Two
    + + Two + @2 +

    Second variant

    @@ -122,39 +122,52 @@ enum Sample {
    • - 0: + n0: u32 @1 -
    • -

      Metadata

      -
      -

      The size range is:

      -
      -

      u32 1..5

      +
      + +
      +
      +

      Metadata

      +
      +

      The size range is:

      +
      +

      u32 1..5

      +
      +
      -
      +
    • - 1: + n1: u64 @2 -
    • -

      Metadata

      -
      -

      The size range is:

      -
      -

      u64 1..10

      +
      + +
      +
      +

      Metadata

      +
      +

      The size range is:

      +
      +

      u64 1..10

      +
      +
      -
      +
  • -
    Three
    + + Three + @3 +
    diff --git a/crates/stef-doc/tests/snapshots/render__render@enum_generics.stef.snap b/crates/stef-doc/tests/snapshots/render__render@enum_generics.stef.snap index 3d8c870..3f5d133 100644 --- a/crates/stef-doc/tests/snapshots/render__render@enum_generics.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@enum_generics.stef.snap @@ -85,16 +85,14 @@ input_file: crates/stef-parser/tests/inputs/enum_generics.stef Enum enum_generics::Sample -
    /// Enum with generics.
    -enum Sample<A, B, C, D> {
    +  
    enum Sample<A, B, C, D> {
         One @1,
         Two(A @1, B @2) @2,
         Three {
             field1: C @1,
             field2: D @2,
         } @3,
    -}
    -
    +}

    Enum with generics.

    @@ -103,15 +101,19 @@ enum Sample<A, B, C, D> {

    Variants

    • -
      One
      + + One + @1 +
      -
      -
    • -
      Two
      + + Two + @2 +
      @@ -120,23 +122,32 @@ enum Sample<A, B, C, D> {
      • - 0: + n0: A @1 +
        + +
      • - 1: + n1: B @2 +
        + +
    • -
      Three
      + + Three + @3 +
      diff --git a/crates/stef-doc/tests/snapshots/render__render@enum_many_ws.stef.snap b/crates/stef-doc/tests/snapshots/render__render@enum_many_ws.stef.snap index 2e01290..965e5e0 100644 --- a/crates/stef-doc/tests/snapshots/render__render@enum_many_ws.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@enum_many_ws.stef.snap @@ -85,16 +85,14 @@ input_file: crates/stef-parser/tests/inputs/enum_many_ws.stef Enum enum_many_ws::Sample -
      /// Sample enum.
      -enum Sample {
      +  
      enum Sample {
           One @1,
           Two(u32 @1, u64 @2) @2,
           Three {
               field1: u32 @1,
               field2: bool @2,
           } @3,
      -}
      -
      +}

      Sample enum.

      @@ -103,15 +101,19 @@ enum Sample {

      Variants

      • -
        One
        + + One + @1 +
        -
        -
      • -
        Two
        + + Two + @2 +
        @@ -120,39 +122,52 @@ enum Sample {
        • - 0: + n0: u32 @1 -
        • -

          Metadata

          -
          -

          The size range is:

          -
          -

          u32 1..5

          +
          + +
          +
          +

          Metadata

          +
          +

          The size range is:

          +
          +

          u32 1..5

          +
          +
          -
          +
        • - 1: + n1: u64 @2 -
        • -

          Metadata

          -
          -

          The size range is:

          -
          -

          u64 1..10

          +
          + +
          +
          +

          Metadata

          +
          +

          The size range is:

          +
          +

          u64 1..10

          +
          +
          -
          +
      • -
        Three
        + + Three + @3 +
        diff --git a/crates/stef-doc/tests/snapshots/render__render@enum_min_ws.stef.snap b/crates/stef-doc/tests/snapshots/render__render@enum_min_ws.stef.snap index 9511016..aae3056 100644 --- a/crates/stef-doc/tests/snapshots/render__render@enum_min_ws.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@enum_min_ws.stef.snap @@ -93,8 +93,7 @@ input_file: crates/stef-parser/tests/inputs/enum_min_ws.stef field2: bool @2, field3: T @3, } @3, -} - +}
        @@ -103,15 +102,19 @@ input_file: crates/stef-parser/tests/inputs/enum_min_ws.stef

        Variants

        • -
          One
          + + One + @1 +
          -
          -
        • -
          Two
          + + Two + @2 +
          @@ -120,46 +123,62 @@ input_file: crates/stef-parser/tests/inputs/enum_min_ws.stef
          • - 0: + n0: u32 @1 -
          • -

            Metadata

            -
            -

            The size range is:

            -
            -

            u32 1..5

            +
            + +
            +
            +

            Metadata

            +
            +

            The size range is:

            +
            +

            u32 1..5

            +
            +
            -
            +
          • - 1: + n1: u64 @2 -
          • -

            Metadata

            -
            -

            The size range is:

            -
            -

            u64 1..10

            +
            + +
            +
            +

            Metadata

            +
            +

            The size range is:

            +
            +

            u64 1..10

            +
            +
            -
            +
          • - 2: + n2: T @3 +
            + +
        • -
          Three
          + + Three + @3 +
          diff --git a/crates/stef-doc/tests/snapshots/render__render@mixed.stef.snap b/crates/stef-doc/tests/snapshots/render__render@mixed.stef.snap index 57ab99c..3e070f6 100644 --- a/crates/stef-doc/tests/snapshots/render__render@mixed.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@mixed.stef.snap @@ -133,18 +133,12 @@ input_file: crates/stef-parser/tests/inputs/mixed.stef Struct mixed::User -
          /// Basic user information.
          -/// 
          -/// Uses various other `structs` and `enums` to compose the information
          -/// in a **type safe** way.
          -struct User {
          +  
          struct User {
               name: FullName @1,
          -    /// Physical address, might not be specified by the user.
               address: option<Address> @2,
               age: u8 @3,
               birthday: birthday::DayOfBirth @4,
          -}
          -
          +}

          Basic user information.

          Uses various other structs and enums to compose the information @@ -166,7 +160,7 @@ in a type safe way.

          FullName @1 -
          +
        • @@ -176,18 +170,20 @@ in a type safe way.

          option<Address> @2 -
          +

          Physical address, might not be specified by the user.

          -

          Metadata

          -
          -

          The size range is:

          -
          -

          option 0..

          +
          +

          Metadata

          +
          +

          The size range is:

          +
          +

          option 0..

          • value: unknown
          +
          @@ -197,15 +193,17 @@ in a type safe way.

          u8 @3 -
          +
          -

          Metadata

          -
          -

          The size range is:

          -
          -

          u8 1

          +
          +

          Metadata

          +
          +

          The size range is:

          +
          +

          u8 1

          +
          @@ -215,11 +213,12 @@ in a type safe way.

          birthday::DayOfBirth @4 -
          +
        + @@ -247,13 +246,11 @@ in a type safe way.

        Struct mixed::FullName -
        /// Full name of a user.
        -struct FullName {
        +  
        struct FullName {
             first: string @1,
             middle: option<string> @2,
             last: string @3,
        -}
        -
        +}

        Full name of a user.

        @@ -273,15 +270,17 @@ struct FullName { string @1 -
        +
        -

        Metadata

        -
        -

        The size range is:

        -
        -

        string 1..

        +
        +

        Metadata

        +
        +

        The size range is:

        +
        +

        string 1..

        +
      • @@ -291,18 +290,20 @@ struct FullName { option<string> @2 -
        +
        -

        Metadata

        -
        -

        The size range is:

        -
        -

        option 0..

        +
        +

        Metadata

        +
        +

        The size range is:

        +
        +

        option 0..

        • value: string 1..
        +
        @@ -312,19 +313,22 @@ struct FullName { string @3 -
        +
        -

        Metadata

        -
        -

        The size range is:

        -
        -

        string 1..

        +
        +

        Metadata

        +
        +

        The size range is:

        +
        +

        string 1..

        +
      + @@ -352,11 +356,7 @@ struct FullName { Alias mixed::Name -
      /// Simple alias for convenience.
      -/// 
      -/// - Might be easier to remember.
      -/// - Often referenced as this.
      -type Name = FullName;
      +
      type Name = FullName;

      Simple alias for convenience.

        @@ -393,13 +393,10 @@ type Name = FullName; mixed::Address
        struct Address {
        -    /// Street name.
             street: string @1,
        -    /// Number of the house in the street.
             house_no: HouseNumber @2,
             city: string @3,
        -}
        -
        +}
        @@ -419,15 +416,17 @@ type Name = FullName; string @1 -
        +

        Street name.

        -

        Metadata

        -
        -

        The size range is:

        -
        -

        string 1..

        +
        +

        Metadata

        +
        +

        The size range is:

        +
        +

        string 1..

        +
        @@ -437,7 +436,7 @@ type Name = FullName; HouseNumber @2 -
        +

        Number of the house in the street.

        @@ -447,19 +446,22 @@ type Name = FullName; string @3 -
        +
        -

        Metadata

        -
        -

        The size range is:

        -
        -

        string 1..

        +
        +

        Metadata

        +
        +

        The size range is:

        +
        +

        string 1..

        +
      +
      @@ -487,16 +489,10 @@ type Name = FullName; Enum mixed::HouseNumber -
      /// The number on the house.
      -/// 
      -/// More details can be found at [Wikipedia](https://en.wikipedia.org/wiki/House_numbering).
      -enum HouseNumber {
      -    /// Digit only number.
      +  
      enum HouseNumber {
           Digit(u16 @1) @1,
      -    /// Mixed _number_ with characters like `1a`.
           Text(string @1) @2,
      -}
      -
      +}

      The number on the house.

      More details can be found at Wikipedia.

      @@ -506,7 +502,10 @@ enum HouseNumber {

      Variants

      • -
        Digit
        + + Digit + @1 +

        Digit only number.

        @@ -515,24 +514,32 @@ enum HouseNumber {
        • - 0: + n0: u16 @1 -
        • -

          Metadata

          -
          -

          The size range is:

          -
          -

          u16 1..3

          +
          +
          -
          +
          +

          Metadata

          +
          +

          The size range is:

          +
          +

          u16 1..3

          +
          +
          +
          +
    • -
      Text
      + + Text + @2 +

      Mixed number with characters like 1a.

      @@ -541,19 +548,24 @@ enum HouseNumber {
      • - 0: + n0: string @1 -
      • -

        Metadata

        -
        -

        The size range is:

        -
        -

        string 1..

        +
        +
        -
        +
        +

        Metadata

        +
        +

        The size range is:

        +
        +

        string 1..

        +
        +
        +
        +
    • @@ -586,8 +598,7 @@ enum HouseNumber { Constant mixed::MAX_AGE -
      /// Probably the max age of a human, currently.
      -const MAX_AGE: u8 = 120;
      +
      const MAX_AGE: u8 = 120;

      Probably the max age of a human, currently.

      @@ -707,23 +718,17 @@ on the month.

      Enum mixed::birthday::DayOfBirth -
      /// As the name suggests, specifies details about birthdays.
      -enum DayOfBirth {
      +  
      enum DayOfBirth {
           Specific {
               year: u16 @1,
               month: Month @2,
               day: u8 @3,
           } @1,
      -    /// The user didn't want to say.
           Secret {
      -        /// Optional info from the user about why they didn't want to
      -        /// reveal their birthday.
               reason: option<string> @1,
           } @2,
      -    /// We never asked and nobody knows.
           Unknown @3,
      -}
      -
      +}

      As the name suggests, specifies details about birthdays.

      @@ -732,7 +737,10 @@ enum DayOfBirth {

      Variants

      • -
        Specific
        + + Specific + @1 +
        @@ -793,7 +801,10 @@ enum DayOfBirth {
      • -
        Secret
        + + Secret + @2 +

        The user didn't want to say.

        @@ -828,12 +839,13 @@ reveal their birthday.

      • -
        Unknown
        + + Unknown + @3 +

        We never asked and nobody knows.

        -
        -
      @@ -864,8 +876,7 @@ reveal their birthday.

      Constant mixed::birthday::MIN_YEAR -
      /// Let's assume we only have details of people born **after** this year.
      -const MIN_YEAR: u16 = 1900;
      +
      const MIN_YEAR: u16 = 1900;

      Let's assume we only have details of people born after this year.

      @@ -897,9 +908,7 @@ const MIN_YEAR: u16 = 1900; Constant mixed::birthday::MAX_DAY -
      /// Absolute maximum for a day, but might be even less depending
      -/// on the month.
      -const MAX_DAY: u8 = 31;
      +
      const MAX_DAY: u8 = 31;

      Absolute maximum for a day, but might be even less depending on the month.

      @@ -932,8 +941,7 @@ on the month.

      Enum mixed::birthday::Month -
      /// The month of the year.
      -enum Month {
      +  
      enum Month {
           January @1,
           February @2,
           March @3,
      @@ -946,8 +954,7 @@ enum Month {
           October @10,
           November @11,
           December @12,
      -}
      -
      +}

      The month of the year.

      @@ -956,100 +963,112 @@ enum Month {

      Variants

      • -
        January
        + + January + @1 +
        -
        -
      • -
        February
        + + February + @2 +
        -
        -
      • -
        March
        + + March + @3 +
        -
        -
      • -
        April
        + + April + @4 +
        -
        -
      • -
        May
        + + May + @5 +
        -
        -
      • -
        June
        + + June + @6 +
        -
        -
      • -
        July
        + + July + @7 +
        -
        -
      • -
        August
        + + August + @8 +
        -
        -
      • -
        September
        + + September + @9 +
        -
        -
      • -
        October
        + + October + @10 +
        -
        -
      • -
        November
        + + November + @11 +
        -
        -
      • -
        December
        + + December + @12 +
        -
        -
      diff --git a/crates/stef-doc/tests/snapshots/render__render@module_basic.stef.snap b/crates/stef-doc/tests/snapshots/render__render@module_basic.stef.snap index d7f82fe..d15950e 100644 --- a/crates/stef-doc/tests/snapshots/render__render@module_basic.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@module_basic.stef.snap @@ -221,8 +221,7 @@ input_file: crates/stef-parser/tests/inputs/module_basic.stef
      enum Sample {
           One @1,
      -}
      -
      +}
      @@ -231,12 +230,13 @@ input_file: crates/stef-parser/tests/inputs/module_basic.stef

      Variants

      • -
        One
        + + One + @1 +
        -
        -
      @@ -270,8 +270,7 @@ input_file: crates/stef-parser/tests/inputs/module_basic.stef
      struct Sample {
           value: u32 @1,
           inner: b::Sample @2,
      -}
      -
      +}
      @@ -291,15 +290,17 @@ input_file: crates/stef-parser/tests/inputs/module_basic.stef u32 @1 -
      +
      -

      Metadata

      -
      -

      The size range is:

      -
      -

      u32 1..5

      +
      +

      Metadata

      +
      +

      The size range is:

      +
      +

      u32 1..5

      +
      @@ -309,11 +310,12 @@ input_file: crates/stef-parser/tests/inputs/module_basic.stef b::Sample @2 -
      +
    + diff --git a/crates/stef-doc/tests/snapshots/render__render@schema_basic.stef.snap b/crates/stef-doc/tests/snapshots/render__render@schema_basic.stef.snap index fa70646..13c0fb1 100644 --- a/crates/stef-doc/tests/snapshots/render__render@schema_basic.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@schema_basic.stef.snap @@ -93,12 +93,10 @@ input_file: crates/stef-parser/tests/inputs/schema_basic.stef Struct schema_basic::SampleStruct -
    /// Basic struct.
    -struct SampleStruct {
    +  
    struct SampleStruct {
         a: u32 @1,
         b: bool @2,
    -}
    -
    +}

    Basic struct.

    @@ -118,15 +116,17 @@ struct SampleStruct { u32 @1 -
    +
    -

    Metadata

    -
    -

    The size range is:

    -
    -

    u32 1..5

    +
    +

    Metadata

    +
    +

    The size range is:

    +
    +

    u32 1..5

    +
  • @@ -136,19 +136,22 @@ struct SampleStruct { bool @2 -
    +
    -

    Metadata

    -
    -

    The size range is:

    -
    -

    bool 1

    +
    +

    Metadata

    +
    +

    The size range is:

    +
    +

    bool 1

    +
+ @@ -176,16 +179,14 @@ struct SampleStruct { Enum schema_basic::SampleEnum -
/// Sample enum.
-enum SampleEnum {
+  
enum SampleEnum {
     One @1,
     Two(u32 @1, u64 @2) @2,
     Three {
         field1: u32 @1,
         field2: bool @2,
     } @3,
-}
-
+}

Sample enum.

@@ -194,15 +195,19 @@ enum SampleEnum {

Variants

  • -
    One
    + + One + @1 +
    -
    -
  • -
    Two
    + + Two + @2 +
    @@ -211,39 +216,52 @@ enum SampleEnum {
    • - 0: + n0: u32 @1 -
    • -

      Metadata

      -
      -

      The size range is:

      -
      -

      u32 1..5

      +
      +
      -
      +
      +

      Metadata

      +
      +

      The size range is:

      +
      +

      u32 1..5

      +
      +
      +
      +
    • - 1: + n1: u64 @2 -
    • -

      Metadata

      -
      -

      The size range is:

      -
      -

      u64 1..10

      +
      +
      -
      +
      +

      Metadata

      +
      +

      The size range is:

      +
      +

      u64 1..10

      +
      +
      +
      +
  • -
    Three
    + + Three + @3 +
    diff --git a/crates/stef-doc/tests/snapshots/render__render@struct_basic.stef.snap b/crates/stef-doc/tests/snapshots/render__render@struct_basic.stef.snap index 5e29b00..99fbf71 100644 --- a/crates/stef-doc/tests/snapshots/render__render@struct_basic.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@struct_basic.stef.snap @@ -85,13 +85,10 @@ input_file: crates/stef-parser/tests/inputs/struct_basic.stef Struct struct_basic::Sample -
    /// Basic struct.
    -struct Sample {
    +  
    struct Sample {
         a: u32 @1,
    -    /// Second field
         b: bool @2,
    -}
    -
    +}

    Basic struct.

    @@ -111,15 +108,17 @@ struct Sample { u32 @1 -
    +
    -

    Metadata

    -
    -

    The size range is:

    -
    -

    u32 1..5

    +
    +

    Metadata

    +
    +

    The size range is:

    +
    +

    u32 1..5

    +
  • @@ -129,19 +128,22 @@ struct Sample { bool @2 -
    +

    Second field

    -

    Metadata

    -
    -

    The size range is:

    -
    -

    bool 1

    +
    +

    Metadata

    +
    +

    The size range is:

    +
    +

    bool 1

    +
+ diff --git a/crates/stef-doc/tests/snapshots/render__render@struct_generics.stef.snap b/crates/stef-doc/tests/snapshots/render__render@struct_generics.stef.snap index 42a9d1a..412bc5c 100644 --- a/crates/stef-doc/tests/snapshots/render__render@struct_generics.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@struct_generics.stef.snap @@ -85,12 +85,10 @@ input_file: crates/stef-parser/tests/inputs/struct_generics.stef Struct struct_generics::KeyValue -
/// Generic key-value pair.
-struct KeyValue<K, V> {
+  
struct KeyValue<K, V> {
     key: K @1,
     value: V @2,
-}
-
+}

Generic key-value pair.

@@ -110,7 +108,7 @@ struct KeyValue<K, V> { K @1 -
+
@@ -120,11 +118,12 @@ struct KeyValue<K, V> { V @2 -
+
+
diff --git a/crates/stef-doc/tests/snapshots/render__render@struct_many_ws.stef.snap b/crates/stef-doc/tests/snapshots/render__render@struct_many_ws.stef.snap index da58ead..07d1d54 100644 --- a/crates/stef-doc/tests/snapshots/render__render@struct_many_ws.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@struct_many_ws.stef.snap @@ -85,13 +85,11 @@ input_file: crates/stef-parser/tests/inputs/struct_many_ws.stef Struct struct_many_ws::Sample -
/// Some comment
-struct Sample<T> {
+  
struct Sample<T> {
     a: u32 @1,
     b: bool @2,
     c: T @3,
-}
-
+}

Some comment

@@ -111,15 +109,17 @@ struct Sample<T> { u32 @1 -
+
-

Metadata

-
-

The size range is:

-
-

u32 1..5

+
+

Metadata

+
+

The size range is:

+
+

u32 1..5

+
@@ -129,15 +129,17 @@ struct Sample<T> { bool @2 -
+
-

Metadata

-
-

The size range is:

-
-

bool 1

+
+

Metadata

+
+

The size range is:

+
+

bool 1

+
@@ -147,11 +149,12 @@ struct Sample<T> { T @3 -
+
+
diff --git a/crates/stef-doc/tests/snapshots/render__render@struct_min_ws.stef.snap b/crates/stef-doc/tests/snapshots/render__render@struct_min_ws.stef.snap index fc95c36..413797c 100644 --- a/crates/stef-doc/tests/snapshots/render__render@struct_min_ws.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@struct_min_ws.stef.snap @@ -89,8 +89,7 @@ input_file: crates/stef-parser/tests/inputs/struct_min_ws.stef a: u32 @1, b: bool @2, c: T @3, -} - +}
@@ -110,15 +109,17 @@ input_file: crates/stef-parser/tests/inputs/struct_min_ws.stef u32 @1 -
+
-

Metadata

-
-

The size range is:

-
-

u32 1..5

+
+

Metadata

+
+

The size range is:

+
+

u32 1..5

+
@@ -128,15 +129,17 @@ input_file: crates/stef-parser/tests/inputs/struct_min_ws.stef bool @2 -
+
-

Metadata

-
-

The size range is:

-
-

bool 1

+
+

Metadata

+
+

The size range is:

+
+

bool 1

+
@@ -146,11 +149,12 @@ input_file: crates/stef-parser/tests/inputs/struct_min_ws.stef T @3 -
+
+
diff --git a/crates/stef-doc/tests/snapshots/render__render@struct_tuple.stef.snap b/crates/stef-doc/tests/snapshots/render__render@struct_tuple.stef.snap index 7a6fb58..4f2455e 100644 --- a/crates/stef-doc/tests/snapshots/render__render@struct_tuple.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@struct_tuple.stef.snap @@ -85,9 +85,7 @@ input_file: crates/stef-parser/tests/inputs/struct_tuple.stef Struct struct_tuple::Sample -
/// Basic struct.
-struct Sample(u32 @1, bool @2)
-
+
struct Sample(u32 @1, bool @2)

Basic struct.

@@ -103,35 +101,46 @@ struct Sample(u32 @1, bool @2)
  • - 0: + n0: u32 @1 -
  • -

    Metadata

    -
    -

    The size range is:

    -
    -

    u32 1..5

    +
    + +
    +
    +

    Metadata

    +
    +

    The size range is:

    +
    +

    u32 1..5

    +
    +
    -
    +
  • - 1: + n1: bool @2 -
  • -

    Metadata

    -
    -

    The size range is:

    -
    -

    bool 1

    +
    +
    -
    +
    +

    Metadata

    +
    +

    The size range is:

    +
    +

    bool 1

    +
    +
    +
    +
+
diff --git a/crates/stef-doc/tests/snapshots/render__render@types_basic.stef.snap b/crates/stef-doc/tests/snapshots/render__render@types_basic.stef.snap index f2ba73d..63122b4 100644 --- a/crates/stef-doc/tests/snapshots/render__render@types_basic.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@types_basic.stef.snap @@ -107,8 +107,7 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef f19: box<bytes> @19, f20: (u32, u32, u32) @20, f21: [u32; 12] @21, -} - +}
@@ -128,15 +127,17 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef bool @1 -
+
-

Metadata

-
-

The size range is:

-
-

bool 1

+
+

Metadata

+
+

The size range is:

+
+

bool 1

+
@@ -146,15 +147,17 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef u8 @2 -
+
-

Metadata

-
-

The size range is:

-
-

u8 1

+
+

Metadata

+
+

The size range is:

+
+

u8 1

+
@@ -164,15 +167,17 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef u16 @3 -
+
-

Metadata

-
-

The size range is:

-
-

u16 1..3

+
+

Metadata

+
+

The size range is:

+
+

u16 1..3

+
@@ -182,15 +187,17 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef u32 @4 -
+
-

Metadata

-
-

The size range is:

-
-

u32 1..5

+
+

Metadata

+
+

The size range is:

+
+

u32 1..5

+
@@ -200,15 +207,17 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef u64 @5 -
+
-

Metadata

-
-

The size range is:

-
-

u64 1..10

+
+

Metadata

+
+

The size range is:

+
+

u64 1..10

+
@@ -218,15 +227,17 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef u128 @6 -
+
-

Metadata

-
-

The size range is:

-
-

u128 1..19

+
+

Metadata

+
+

The size range is:

+
+

u128 1..19

+
@@ -236,15 +247,17 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef i8 @7 -
+
-

Metadata

-
-

The size range is:

-
-

i8 1

+
+

Metadata

+
+

The size range is:

+
+

i8 1

+
@@ -254,15 +267,17 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef i16 @8 -
+
-

Metadata

-
-

The size range is:

-
-

i16 1..3

+
+

Metadata

+
+

The size range is:

+
+

i16 1..3

+
@@ -272,15 +287,17 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef i32 @9 -
+
-

Metadata

-
-

The size range is:

-
-

i32 1..5

+
+

Metadata

+
+

The size range is:

+
+

i32 1..5

+
@@ -290,15 +307,17 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef i64 @10 -
+
-

Metadata

-
-

The size range is:

-
-

i64 1..10

+
+

Metadata

+
+

The size range is:

+
+

i64 1..10

+
@@ -308,15 +327,17 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef i128 @11 -
+
-

Metadata

-
-

The size range is:

-
-

i128 1..19

+
+

Metadata

+
+

The size range is:

+
+

i128 1..19

+
@@ -326,15 +347,17 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef f32 @12 -
+
-

Metadata

-
-

The size range is:

-
-

f32 4

+
+

Metadata

+
+

The size range is:

+
+

f32 4

+
@@ -344,15 +367,17 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef f64 @13 -
+
-

Metadata

-
-

The size range is:

-
-

f64 8

+
+

Metadata

+
+

The size range is:

+
+

f64 8

+
@@ -362,15 +387,17 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef string @14 -
+
-

Metadata

-
-

The size range is:

-
-

string 1..

+
+

Metadata

+
+

The size range is:

+
+

string 1..

+
@@ -380,15 +407,17 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef &string @15 -
+
-

Metadata

-
-

The size range is:

-
-

&string 1..

+
+

Metadata

+
+

The size range is:

+
+

&string 1..

+
@@ -398,15 +427,17 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef bytes @16 -
+
-

Metadata

-
-

The size range is:

-
-

bytes 1..

+
+

Metadata

+
+

The size range is:

+
+

bytes 1..

+
@@ -416,15 +447,17 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef &bytes @17 -
+
-

Metadata

-
-

The size range is:

-
-

&bytes 1..

+
+

Metadata

+
+

The size range is:

+
+

&bytes 1..

+
@@ -434,15 +467,17 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef box<string> @18 -
+
-

Metadata

-
-

The size range is:

-
-

box<string> 1..

+
+

Metadata

+
+

The size range is:

+
+

box<string> 1..

+
@@ -452,15 +487,17 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef box<bytes> @19 -
+
-

Metadata

-
-

The size range is:

-
-

box<bytes> 1..

+
+

Metadata

+
+

The size range is:

+
+

box<bytes> 1..

+
@@ -470,20 +507,22 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef (u32, u32, u32) @20 -
+
-

Metadata

-
-

The size range is:

-
-

tuple 3..15

+
+

Metadata

+
+

The size range is:

+
+

tuple 3..15

  • 0: u32 1..5
  • 1: u32 1..5
  • 2: u32 1..5
+
@@ -493,23 +532,26 @@ input_file: crates/stef-parser/tests/inputs/types_basic.stef [u32; 12] @21 -
+
-

Metadata

-
-

The size range is:

-
-

array 13..61

+
+

Metadata

+
+

The size range is:

+
+

array 13..61

  • length: u64 1
  • element: u32 1..5
+
+
diff --git a/crates/stef-doc/tests/snapshots/render__render@types_generic.stef.snap b/crates/stef-doc/tests/snapshots/render__render@types_generic.stef.snap index 1d1c91d..133560b 100644 --- a/crates/stef-doc/tests/snapshots/render__render@types_generic.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@types_generic.stef.snap @@ -99,8 +99,7 @@ input_file: crates/stef-parser/tests/inputs/types_generic.stef f3: hash_set<u32> @3, f4: option<u32> @4, f5: non_zero<u32> @5, -} - +}
@@ -120,19 +119,21 @@ input_file: crates/stef-parser/tests/inputs/types_generic.stef vec<u32> @1 -
+
-

Metadata

-
-

The size range is:

-
-

vec 1..

+
+

Metadata

+
+

The size range is:

+
+

vec 1..

  • length: u64 1..10
  • element: u32 1..5
+
@@ -142,20 +143,22 @@ input_file: crates/stef-parser/tests/inputs/types_generic.stef hash_map<u32, string> @2 -
+
-

Metadata

-
-

The size range is:

-
-

hash_map 1..

+
+

Metadata

+
+

The size range is:

+
+

hash_map 1..

  • length: u64 1..10
  • key: u32 1..5
  • value: string 1..
+
@@ -165,19 +168,21 @@ input_file: crates/stef-parser/tests/inputs/types_generic.stef hash_set<u32> @3 -
+
-

Metadata

-
-

The size range is:

-
-

hash_set 1..

+
+

Metadata

+
+

The size range is:

+
+

hash_set 1..

  • length: u64 1..10
  • element: u32 1..5
+
@@ -187,18 +192,20 @@ input_file: crates/stef-parser/tests/inputs/types_generic.stef option<u32> @4 -
+
-

Metadata

-
-

The size range is:

-
-

option 0..6

+
+

Metadata

+
+

The size range is:

+
+

option 0..6

  • value: u32 1..5
+
@@ -208,22 +215,25 @@ input_file: crates/stef-parser/tests/inputs/types_generic.stef non_zero<u32> @5 -
+
-

Metadata

-
-

The size range is:

-
-

non_zero 0..6

+
+

Metadata

+
+

The size range is:

+
+

non_zero 0..6

  • value: u32 1..5
+
+
@@ -251,8 +261,7 @@ input_file: crates/stef-parser/tests/inputs/types_generic.stef Struct types_generic::SampleUnnamed -
struct SampleUnnamed(vec<u32> @1, hash_map<u32, string> @2, hash_set<u32> @3, option<u32> @4, non_zero<u32> @5)
-
+
struct SampleUnnamed(vec<u32> @1, hash_map<u32, string> @2, hash_set<u32> @3, option<u32> @4, non_zero<u32> @5)
@@ -268,99 +277,125 @@ input_file: crates/stef-parser/tests/inputs/types_generic.stef
  • - 0: + n0: vec<u32> @1 -
  • -

    Metadata

    -
    -

    The size range is:

    -
    -

    vec 1..

    -
      -
    • length: u64 1..10
    • -
    • element: u32 1..5
    • -
    +
    + +
    +
    +

    Metadata

    +
    +

    The size range is:

    +
    +

    vec 1..

    +
      +
    • length: u64 1..10
    • +
    • element: u32 1..5
    • +
    +
    +
    -
    +
  • - 1: + n1: hash_map<u32, string> @2 -
  • -

    Metadata

    -
    -

    The size range is:

    -
    -

    hash_map 1..

    -
      -
    • length: u64 1..10
    • -
    • key: u32 1..5
    • -
    • value: string 1..
    • -
    +
    +
    -
    +
    +

    Metadata

    +
    +

    The size range is:

    +
    +

    hash_map 1..

    +
      +
    • length: u64 1..10
    • +
    • key: u32 1..5
    • +
    • value: string 1..
    • +
    +
    +
    +
    +
  • - 2: + n2: hash_set<u32> @3 -
  • -

    Metadata

    -
    -

    The size range is:

    -
    -

    hash_set 1..

    -
      -
    • length: u64 1..10
    • -
    • element: u32 1..5
    • -
    +
    + +
    +
    +

    Metadata

    +
    +

    The size range is:

    +
    +

    hash_set 1..

    +
      +
    • length: u64 1..10
    • +
    • element: u32 1..5
    • +
    +
    +
    -
    +
  • - 3: + n3: option<u32> @4 -
  • -

    Metadata

    -
    -

    The size range is:

    -
    -

    option 0..6

    -
      -
    • value: u32 1..5
    • -
    +
    +
    -
    +
    +

    Metadata

    +
    +

    The size range is:

    +
    +

    option 0..6

    +
      +
    • value: u32 1..5
    • +
    +
    +
    +
    +
  • - 4: + n4: non_zero<u32> @5 -
  • -

    Metadata

    -
    -

    The size range is:

    -
    -

    non_zero 0..6

    -
      -
    • value: u32 1..5
    • -
    +
    +
    -
    +
    +

    Metadata

    +
    +

    The size range is:

    +
    +

    non_zero 0..6

    +
      +
    • value: u32 1..5
    • +
    +
    +
    +
    +
+
diff --git a/crates/stef-doc/tests/snapshots/render__render@types_nested.stef.snap b/crates/stef-doc/tests/snapshots/render__render@types_nested.stef.snap index e84c850..7c3773c 100644 --- a/crates/stef-doc/tests/snapshots/render__render@types_nested.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@types_nested.stef.snap @@ -87,8 +87,7 @@ input_file: crates/stef-parser/tests/inputs/types_nested.stef
struct Sample {
     value: vec<option<non_zero<hash_map<i64, box<string>>>>> @1,
-}
-
+}
@@ -108,15 +107,16 @@ input_file: crates/stef-parser/tests/inputs/types_nested.stef vec<option<non_zero<hash_map<i64, box<string>>>>> @1 -
+
-

Metadata

-
-

The size range is:

-
-

vec 1..

+
+

Metadata

+
+

The size range is:

+
+

vec 1..

  • length: u64 1..10
  • element: option 0.. @@ -135,10 +135,12 @@ input_file: crates/stef-parser/tests/inputs/types_nested.stef
+
+
diff --git a/crates/stef-doc/tests/snapshots/render__render@types_non_zero.stef.snap b/crates/stef-doc/tests/snapshots/render__render@types_non_zero.stef.snap index a7c7b72..2255a5b 100644 --- a/crates/stef-doc/tests/snapshots/render__render@types_non_zero.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@types_non_zero.stef.snap @@ -101,8 +101,7 @@ input_file: crates/stef-parser/tests/inputs/types_non_zero.stef f13: non_zero<vec<string>> @13, f14: non_zero<hash_map<string, bytes>> @14, f15: non_zero<hash_set<string>> @15, -} - +}
@@ -122,18 +121,20 @@ input_file: crates/stef-parser/tests/inputs/types_non_zero.stef non_zero<u8> @1 -
+
-

Metadata

-
-

The size range is:

-
-

non_zero 0..2

+
+

Metadata

+
+

The size range is:

+
+

non_zero 0..2

  • value: u8 1
+
@@ -143,18 +144,20 @@ input_file: crates/stef-parser/tests/inputs/types_non_zero.stef non_zero<u16> @2 -
+
-

Metadata

-
-

The size range is:

-
-

non_zero 0..4

+
+

Metadata

+
+

The size range is:

+
+

non_zero 0..4

  • value: u16 1..3
+
@@ -164,18 +167,20 @@ input_file: crates/stef-parser/tests/inputs/types_non_zero.stef non_zero<u32> @3 -
+
-

Metadata

-
-

The size range is:

-
-

non_zero 0..6

+
+

Metadata

+
+

The size range is:

+
+

non_zero 0..6

  • value: u32 1..5
+
@@ -185,18 +190,20 @@ input_file: crates/stef-parser/tests/inputs/types_non_zero.stef non_zero<u64> @4 -
+
-

Metadata

-
-

The size range is:

-
-

non_zero 0..11

+
+

Metadata

+
+

The size range is:

+
+

non_zero 0..11

  • value: u64 1..10
+
@@ -206,18 +213,20 @@ input_file: crates/stef-parser/tests/inputs/types_non_zero.stef non_zero<u128> @5 -
+
-

Metadata

-
-

The size range is:

-
-

non_zero 0..20

+
+

Metadata

+
+

The size range is:

+
+

non_zero 0..20

  • value: u128 1..19
+
@@ -227,18 +236,20 @@ input_file: crates/stef-parser/tests/inputs/types_non_zero.stef non_zero<i8> @6 -
+
-

Metadata

-
-

The size range is:

-
-

non_zero 0..2

+
+

Metadata

+
+

The size range is:

+
+

non_zero 0..2

  • value: i8 1
+
@@ -248,18 +259,20 @@ input_file: crates/stef-parser/tests/inputs/types_non_zero.stef non_zero<i16> @7 -
+
-

Metadata

-
-

The size range is:

-
-

non_zero 0..4

+
+

Metadata

+
+

The size range is:

+
+

non_zero 0..4

  • value: i16 1..3
+
@@ -269,18 +282,20 @@ input_file: crates/stef-parser/tests/inputs/types_non_zero.stef non_zero<i32> @8 -
+
-

Metadata

-
-

The size range is:

-
-

non_zero 0..6

+
+

Metadata

+
+

The size range is:

+
+

non_zero 0..6

  • value: i32 1..5
+
@@ -290,18 +305,20 @@ input_file: crates/stef-parser/tests/inputs/types_non_zero.stef non_zero<i64> @9 -
+
-

Metadata

-
-

The size range is:

-
-

non_zero 0..11

+
+

Metadata

+
+

The size range is:

+
+

non_zero 0..11

  • value: i64 1..10
+
@@ -311,18 +328,20 @@ input_file: crates/stef-parser/tests/inputs/types_non_zero.stef non_zero<i128> @10 -
+
-

Metadata

-
-

The size range is:

-
-

non_zero 0..20

+
+

Metadata

+
+

The size range is:

+
+

non_zero 0..20

  • value: i128 1..19
+
@@ -332,18 +351,20 @@ input_file: crates/stef-parser/tests/inputs/types_non_zero.stef non_zero<string> @11 -
+
-

Metadata

-
-

The size range is:

-
-

non_zero 0..

+
+

Metadata

+
+

The size range is:

+
+

non_zero 0..

  • value: string 1..
+
@@ -353,18 +374,20 @@ input_file: crates/stef-parser/tests/inputs/types_non_zero.stef non_zero<bytes> @12 -
+
-

Metadata

-
-

The size range is:

-
-

non_zero 0..

+
+

Metadata

+
+

The size range is:

+
+

non_zero 0..

  • value: bytes 1..
+
@@ -374,15 +397,16 @@ input_file: crates/stef-parser/tests/inputs/types_non_zero.stef non_zero<vec<string>> @13 -
+
-

Metadata

-
-

The size range is:

-
-

non_zero 0..

+
+

Metadata

+
+

The size range is:

+
+

non_zero 0..

  • value: vec 1..
      @@ -391,6 +415,7 @@ input_file: crates/stef-parser/tests/inputs/types_non_zero.stef
+
@@ -400,15 +425,16 @@ input_file: crates/stef-parser/tests/inputs/types_non_zero.stef non_zero<hash_map<string, bytes>> @14 -
+
-

Metadata

-
-

The size range is:

-
-

non_zero 0..

+
+

Metadata

+
+

The size range is:

+
+

non_zero 0..

  • value: hash_map 1..
      @@ -418,6 +444,7 @@ input_file: crates/stef-parser/tests/inputs/types_non_zero.stef
+
@@ -427,15 +454,16 @@ input_file: crates/stef-parser/tests/inputs/types_non_zero.stef non_zero<hash_set<string>> @15 -
+
-

Metadata

-
-

The size range is:

-
-

non_zero 0..

+
+

Metadata

+
+

The size range is:

+
+

non_zero 0..

  • value: hash_set 1..
      @@ -444,10 +472,12 @@ input_file: crates/stef-parser/tests/inputs/types_non_zero.stef
+
+
diff --git a/crates/stef-doc/tests/snapshots/render__render@types_ref.stef.snap b/crates/stef-doc/tests/snapshots/render__render@types_ref.stef.snap index f1be410..11cf4e6 100644 --- a/crates/stef-doc/tests/snapshots/render__render@types_ref.stef.snap +++ b/crates/stef-doc/tests/snapshots/render__render@types_ref.stef.snap @@ -104,8 +104,7 @@ input_file: crates/stef-parser/tests/inputs/types_ref.stef
struct Sample {
     basic: Test123 @1,
     with_generics: KeyValue<u32, bool> @2,
-}
-
+}
@@ -125,7 +124,7 @@ input_file: crates/stef-parser/tests/inputs/types_ref.stef Test123 @1 -
+
@@ -135,11 +134,12 @@ input_file: crates/stef-parser/tests/inputs/types_ref.stef KeyValue<u32, bool> @2 -
+
+
@@ -169,8 +169,7 @@ input_file: crates/stef-parser/tests/inputs/types_ref.stef
enum Test123 {
     Value @1,
-}
-
+}
@@ -179,12 +178,13 @@ input_file: crates/stef-parser/tests/inputs/types_ref.stef

Variants

  • -
    Value
    + + Value + @1 +
    -
    -
@@ -218,8 +218,7 @@ input_file: crates/stef-parser/tests/inputs/types_ref.stef
struct KeyValue<K, V> {
     key: K @1,
     value: V @2,
-}
-
+}
@@ -239,7 +238,7 @@ input_file: crates/stef-parser/tests/inputs/types_ref.stef K @1 -
+
@@ -249,11 +248,12 @@ input_file: crates/stef-parser/tests/inputs/types_ref.stef V @2 -
+
+
diff --git a/crates/stef-lsp/src/handlers/compile.rs b/crates/stef-lsp/src/handlers/compile.rs index 033a3cc..b5c4baf 100644 --- a/crates/stef-lsp/src/handlers/compile.rs +++ b/crates/stef-lsp/src/handlers/compile.rs @@ -17,7 +17,7 @@ pub fn compile<'a>( file: Url, schema: &'a str, index: &'_ LineIndex, -) -> std::result::Result, Diagnostic> { +) -> Result, Diagnostic> { let parsed = stef_parser::Schema::parse(schema, None).map_err(|e| parse_schema_diagnostic(index, &e))?; @@ -27,6 +27,12 @@ pub fn compile<'a>( Ok(parsed) } +pub fn simplify<'a>( + result: &'a Result, Diagnostic>, +) -> Result, &'a Diagnostic> { + result.as_ref().map(stef_compiler::simplify_schema) +} + fn parse_schema_diagnostic(index: &LineIndex, e: &ParseSchemaError) -> Diagnostic { match &e.cause { ParseSchemaCause::Parser(_, at) => { diff --git a/crates/stef-lsp/src/handlers/hover.rs b/crates/stef-lsp/src/handlers/hover.rs index a1c9e81..1d5afe7 100644 --- a/crates/stef-lsp/src/handlers/hover.rs +++ b/crates/stef-lsp/src/handlers/hover.rs @@ -3,10 +3,10 @@ use std::{fmt::Write, ops::Range}; use anyhow::{Context, Result}; use line_index::{LineIndex, TextSize, WideLineCol}; use lsp_types::{Position, Range as LspRange}; -use stef_parser::{ - Comment, Const, Definition, Enum, Fields, Module, NamedField, Schema, Span, Spanned, Struct, - TypeAlias, Variant, +use stef_compiler::simplify::{ + Const, Definition, Enum, Field, Fields, Module, ParserField, Schema, Struct, TypeAlias, Variant, }; +use stef_parser::{Span, Spanned}; pub fn visit_schema( index: &LineIndex, @@ -47,8 +47,8 @@ fn visit_definition(item: &Definition<'_>, position: usize) -> Option<(String, S } fn visit_module(item: &Module<'_>, position: usize) -> Option<(String, Span)> { - (Range::from(item.name.span()).contains(&position)) - .then(|| (fold_comment(&item.comment), item.name.span())) + (Range::from(item.source.name.span()).contains(&position)) + .then(|| (fold_comment(&item.comment), item.source.name.span())) .or_else(|| { item.definitions .iter() @@ -57,7 +57,7 @@ fn visit_module(item: &Module<'_>, position: usize) -> Option<(String, Span)> { } fn visit_struct(item: &Struct<'_>, position: usize) -> Option<(String, Span)> { - (Range::from(item.name.span()).contains(&position)) + (Range::from(item.source.name.span()).contains(&position)) .then(|| { let mut text = fold_comment(&item.comment); @@ -65,13 +65,13 @@ fn visit_struct(item: &Struct<'_>, position: usize) -> Option<(String, Span)> { let _ = writeln!(&mut text, "- next ID: `{next_id}`"); } - (text, item.name.span()) + (text, item.source.name.span()) }) .or_else(|| visit_fields(&item.fields, position)) } fn visit_enum(item: &Enum<'_>, position: usize) -> Option<(String, Span)> { - (Range::from(item.name.span()).contains(&position)) + (Range::from(item.source.name.span()).contains(&position)) .then(|| { let mut text = fold_comment(&item.comment); @@ -81,7 +81,7 @@ fn visit_enum(item: &Enum<'_>, position: usize) -> Option<(String, Span)> { stef_meta::next_variant_id(&item.variants) ); - (text, item.name.span()) + (text, item.source.name.span()) }) .or_else(|| { item.variants @@ -91,7 +91,7 @@ fn visit_enum(item: &Enum<'_>, position: usize) -> Option<(String, Span)> { } fn visit_variant(item: &Variant<'_>, position: usize) -> Option<(String, Span)> { - (Range::from(item.name.span()).contains(&position)) + (Range::from(item.source.name.span()).contains(&position)) .then(|| { let mut text = fold_comment(&item.comment); @@ -99,49 +99,49 @@ fn visit_variant(item: &Variant<'_>, position: usize) -> Option<(String, Span)> let _ = writeln!(&mut text, "- next ID: `{next_id}`"); } - (text, item.name.span()) + (text, item.source.name.span()) }) .or_else(|| visit_fields(&item.fields, position)) } fn visit_fields(item: &Fields<'_>, position: usize) -> Option<(String, Span)> { - if let Fields::Named(named) = item { - named - .iter() - .find_map(|field| visit_named_field(field, position)) - } else { - None - } + item.fields + .iter() + .find_map(|field| visit_named_field(field, position)) } -fn visit_named_field(item: &NamedField<'_>, position: usize) -> Option<(String, Span)> { - (Range::from(item.name.span()).contains(&position)).then(|| { +fn visit_named_field(item: &Field<'_>, position: usize) -> Option<(String, Span)> { + let ParserField::Named(field) = item.source else { + return None; + }; + + (Range::from(field.name.span()).contains(&position)).then(|| { let mut text = fold_comment(&item.comment); let _ = write!(&mut text, "### Wire size\n\n"); - if let Some(size) = stef_meta::wire_size(&item.ty.value) { + if let Some(size) = stef_meta::wire_size(&item.ty) { size.print(&mut text, 0); } else { let _ = write!(&mut text, "_unknown_"); } - (text, item.name.span()) + (text, field.name.span()) }) } fn visit_alias(item: &TypeAlias<'_>, position: usize) -> Option<(String, Span)> { - (Range::from(item.name.span()).contains(&position)) - .then(|| (fold_comment(&item.comment), item.name.span())) + (Range::from(item.source.name.span()).contains(&position)) + .then(|| (fold_comment(&item.comment), item.source.name.span())) } fn visit_const(item: &Const<'_>, position: usize) -> Option<(String, Span)> { - (Range::from(item.name.span()).contains(&position)) - .then(|| (fold_comment(&item.comment), item.name.span())) + (Range::from(item.source.name.span()).contains(&position)) + .then(|| (fold_comment(&item.comment), item.source.name.span())) } -fn fold_comment(comment: &Comment<'_>) -> String { - comment.0.iter().fold(String::new(), |mut acc, line| { - acc.push_str(line.value); +fn fold_comment(comment: &[&str]) -> String { + comment.iter().fold(String::new(), |mut acc, line| { + acc.push_str(line); acc.push('\n'); acc }) diff --git a/crates/stef-lsp/src/handlers/mod.rs b/crates/stef-lsp/src/handlers/mod.rs index b78751c..620584e 100644 --- a/crates/stef-lsp/src/handlers/mod.rs +++ b/crates/stef-lsp/src/handlers/mod.rs @@ -223,7 +223,7 @@ pub fn hover(state: &mut GlobalState<'_>, params: HoverParams) -> Result state::File { index: LineIndex::new(&text), content: text, schema_builder: |index, schema| compile::compile(uri, schema, index), + simplified_builder: compile::simplify, } .build() } @@ -357,6 +358,7 @@ fn update_file( index: LineIndex::new(&text), content: text, schema_builder: |index, schema| compile::compile(uri, schema, index), + simplified_builder: compile::simplify, } .build() } diff --git a/crates/stef-lsp/src/state.rs b/crates/stef-lsp/src/state.rs index ba3c25b..4ad6deb 100644 --- a/crates/stef-lsp/src/state.rs +++ b/crates/stef-lsp/src/state.rs @@ -18,7 +18,6 @@ pub struct GlobalState<'a> { } #[self_referencing(pub_extras)] -#[derive(Debug)] pub struct File { rope: Rope, pub index: LineIndex, @@ -26,6 +25,9 @@ pub struct File { #[borrows(index, content)] #[covariant] pub schema: Result, Diagnostic>, + #[borrows(schema)] + #[covariant] + pub simplified: Result, &'this Diagnostic>, } impl GlobalState<'_> { diff --git a/crates/stef-meta/Cargo.toml b/crates/stef-meta/Cargo.toml index 8fc53fb..0f88adf 100644 --- a/crates/stef-meta/Cargo.toml +++ b/crates/stef-meta/Cargo.toml @@ -10,7 +10,7 @@ repository.workspace = true license.workspace = true [dependencies] -stef-parser = { path = "../stef-parser" } +stef-compiler = { path = "../stef-compiler" } [lints] workspace = true diff --git a/crates/stef-meta/src/lib.rs b/crates/stef-meta/src/lib.rs index d6696d6..d34980d 100644 --- a/crates/stef-meta/src/lib.rs +++ b/crates/stef-meta/src/lib.rs @@ -5,35 +5,29 @@ use std::{borrow::Cow, fmt::Write}; -use stef_parser::{DataType, Fields, Type, Variant}; +use stef_compiler::simplify::{FieldKind, Fields, Type, Variant}; /// Get the next free ID for an enum variant. #[must_use] pub fn next_variant_id(variants: &[Variant<'_>]) -> u32 { - variants - .iter() - .map(|variant| variant.id.get()) - .max() - .unwrap_or(0) - + 1 + variants.iter().map(|variant| variant.id).max().unwrap_or(0) + 1 } /// Get the next free ID for a struct or enum variant field. #[must_use] pub fn next_field_id(fields: &Fields<'_>) -> Option { - match fields { - Fields::Named(named) => { - Some(named.iter().map(|field| field.id.get()).max().unwrap_or(0) + 1) - } - Fields::Unnamed(unnamed) => Some( - unnamed + if fields.kind == FieldKind::Unit { + None + } else { + Some( + fields + .fields .iter() - .map(|field| field.id.get()) + .map(|field| field.id) .max() .unwrap_or(0) + 1, - ), - Fields::Unit => None, + ) } } @@ -99,55 +93,55 @@ impl WireSize { /// The resulting size can have various forms of precision, depending on the data type. For example, /// it could be of fixed size, inside known bounds or even unknown. #[must_use] -pub fn wire_size(ty: &DataType<'_>) -> Option { +pub fn wire_size(ty: &Type<'_>) -> Option { Some(match ty { - DataType::Bool => WireSize::fixed("bool", 1), - DataType::U8 => WireSize::fixed("u8", 1), - DataType::I8 => WireSize::fixed("i8", 1), - DataType::U16 => WireSize::range("u16", 1, 3), - DataType::I16 => WireSize::range("i16", 1, 3), - DataType::U32 => WireSize::range("u32", 1, 5), - DataType::I32 => WireSize::range("i32", 1, 5), - DataType::U64 => WireSize::range("u64", 1, 10), - DataType::I64 => WireSize::range("i64", 1, 10), - DataType::U128 => WireSize::range("u128", 1, 19), - DataType::I128 => WireSize::range("i128", 1, 19), - DataType::F32 => WireSize::fixed("f32", 4), - DataType::F64 => WireSize::fixed("f64", 8), - DataType::String => WireSize::min("string", 1), - DataType::StringRef => WireSize::min("&string", 1), - DataType::Bytes => WireSize::min("bytes", 1), - DataType::BytesRef => WireSize::min("&bytes", 1), - DataType::Vec(ty) => WireSize { + Type::Bool => WireSize::fixed("bool", 1), + Type::U8 => WireSize::fixed("u8", 1), + Type::I8 => WireSize::fixed("i8", 1), + Type::U16 => WireSize::range("u16", 1, 3), + Type::I16 => WireSize::range("i16", 1, 3), + Type::U32 => WireSize::range("u32", 1, 5), + Type::I32 => WireSize::range("i32", 1, 5), + Type::U64 => WireSize::range("u64", 1, 10), + Type::I64 => WireSize::range("i64", 1, 10), + Type::U128 => WireSize::range("u128", 1, 19), + Type::I128 => WireSize::range("i128", 1, 19), + Type::F32 => WireSize::fixed("f32", 4), + Type::F64 => WireSize::fixed("f64", 8), + Type::String => WireSize::min("string", 1), + Type::StringRef => WireSize::min("&string", 1), + Type::Bytes => WireSize::min("bytes", 1), + Type::BytesRef => WireSize::min("&bytes", 1), + Type::Vec(ty) => WireSize { label: "vec".into(), min: 1, max: None, inner: vec![ - ("length".into(), wire_size(&DataType::U64)), - ("element".into(), wire_size(&ty.value)), + ("length".into(), wire_size(&Type::U64)), + ("element".into(), wire_size(ty)), ], }, - DataType::HashMap(kv) => WireSize { + Type::HashMap(kv) => WireSize { label: "hash_map".into(), min: 1, max: None, inner: vec![ - ("length".into(), wire_size(&DataType::U64)), - ("key".into(), wire_size(&kv.0.value)), - ("value".into(), wire_size(&kv.1.value)), + ("length".into(), wire_size(&Type::U64)), + ("key".into(), wire_size(&kv.0)), + ("value".into(), wire_size(&kv.1)), ], }, - DataType::HashSet(ty) => WireSize { + Type::HashSet(ty) => WireSize { label: "hash_set".into(), min: 1, max: None, inner: vec![ - ("length".into(), wire_size(&DataType::U64)), - ("element".into(), wire_size(&ty.value)), + ("length".into(), wire_size(&Type::U64)), + ("element".into(), wire_size(ty)), ], }, - DataType::Option(ty) => { - let inner = wire_size(&ty.value); + Type::Option(ty) => { + let inner = wire_size(ty); WireSize { label: "option".into(), min: 0, @@ -155,8 +149,8 @@ pub fn wire_size(ty: &DataType<'_>) -> Option { inner: vec![("value".into(), inner)], } } - DataType::NonZero(ty) => { - let inner = wire_size(&ty.value); + Type::NonZero(ty) => { + let inner = wire_size(ty); WireSize { label: "non_zero".into(), min: 0, @@ -164,17 +158,17 @@ pub fn wire_size(ty: &DataType<'_>) -> Option { inner: vec![("value".into(), inner)], } } - DataType::BoxString => WireSize::min("box", 1), - DataType::BoxBytes => WireSize::min("box", 1), - DataType::Array(ty, size) => wire_size_array(ty, *size), - DataType::Tuple(types) => wire_size_tuple(types), - DataType::External(_) => return None, + Type::BoxString => WireSize::min("box", 1), + Type::BoxBytes => WireSize::min("box", 1), + Type::Array(ty, size) => wire_size_array(ty, *size), + Type::Tuple(types) => wire_size_tuple(types), + Type::External(_) => return None, }) } fn wire_size_array(ty: &Type<'_>, size: u32) -> WireSize { let length = varint_size(size); - let inner = wire_size(&ty.value); + let inner = wire_size(ty); WireSize { label: "array".into(), @@ -194,7 +188,7 @@ fn wire_size_tuple(types: &[Type<'_>]) -> WireSize { let inner = types .iter() .enumerate() - .map(|(i, ty)| (i.to_string().into(), wire_size(&ty.value))) + .map(|(i, ty)| (i.to_string().into(), wire_size(ty))) .collect::>(); WireSize {