From 37249c20f793689b523cbb379f30b37466ca51b9 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Fri, 12 Jan 2024 14:36:41 +0100 Subject: [PATCH] Autogenerate default cast in`#[ty]` unless `cast` is specified --- crates/typst-macros/src/lib.rs | 2 ++ crates/typst-macros/src/ty.rs | 34 +++++++++++----------- crates/typst-macros/src/util.rs | 1 + crates/typst/src/foundations/args.rs | 2 +- crates/typst/src/foundations/array.rs | 2 +- crates/typst/src/foundations/auto.rs | 2 +- crates/typst/src/foundations/bool.rs | 2 +- crates/typst/src/foundations/bytes.rs | 2 +- crates/typst/src/foundations/content.rs | 2 +- crates/typst/src/foundations/datetime.rs | 2 +- crates/typst/src/foundations/dict.rs | 2 +- crates/typst/src/foundations/duration.rs | 2 +- crates/typst/src/foundations/float.rs | 2 +- crates/typst/src/foundations/func.rs | 2 +- crates/typst/src/foundations/int.rs | 2 +- crates/typst/src/foundations/label.rs | 2 +- crates/typst/src/foundations/module.rs | 2 +- crates/typst/src/foundations/none.rs | 2 +- crates/typst/src/foundations/plugin.rs | 2 +- crates/typst/src/foundations/selector.rs | 2 +- crates/typst/src/foundations/str.rs | 6 +--- crates/typst/src/foundations/styles.rs | 2 +- crates/typst/src/foundations/ty.rs | 2 +- crates/typst/src/foundations/version.rs | 2 +- crates/typst/src/introspection/counter.rs | 6 +--- crates/typst/src/introspection/location.rs | 6 +--- crates/typst/src/introspection/mod.rs | 7 +---- crates/typst/src/introspection/state.rs | 6 +--- crates/typst/src/layout/align.rs | 4 --- crates/typst/src/layout/angle.rs | 2 +- crates/typst/src/layout/dir.rs | 6 +--- crates/typst/src/layout/fr.rs | 2 +- crates/typst/src/layout/length.rs | 2 +- crates/typst/src/layout/ratio.rs | 2 +- crates/typst/src/layout/rel.rs | 2 +- crates/typst/src/model/bibliography.rs | 6 +--- crates/typst/src/symbols/symbol.rs | 2 +- crates/typst/src/text/deco.rs | 6 +--- crates/typst/src/visualize/color.rs | 2 +- crates/typst/src/visualize/gradient.rs | 2 +- crates/typst/src/visualize/pattern.rs | 2 +- crates/typst/src/visualize/stroke.rs | 2 +- 42 files changed, 58 insertions(+), 92 deletions(-) diff --git a/crates/typst-macros/src/lib.rs b/crates/typst-macros/src/lib.rs index 8ca7bcac3..a9185a9ba 100644 --- a/crates/typst-macros/src/lib.rs +++ b/crates/typst-macros/src/lib.rs @@ -116,6 +116,8 @@ pub fn func(stream: BoundaryStream, item: BoundaryStream) -> BoundaryStream { /// You can customize some properties of the resulting type: /// - `scope`: Indicates that the type has an associated scope defined by the /// `#[scope]` macro +/// - `cast`: Indicates that the type has a custom `cast!` implementation. +/// The macro will then not autogenerate one. /// - `name`: The type's normal name (e.g. `str`). Defaults to the Rust name in /// kebab-case. /// - `title`: The type's title case name (e.g. `String`). Defaults to the diff --git a/crates/typst-macros/src/ty.rs b/crates/typst-macros/src/ty.rs index 23f818bd7..943bd4530 100644 --- a/crates/typst-macros/src/ty.rs +++ b/crates/typst-macros/src/ty.rs @@ -28,18 +28,18 @@ pub fn ty(stream: TokenStream, item: syn::Item) -> Result { /// Holds all relevant parsed data about a type. struct Type { + meta: Meta, ident: Ident, name: String, long: String, - scope: bool, title: String, docs: String, - keywords: Vec, } /// The `..` in `#[ty(..)]`. struct Meta { scope: bool, + cast: bool, name: Option, title: Option, keywords: Vec, @@ -49,6 +49,7 @@ impl Parse for Meta { fn parse(input: ParseStream) -> Result { Ok(Self { scope: parse_flag::(input)?, + cast: parse_flag::(input)?, name: parse_string::(input)?, title: parse_string::(input)?, keywords: parse_string_array::(input)?, @@ -59,37 +60,35 @@ impl Parse for Meta { /// Parse details about the type from its definition. fn parse(meta: Meta, ident: Ident, attrs: &[Attribute]) -> Result { let docs = documentation(attrs); - let (name, title) = determine_name_and_title(meta.name, meta.title, &ident, None)?; + let (name, title) = + determine_name_and_title(meta.name.clone(), meta.title.clone(), &ident, None)?; let long = title.to_lowercase(); - Ok(Type { - ident, - name, - long, - scope: meta.scope, - keywords: meta.keywords, - title, - docs, - }) + Ok(Type { meta, ident, name, long, title, docs }) } /// Produce the output of the macro. fn create(ty: &Type, item: Option<&syn::Item>) -> TokenStream { - let Type { - ident, name, long, title, docs, keywords, scope, .. - } = ty; + let Type { ident, name, long, title, docs, meta, .. } = ty; + let Meta { keywords, .. } = meta; - let constructor = if *scope { + let constructor = if meta.scope { quote! { <#ident as #foundations::NativeScope>::constructor() } } else { quote! { None } }; - let scope = if *scope { + let scope = if meta.scope { quote! { <#ident as #foundations::NativeScope>::scope() } } else { quote! { #foundations::Scope::new() } }; + let cast = (!meta.cast).then(|| { + quote! { + #foundations::cast! { type #ident, } + } + }); + let data = quote! { #foundations::NativeTypeData { name: #name, @@ -104,6 +103,7 @@ fn create(ty: &Type, item: Option<&syn::Item>) -> TokenStream { quote! { #item + #cast impl #foundations::NativeType for #ident { const NAME: &'static str = #name; diff --git a/crates/typst-macros/src/util.rs b/crates/typst-macros/src/util.rs index 4f7cc5fb5..89880db17 100644 --- a/crates/typst-macros/src/util.rs +++ b/crates/typst-macros/src/util.rs @@ -255,6 +255,7 @@ pub mod kw { syn::custom_keyword!(span); syn::custom_keyword!(title); syn::custom_keyword!(scope); + syn::custom_keyword!(cast); syn::custom_keyword!(constructor); syn::custom_keyword!(keywords); syn::custom_keyword!(parent); diff --git a/crates/typst/src/foundations/args.rs b/crates/typst/src/foundations/args.rs index 01cf6d7fc..548114375 100644 --- a/crates/typst/src/foundations/args.rs +++ b/crates/typst/src/foundations/args.rs @@ -38,7 +38,7 @@ use crate::syntax::{Span, Spanned}; /// #let dict = (fill: blue) /// #text(..dict)[Hello] /// ``` -#[ty(scope, name = "arguments")] +#[ty(scope, cast, name = "arguments")] #[derive(Clone, Hash)] #[allow(clippy::derived_hash_with_manual_eq)] pub struct Args { diff --git a/crates/typst/src/foundations/array.rs b/crates/typst/src/foundations/array.rs index 86d4c2ac8..6f7c63475 100644 --- a/crates/typst/src/foundations/array.rs +++ b/crates/typst/src/foundations/array.rs @@ -67,7 +67,7 @@ pub use crate::__array as array; /// #(("A", "B", "C") /// .join(", ", last: " and ")) /// ``` -#[ty(scope)] +#[ty(scope, cast)] #[derive(Default, Clone, PartialEq, Hash, Serialize, Deserialize)] #[serde(transparent)] pub struct Array(EcoVec); diff --git a/crates/typst/src/foundations/auto.rs b/crates/typst/src/foundations/auto.rs index 5cb03f7f1..fcd72999b 100644 --- a/crates/typst/src/foundations/auto.rs +++ b/crates/typst/src/foundations/auto.rs @@ -15,7 +15,7 @@ use crate::foundations::{ /// contextual behaviour. A good example is the [text direction]($text.dir) /// parameter. Setting it to `{auto}` lets Typst automatically determine the /// direction from the [text language]($text.lang). -#[ty(name = "auto")] +#[ty(cast, name = "auto")] #[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct AutoValue; diff --git a/crates/typst/src/foundations/bool.rs b/crates/typst/src/foundations/bool.rs index 4c85a7415..e88c8c6ff 100644 --- a/crates/typst/src/foundations/bool.rs +++ b/crates/typst/src/foundations/bool.rs @@ -13,7 +13,7 @@ use crate::foundations::{ty, Repr}; /// #true \ /// #(1 < 2) /// ``` -#[ty(title = "Boolean")] +#[ty(cast, title = "Boolean")] type bool; impl Repr for bool { diff --git a/crates/typst/src/foundations/bytes.rs b/crates/typst/src/foundations/bytes.rs index 4e894241e..b2fd0e3bd 100644 --- a/crates/typst/src/foundations/bytes.rs +++ b/crates/typst/src/foundations/bytes.rs @@ -37,7 +37,7 @@ use crate::foundations::{cast, func, scope, ty, Array, Reflect, Repr, Str, Value /// #array(data.slice(0, 4)) \ /// #str(data.slice(1, 4)) /// ``` -#[ty(scope)] +#[ty(scope, cast)] #[derive(Clone, Hash, Eq, PartialEq)] pub struct Bytes(Arc>>); diff --git a/crates/typst/src/foundations/content.rs b/crates/typst/src/foundations/content.rs index 3d40c7035..e498c8cb2 100644 --- a/crates/typst/src/foundations/content.rs +++ b/crates/typst/src/foundations/content.rs @@ -65,7 +65,7 @@ use crate::util::fat; /// In the web app, you can hover over a content variable to see exactly which /// elements the content is composed of and what fields they have. /// Alternatively, you can inspect the output of the [`repr`]($repr) function. -#[ty(scope)] +#[ty(scope, cast)] #[derive(Clone, Hash)] #[allow(clippy::derived_hash_with_manual_eq)] pub struct Content(Arc); diff --git a/crates/typst/src/foundations/datetime.rs b/crates/typst/src/foundations/datetime.rs index 78290c994..28a0d177b 100644 --- a/crates/typst/src/foundations/datetime.rs +++ b/crates/typst/src/foundations/datetime.rs @@ -111,7 +111,7 @@ use crate::World; /// will be stored as a plain date internally, meaning that you cannot use /// components such as `hour` or `minute`, which would only work on datetimes /// that have a specified time. -#[ty(scope)] +#[ty(scope, cast)] #[derive(Debug, Clone, Copy, PartialEq, Hash)] pub enum Datetime { /// Representation as a date. diff --git a/crates/typst/src/foundations/dict.rs b/crates/typst/src/foundations/dict.rs index b43c5428e..f072a6193 100644 --- a/crates/typst/src/foundations/dict.rs +++ b/crates/typst/src/foundations/dict.rs @@ -62,7 +62,7 @@ pub use crate::__dict as dict; /// #dict.insert("city", "Berlin ") /// #("name" in dict) /// ``` -#[ty(scope, name = "dictionary")] +#[ty(scope, cast, name = "dictionary")] #[derive(Default, Clone, PartialEq)] pub struct Dict(Arc>); diff --git a/crates/typst/src/foundations/duration.rs b/crates/typst/src/foundations/duration.rs index b6a48f3b0..83e3d962a 100644 --- a/crates/typst/src/foundations/duration.rs +++ b/crates/typst/src/foundations/duration.rs @@ -7,7 +7,7 @@ use time::ext::NumericalDuration; use crate::foundations::{func, repr, scope, ty, Repr}; /// Represents a positive or negative span of time. -#[ty(scope)] +#[ty(scope, cast)] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct Duration(time::Duration); diff --git a/crates/typst/src/foundations/float.rs b/crates/typst/src/foundations/float.rs index 6bcebda88..723e023bb 100644 --- a/crates/typst/src/foundations/float.rs +++ b/crates/typst/src/foundations/float.rs @@ -19,7 +19,7 @@ use crate::layout::Ratio; /// #1e4 \ /// #(10 / 4) /// ``` -#[ty(scope, name = "float")] +#[ty(scope, cast, name = "float")] type f64; #[scope] diff --git a/crates/typst/src/foundations/func.rs b/crates/typst/src/foundations/func.rs index 22a20fdb6..70d0ec311 100644 --- a/crates/typst/src/foundations/func.rs +++ b/crates/typst/src/foundations/func.rs @@ -123,7 +123,7 @@ pub use typst_macros::func; /// The only exception are built-in methods like /// [`array.push(value)`]($array.push). These can modify the values they are /// called on. -#[ty(scope, name = "function")] +#[ty(scope, cast, name = "function")] #[derive(Clone, Hash)] #[allow(clippy::derived_hash_with_manual_eq)] pub struct Func { diff --git a/crates/typst/src/foundations/int.rs b/crates/typst/src/foundations/int.rs index 602cdd5ae..9fffadbfe 100644 --- a/crates/typst/src/foundations/int.rs +++ b/crates/typst/src/foundations/int.rs @@ -25,7 +25,7 @@ use crate::foundations::{cast, func, repr, scope, ty, Repr, Str, Value}; /// #0o10 \ /// #0b1001 /// ``` -#[ty(scope, name = "int", title = "Integer")] +#[ty(scope, cast, name = "int", title = "Integer")] type i64; #[scope] diff --git a/crates/typst/src/foundations/label.rs b/crates/typst/src/foundations/label.rs index 53914c358..9d84a5643 100644 --- a/crates/typst/src/foundations/label.rs +++ b/crates/typst/src/foundations/label.rs @@ -31,7 +31,7 @@ use crate::util::PicoStr; /// /// Currently, labels can only be attached to elements in markup mode, not in /// code mode. This might change in the future. -#[ty(scope)] +#[ty(scope, cast)] #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct Label(PicoStr); diff --git a/crates/typst/src/foundations/module.rs b/crates/typst/src/foundations/module.rs index 140618bcf..3ec516d34 100644 --- a/crates/typst/src/foundations/module.rs +++ b/crates/typst/src/foundations/module.rs @@ -23,7 +23,7 @@ use crate::foundations::{repr, ty, Content, Scope, Value}; /// >>> /// >>> #(-3) /// ``` -#[ty] +#[ty(cast)] #[derive(Clone, Hash)] #[allow(clippy::derived_hash_with_manual_eq)] pub struct Module { diff --git a/crates/typst/src/foundations/none.rs b/crates/typst/src/foundations/none.rs index d03ca8fc0..afb055a69 100644 --- a/crates/typst/src/foundations/none.rs +++ b/crates/typst/src/foundations/none.rs @@ -20,7 +20,7 @@ use crate::foundations::{ /// ```example /// Not visible: #none /// ``` -#[ty(name = "none")] +#[ty(cast, name = "none")] #[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct NoneValue; diff --git a/crates/typst/src/foundations/plugin.rs b/crates/typst/src/foundations/plugin.rs index 898ac6f8c..80644ea53 100644 --- a/crates/typst/src/foundations/plugin.rs +++ b/crates/typst/src/foundations/plugin.rs @@ -115,7 +115,7 @@ use crate::World; /// - Wrappers to help you write your plugin in Rust (Zig wrapper in /// development) /// - A stubber for WASI -#[ty(scope)] +#[ty(scope, cast)] #[derive(Clone)] pub struct Plugin(Arc); diff --git a/crates/typst/src/foundations/selector.rs b/crates/typst/src/foundations/selector.rs index f0ab90ee7..05bc480e2 100644 --- a/crates/typst/src/foundations/selector.rs +++ b/crates/typst/src/foundations/selector.rs @@ -76,7 +76,7 @@ pub use crate::__select_where as select_where; /// == So will this /// === But this will not. /// ``` -#[ty(scope)] +#[ty(scope, cast)] #[derive(Debug, Clone, PartialEq, Hash)] pub enum Selector { /// Matches a specific type of element. diff --git a/crates/typst/src/foundations/str.rs b/crates/typst/src/foundations/str.rs index 57eac74d8..553c21701 100644 --- a/crates/typst/src/foundations/str.rs +++ b/crates/typst/src/foundations/str.rs @@ -67,7 +67,7 @@ pub use ecow::eco_format; /// - `[\r]` for a carriage return /// - `[\t]` for a tab /// - `[\u{1f600}]` for a hexadecimal Unicode escape sequence -#[ty(scope, title = "String")] +#[ty(scope, cast, title = "String")] #[derive(Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] #[derive(Serialize, Deserialize)] #[serde(transparent)] @@ -898,10 +898,6 @@ impl Hash for Regex { } } -cast! { - type Regex, -} - /// A pattern which can be searched for in a string. #[derive(Debug, Clone)] pub enum StrPattern { diff --git a/crates/typst/src/foundations/styles.rs b/crates/typst/src/foundations/styles.rs index 3fa9e9fbd..771a3d2c5 100644 --- a/crates/typst/src/foundations/styles.rs +++ b/crates/typst/src/foundations/styles.rs @@ -64,7 +64,7 @@ impl Show for StyleElem { } /// A list of style properties. -#[ty] +#[ty(cast)] #[derive(Default, PartialEq, Clone, Hash)] pub struct Styles(EcoVec>); diff --git a/crates/typst/src/foundations/ty.rs b/crates/typst/src/foundations/ty.rs index 4b9f19a96..485bfb453 100644 --- a/crates/typst/src/foundations/ty.rs +++ b/crates/typst/src/foundations/ty.rs @@ -53,7 +53,7 @@ pub use typst_macros::{scope, ty}; /// - Adding/joining a type and string will yield a string /// - The `{in}` operator on a type and a dictionary will evaluate to `{true}` /// if the dictionary has a string key matching the type's name -#[ty(scope)] +#[ty(scope, cast)] #[derive(Copy, Clone, Eq, PartialEq, Hash)] pub struct Type(Static); diff --git a/crates/typst/src/foundations/version.rs b/crates/typst/src/foundations/version.rs index 98ad598b7..6f5c350ca 100644 --- a/crates/typst/src/foundations/version.rs +++ b/crates/typst/src/foundations/version.rs @@ -20,7 +20,7 @@ use crate::foundations::{cast, func, repr, scope, ty, Repr}; /// /// You can convert a version to an array of explicitly given components using /// the [`array`]($array) constructor. -#[ty(scope)] +#[ty(scope, cast)] #[derive(Debug, Default, Clone, Hash)] #[allow(clippy::derived_hash_with_manual_eq)] pub struct Version(EcoVec); diff --git a/crates/typst/src/introspection/counter.rs b/crates/typst/src/introspection/counter.rs index 0de1ecaed..0f319fabd 100644 --- a/crates/typst/src/introspection/counter.rs +++ b/crates/typst/src/introspection/counter.rs @@ -475,10 +475,6 @@ impl Repr for Counter { } } -cast! { - type Counter, -} - /// Identifies a counter. #[derive(Debug, Clone, PartialEq, Hash)] pub enum CounterKey { @@ -521,7 +517,7 @@ impl Repr for CounterKey { } /// An update to perform on a counter. -#[ty] +#[ty(cast)] #[derive(Debug, Clone, PartialEq, Hash)] pub enum CounterUpdate { /// Set the counter to the specified state. diff --git a/crates/typst/src/introspection/location.rs b/crates/typst/src/introspection/location.rs index 0ceaac8b4..23ed81d67 100644 --- a/crates/typst/src/introspection/location.rs +++ b/crates/typst/src/introspection/location.rs @@ -3,7 +3,7 @@ use std::num::NonZeroUsize; use ecow::EcoString; use crate::engine::Engine; -use crate::foundations::{cast, func, scope, ty, Dict, Repr}; +use crate::foundations::{func, scope, ty, Dict, Repr}; use crate::model::Numbering; /// Identifies an element in the document. @@ -79,9 +79,5 @@ impl Repr for Location { } } -cast! { - type Location, -} - /// Makes this element locatable through `engine.locate`. pub trait Locatable {} diff --git a/crates/typst/src/introspection/mod.rs b/crates/typst/src/introspection/mod.rs index 0f5d11b14..869178918 100644 --- a/crates/typst/src/introspection/mod.rs +++ b/crates/typst/src/introspection/mod.rs @@ -26,8 +26,7 @@ use ecow::{eco_format, EcoString}; use smallvec::SmallVec; use crate::foundations::{ - cast, category, elem, ty, Behave, Behaviour, Category, Content, Repr, Scope, - Unlabellable, + category, elem, ty, Behave, Behaviour, Category, Content, Repr, Scope, Unlabellable, }; use crate::layout::PdfPageLabel; use crate::model::{Destination, Numbering}; @@ -89,10 +88,6 @@ pub enum Meta { Hide, } -cast! { - type Meta, -} - impl Debug for Meta { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match self { diff --git a/crates/typst/src/introspection/state.rs b/crates/typst/src/introspection/state.rs index c1264f1c7..3d050fb0d 100644 --- a/crates/typst/src/introspection/state.rs +++ b/crates/typst/src/introspection/state.rs @@ -351,12 +351,8 @@ impl Repr for State { } } -cast! { - type State, -} - /// An update to perform on a state. -#[ty] +#[ty(cast)] #[derive(Debug, Clone, PartialEq, Hash)] pub enum StateUpdate { /// Set the state to the specified value. diff --git a/crates/typst/src/layout/align.rs b/crates/typst/src/layout/align.rs index 052ac49fe..a1f2a5536 100644 --- a/crates/typst/src/layout/align.rs +++ b/crates/typst/src/layout/align.rs @@ -245,10 +245,6 @@ impl From for Alignment { } } -cast! { - type Alignment, -} - /// Where to align something horizontally. #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)] pub enum HAlignment { diff --git a/crates/typst/src/layout/angle.rs b/crates/typst/src/layout/angle.rs index e6ed9a588..1cded9a3b 100644 --- a/crates/typst/src/layout/angle.rs +++ b/crates/typst/src/layout/angle.rs @@ -19,7 +19,7 @@ use crate::util::{Numeric, Scalar}; /// ```example /// #rotate(10deg)[Hello there!] /// ``` -#[ty(scope)] +#[ty(scope, cast)] #[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct Angle(Scalar); diff --git a/crates/typst/src/layout/dir.rs b/crates/typst/src/layout/dir.rs index 569c18800..9a2e77105 100644 --- a/crates/typst/src/layout/dir.rs +++ b/crates/typst/src/layout/dir.rs @@ -1,6 +1,6 @@ use ecow::EcoString; -use crate::foundations::{cast, func, scope, ty, Repr}; +use crate::foundations::{func, scope, ty, Repr}; use crate::layout::{Axis, Side}; /// The four directions into which content can be laid out. @@ -130,7 +130,3 @@ impl Repr for Dir { } } } - -cast! { - type Dir, -} diff --git a/crates/typst/src/layout/fr.rs b/crates/typst/src/layout/fr.rs index 24fe77f86..23f5cf620 100644 --- a/crates/typst/src/layout/fr.rs +++ b/crates/typst/src/layout/fr.rs @@ -20,7 +20,7 @@ use crate::util::{Numeric, Scalar}; /// ```example /// Left #h(1fr) Left-ish #h(2fr) Right /// ``` -#[ty(name = "fraction")] +#[ty(cast, name = "fraction")] #[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct Fr(Scalar); diff --git a/crates/typst/src/layout/length.rs b/crates/typst/src/layout/length.rs index 26b9320bf..94e85248a 100644 --- a/crates/typst/src/layout/length.rs +++ b/crates/typst/src/layout/length.rs @@ -38,7 +38,7 @@ use crate::util::Numeric; /// - `abs`: A length with just the absolute component of the current length /// (that is, excluding the `em` component). /// - `em`: The amount of `em` units in this length, as a [float]($float). -#[ty(scope)] +#[ty(scope, cast)] #[derive(Default, Copy, Clone, Eq, PartialEq, Hash)] pub struct Length { /// The absolute part. diff --git a/crates/typst/src/layout/ratio.rs b/crates/typst/src/layout/ratio.rs index b535df38d..8b4e72d42 100644 --- a/crates/typst/src/layout/ratio.rs +++ b/crates/typst/src/layout/ratio.rs @@ -17,7 +17,7 @@ use crate::util::{Numeric, Scalar}; /// Scaled apart. /// ] /// ``` -#[ty] +#[ty(cast)] #[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct Ratio(Scalar); diff --git a/crates/typst/src/layout/rel.rs b/crates/typst/src/layout/rel.rs index 7769f858b..8f9371020 100644 --- a/crates/typst/src/layout/rel.rs +++ b/crates/typst/src/layout/rel.rs @@ -25,7 +25,7 @@ use crate::util::Numeric; /// A relative length has the following fields: /// - `length`: Its length component. /// - `ratio`: Its ratio component. -#[ty(name = "relative", title = "Relative Length")] +#[ty(cast, name = "relative", title = "Relative Length")] #[derive(Default, Copy, Clone, Eq, PartialEq, Hash)] pub struct Rel { /// The relative part. diff --git a/crates/typst/src/model/bibliography.rs b/crates/typst/src/model/bibliography.rs index 62e6d986f..6ec43b66f 100644 --- a/crates/typst/src/model/bibliography.rs +++ b/crates/typst/src/model/bibliography.rs @@ -421,10 +421,6 @@ impl Repr for Bibliography { } } -cast! { - type Bibliography, -} - /// Format a BibLaTeX loading error. fn format_biblatex_error(path: &str, src: &str, errors: Vec) -> EcoString { let Some(error) = errors.first() else { @@ -440,7 +436,7 @@ fn format_biblatex_error(path: &str, src: &str, errors: Vec) -> E } /// A loaded CSL style. -#[ty] +#[ty(cast)] #[derive(Debug, Clone, PartialEq, Hash)] pub struct CslStyle { name: Option, diff --git a/crates/typst/src/symbols/symbol.rs b/crates/typst/src/symbols/symbol.rs index 4ba603825..73bdd286a 100644 --- a/crates/typst/src/symbols/symbol.rs +++ b/crates/typst/src/symbols/symbol.rs @@ -42,7 +42,7 @@ pub use typst_macros::symbols; /// $arrow.r$ \ /// $arrow.t.quad$ /// ``` -#[ty(scope)] +#[ty(scope, cast)] #[derive(Debug, Clone, Eq, PartialEq, Hash)] pub struct Symbol(Repr); diff --git a/crates/typst/src/text/deco.rs b/crates/typst/src/text/deco.rs index 4aced36d9..0968782ef 100644 --- a/crates/typst/src/text/deco.rs +++ b/crates/typst/src/text/deco.rs @@ -5,7 +5,7 @@ use ecow::{eco_format, EcoString}; use crate::diag::SourceResult; use crate::engine::Engine; -use crate::foundations::{cast, elem, ty, Content, Fold, Repr, Show, Smart, StyleChain}; +use crate::foundations::{elem, ty, Content, Fold, Repr, Show, Smart, StyleChain}; use crate::layout::{Abs, Em, Frame, FrameItem, Length, Point, Size}; use crate::syntax::Span; use crate::text::{ @@ -363,10 +363,6 @@ impl Repr for Decoration { } } -cast! { - type Decoration, -} - /// A kind of decorative line. #[derive(Debug, Clone, Eq, PartialEq, Hash)] enum DecoLine { diff --git a/crates/typst/src/visualize/color.rs b/crates/typst/src/visualize/color.rs index 0f50432bd..a8b51ceea 100644 --- a/crates/typst/src/visualize/color.rs +++ b/crates/typst/src/visualize/color.rs @@ -163,7 +163,7 @@ const ANGLE_EPSILON: f32 = 1e-5; /// ) /// })) /// ``` -#[ty(scope)] +#[ty(scope, cast)] #[derive(Copy, Clone)] pub enum Color { /// A 32-bit luma color. diff --git a/crates/typst/src/visualize/gradient.rs b/crates/typst/src/visualize/gradient.rs index 623cc368b..da7470265 100644 --- a/crates/typst/src/visualize/gradient.rs +++ b/crates/typst/src/visualize/gradient.rs @@ -175,7 +175,7 @@ use crate::visualize::{Color, ColorSpace, WeightedColor}; /// [`color.oklab`]($color.oklab) colors with extra stops in between. This /// avoids needing to encode these color spaces in your PDF file, but it does /// add extra stops to your gradient, which can increase the file size. -#[ty(scope)] +#[ty(scope, cast)] #[derive(Clone, PartialEq, Eq, Hash)] pub enum Gradient { Linear(Arc), diff --git a/crates/typst/src/visualize/pattern.rs b/crates/typst/src/visualize/pattern.rs index a181ad0d7..4da1cd616 100644 --- a/crates/typst/src/visualize/pattern.rs +++ b/crates/typst/src/visualize/pattern.rs @@ -95,7 +95,7 @@ use crate::World; /// that are implicitly created by show rules and elements. For example, a /// [`rotate`]($rotate) will not affect the parent of a gradient, but a /// [`grid`]($grid) will. -#[ty(scope)] +#[ty(scope, cast)] #[derive(Debug, Clone, Eq, PartialEq, Hash)] pub struct Pattern(Arc); diff --git a/crates/typst/src/visualize/stroke.rs b/crates/typst/src/visualize/stroke.rs index 3686831d4..1d43a7b3c 100644 --- a/crates/typst/src/visualize/stroke.rs +++ b/crates/typst/src/visualize/stroke.rs @@ -49,7 +49,7 @@ use crate::visualize::{Color, Gradient, Paint, Pattern}; /// constructor function. For example, `{(2pt + blue).thickness}` is `{2pt}`. /// Meanwhile, `{stroke(red).cap}` is `{auto}` because it's unspecified. Fields /// set to `{auto}` are inherited. -#[ty(scope)] +#[ty(scope, cast)] #[derive(Debug, Default, Clone, Eq, PartialEq, Hash)] pub struct Stroke { /// The stroke's paint.