Skip to content

Commit

Permalink
refactor: move doc, lsp and meta over to the compiler structs
Browse files Browse the repository at this point in the history
These crates were still working directly on top of the `stef_parser`
crate's types, but can get the same benefits as the other crates by
depending on the `stef_compiler`'s simplified structs instead.
  • Loading branch information
dnaka91 committed Dec 29, 2023
1 parent 15321f3 commit facee4a
Show file tree
Hide file tree
Showing 45 changed files with 1,457 additions and 998 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/stef-build/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
5 changes: 5 additions & 0 deletions crates/stef-build/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ fn compile_module(
comment,
name,
definitions,
..
}: &Module<'_>,
) -> TokenStream {
let comment = compile_comment(comment);
Expand All @@ -86,6 +87,7 @@ fn compile_struct(
name,
generics,
fields,
..
}: &Struct<'_>,
) -> TokenStream {
let comment = compile_comment(comment);
Expand All @@ -109,6 +111,7 @@ fn compile_enum(
name,
generics,
variants,
..
}: &Enum<'_>,
) -> TokenStream {
let comment = compile_comment(comment);
Expand Down Expand Up @@ -152,6 +155,7 @@ fn compile_alias(
name,
generics,
target,
..
}: &TypeAlias<'_>,
) -> TokenStream {
let comment = compile_comment(comment);
Expand All @@ -172,6 +176,7 @@ fn compile_const(
name,
ty,
value,
..
}: &Const<'_>,
) -> TokenStream {
let comment = compile_comment(comment);
Expand Down
10 changes: 3 additions & 7 deletions crates/stef-build/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions crates/stef-build/src/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand Down
51 changes: 51 additions & 0 deletions crates/stef-compiler/src/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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).
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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))]
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
}
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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,
},
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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()),
}
Expand Down
3 changes: 2 additions & 1 deletion crates/stef-doc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions crates/stef-doc/examples/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down
8 changes: 8 additions & 0 deletions crates/stef-doc/input.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit facee4a

Please sign in to comment.