From 7576d3bc61dc3b1aefd684d92092984127dd51ab Mon Sep 17 00:00:00 2001 From: Malo <57839069+MDLC01@users.noreply.github.com> Date: Thu, 29 May 2025 21:05:29 +0100 Subject: [PATCH] Make `#[synthesized]` fields `#[internal]` --- crates/typst-macros/src/elem.rs | 15 +++++++-------- crates/typst-macros/src/lib.rs | 4 +++- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/crates/typst-macros/src/elem.rs b/crates/typst-macros/src/elem.rs index 67fe7ed6a..cf996b2f8 100644 --- a/crates/typst-macros/src/elem.rs +++ b/crates/typst-macros/src/elem.rs @@ -78,9 +78,7 @@ impl Elem { /// Fields that show up in the documentation. fn doc_fields(&self) -> impl Iterator + Clone { - self.fields - .iter() - .filter(|field| !field.internal && !field.synthesized) + self.fields.iter().filter(|field| !field.internal) } /// Fields that are relevant for `Construct` impl. @@ -89,9 +87,8 @@ impl Elem { /// because it's a pattern used a lot for parsing data from the input and /// then storing it in a field. fn construct_fields(&self) -> impl Iterator + Clone { - self.real_fields().filter(|field| { - field.parse.is_some() || (!field.synthesized && !field.internal) - }) + self.real_fields() + .filter(|field| field.parse.is_some() || !field.internal) } /// Fields that can be configured with set rules. @@ -242,6 +239,8 @@ fn parse_field(field: &syn::Field) -> Result { let variadic = has_attr(&mut attrs, "variadic"); let required = has_attr(&mut attrs, "required") || variadic; let positional = has_attr(&mut attrs, "positional") || required; + let synthesized = has_attr(&mut attrs, "synthesized"); + let internal = has_attr(&mut attrs, "internal") || synthesized; let mut field = Field { ident: ident.clone(), @@ -261,11 +260,11 @@ fn parse_field(field: &syn::Field) -> Result { variadic, resolve: has_attr(&mut attrs, "resolve"), fold: has_attr(&mut attrs, "fold"), - internal: has_attr(&mut attrs, "internal"), + internal, external: has_attr(&mut attrs, "external"), borrowed: has_attr(&mut attrs, "borrowed"), ghost: has_attr(&mut attrs, "ghost"), - synthesized: has_attr(&mut attrs, "synthesized"), + synthesized, parse: parse_attr(&mut attrs, "parse")?.flatten(), default: parse_attr::(&mut attrs, "default")?.flatten(), }; diff --git a/crates/typst-macros/src/lib.rs b/crates/typst-macros/src/lib.rs index 82e63ddc8..fdf707647 100644 --- a/crates/typst-macros/src/lib.rs +++ b/crates/typst-macros/src/lib.rs @@ -201,7 +201,9 @@ pub fn ty(stream: BoundaryStream, item: BoundaryStream) -> BoundaryStream { /// flexibility. /// - `#[synthesized]`: The field cannot be specified in a constructor or set /// rule. Instead, it is added to an element before its show rule runs -/// through the `Synthesize` trait. +/// through the `Synthesize` trait. This implies `#[internal]`. If a +/// synthesized field needs to be exposed to the user, that should be done via +/// a getter method. /// - `#[ghost]`: Allows creating fields that are only present in the style chain, /// this means that they *cannot* be accessed by the user, they cannot be set /// on an individual instantiated element, and must be set via the style chain.