mirror of
https://github.com/typst/typst
synced 2025-05-18 11:05:28 +08:00
Rename _new to new and typify to value
This commit is contained in:
parent
72434f0695
commit
7025590405
@ -21,7 +21,7 @@ fn benchmarks(c: &mut Criterion) {
|
|||||||
|
|
||||||
let mut env = Env::new(loader);
|
let mut env = Env::new(loader);
|
||||||
|
|
||||||
let scope = library::_new();
|
let scope = library::new();
|
||||||
let state = State::default();
|
let state = State::default();
|
||||||
|
|
||||||
for case in CASES {
|
for case in CASES {
|
||||||
|
@ -650,26 +650,30 @@ impl From<AnyValue> for Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Make a type usable as a [`Value`].
|
/// Mark a type as a [`Value`].
|
||||||
///
|
///
|
||||||
/// Given a type `T`, this implements the following traits:
|
/// Given a type `T`, this implements the following traits:
|
||||||
/// - [`Type`] for `T`,
|
/// - [`Type`] for `T`,
|
||||||
/// - [`Cast<Value>`](Cast) for `T`.
|
/// - [`Cast<Value>`](Cast) for `T`.
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
/// Allow a type `FontFamily` to be cast from:
|
|
||||||
/// - a [`Value::Any`] variant already containing a `FontFamily`
|
|
||||||
/// - a string, producing a `FontFamiliy::Named(..)`.
|
|
||||||
/// ```
|
/// ```
|
||||||
/// # use typst::typify;
|
/// # use typst::value;
|
||||||
/// # enum FontFamily { Named(String) }
|
/// enum FontFamily {
|
||||||
/// typify! {
|
/// Serif,
|
||||||
|
/// Named(String),
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// value! {
|
||||||
/// FontFamily: "font family",
|
/// FontFamily: "font family",
|
||||||
/// Value::Str(string) => Self::Named(string.to_lowercase())
|
/// Value::Str(string) => Self::Named(string),
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
/// This would allow the type `FontFamily` to be cast from:
|
||||||
|
/// - a [`Value::Any`] variant already containing a `FontFamily`,
|
||||||
|
/// - a string, producing a named font family.
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! typify {
|
macro_rules! value {
|
||||||
($type:ty:
|
($type:ty:
|
||||||
$type_name:literal
|
$type_name:literal
|
||||||
$(, $pattern:pat => $out:expr)*
|
$(, $pattern:pat => $out:expr)*
|
||||||
|
@ -123,6 +123,6 @@ impl Display for AlignValue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
typify! {
|
value! {
|
||||||
AlignValue: "alignment",
|
AlignValue: "alignment",
|
||||||
}
|
}
|
||||||
|
@ -132,7 +132,7 @@ pub fn font(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
|||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
struct FontFamilies(Vec<String>);
|
struct FontFamilies(Vec<String>);
|
||||||
|
|
||||||
typify! {
|
value! {
|
||||||
FontFamilies: "string or array of strings",
|
FontFamilies: "string or array of strings",
|
||||||
Value::Str(string) => Self(vec![string.to_lowercase()]),
|
Value::Str(string) => Self(vec![string.to_lowercase()]),
|
||||||
Value::Array(values) => Self(values
|
Value::Array(values) => Self(values
|
||||||
@ -143,16 +143,16 @@ typify! {
|
|||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
typify! {
|
value! {
|
||||||
FontFamily: "font family",
|
FontFamily: "font family",
|
||||||
Value::Str(string) => Self::Named(string.to_lowercase())
|
Value::Str(string) => Self::Named(string.to_lowercase())
|
||||||
}
|
}
|
||||||
|
|
||||||
typify! {
|
value! {
|
||||||
FontStyle: "font style",
|
FontStyle: "font style",
|
||||||
}
|
}
|
||||||
|
|
||||||
typify! {
|
value! {
|
||||||
FontWeight: "font weight",
|
FontWeight: "font weight",
|
||||||
Value::Int(number) => {
|
Value::Int(number) => {
|
||||||
let [min, max] = [Self::THIN, Self::BLACK];
|
let [min, max] = [Self::THIN, Self::BLACK];
|
||||||
@ -172,7 +172,7 @@ typify! {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
typify! {
|
value! {
|
||||||
FontStretch: "font stretch",
|
FontStretch: "font stretch",
|
||||||
Value::Relative(relative) => {
|
Value::Relative(relative) => {
|
||||||
let [min, max] = [Self::ULTRA_CONDENSED, Self::ULTRA_EXPANDED];
|
let [min, max] = [Self::ULTRA_CONDENSED, Self::ULTRA_EXPANDED];
|
||||||
@ -193,6 +193,6 @@ typify! {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
typify! {
|
value! {
|
||||||
VerticalFontMetric: "vertical font metric",
|
VerticalFontMetric: "vertical font metric",
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
//! The standard library.
|
//! The standard library.
|
||||||
//!
|
//!
|
||||||
//! Call [`_new`] to obtain a [`Scope`] containing all standard library
|
//! Call [`new`] to obtain a [`Scope`] containing all standard library
|
||||||
//! definitions.
|
//! definitions.
|
||||||
|
|
||||||
mod align;
|
mod align;
|
||||||
@ -40,7 +40,7 @@ use crate::geom::*;
|
|||||||
use crate::syntax::{Node, Spanned};
|
use crate::syntax::{Node, Spanned};
|
||||||
|
|
||||||
/// Construct a scope containing all standard library definitions.
|
/// Construct a scope containing all standard library definitions.
|
||||||
pub fn _new() -> Scope {
|
pub fn new() -> Scope {
|
||||||
let mut std = Scope::new();
|
let mut std = Scope::new();
|
||||||
|
|
||||||
macro_rules! func {
|
macro_rules! func {
|
||||||
@ -120,6 +120,6 @@ pub fn _new() -> Scope {
|
|||||||
std
|
std
|
||||||
}
|
}
|
||||||
|
|
||||||
typify! {
|
value! {
|
||||||
Dir: "direction"
|
Dir: "direction"
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ fn main() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
let mut env = Env::new(loader);
|
let mut env = Env::new(loader);
|
||||||
|
|
||||||
let scope = library::_new();
|
let scope = library::new();
|
||||||
let state = State::default();
|
let state = State::default();
|
||||||
|
|
||||||
let Pass { output: frames, diags } = typeset(&mut env, &src, &scope, state);
|
let Pass { output: frames, diags } = typeset(&mut env, &src, &scope, state);
|
||||||
|
@ -211,7 +211,7 @@ fn test_part(
|
|||||||
let (local_compare_ref, ref_diags) = parse_metadata(src, &map);
|
let (local_compare_ref, ref_diags) = parse_metadata(src, &map);
|
||||||
let compare_ref = local_compare_ref.unwrap_or(compare_ref);
|
let compare_ref = local_compare_ref.unwrap_or(compare_ref);
|
||||||
|
|
||||||
let mut scope = library::_new();
|
let mut scope = library::new();
|
||||||
|
|
||||||
let panics = Rc::new(RefCell::new(vec![]));
|
let panics = Rc::new(RefCell::new(vec![]));
|
||||||
register_helpers(&mut scope, Rc::clone(&panics));
|
register_helpers(&mut scope, Rc::clone(&panics));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user