Rename _new to new and typify to value

This commit is contained in:
Laurenz 2021-05-18 16:53:11 +02:00
parent 72434f0695
commit 7025590405
7 changed files with 26 additions and 22 deletions

View File

@ -21,7 +21,7 @@ fn benchmarks(c: &mut Criterion) {
let mut env = Env::new(loader);
let scope = library::_new();
let scope = library::new();
let state = State::default();
for case in CASES {

View File

@ -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:
/// - [`Type`] for `T`,
/// - [`Cast<Value>`](Cast) for `T`.
///
/// # 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;
/// # enum FontFamily { Named(String) }
/// typify! {
/// # use typst::value;
/// enum FontFamily {
/// Serif,
/// Named(String),
/// }
///
/// value! {
/// 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_rules! typify {
macro_rules! value {
($type:ty:
$type_name:literal
$(, $pattern:pat => $out:expr)*

View File

@ -123,6 +123,6 @@ impl Display for AlignValue {
}
}
typify! {
value! {
AlignValue: "alignment",
}

View File

@ -132,7 +132,7 @@ pub fn font(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
#[derive(Debug, Clone, PartialEq)]
struct FontFamilies(Vec<String>);
typify! {
value! {
FontFamilies: "string or array of strings",
Value::Str(string) => Self(vec![string.to_lowercase()]),
Value::Array(values) => Self(values
@ -143,16 +143,16 @@ typify! {
),
}
typify! {
value! {
FontFamily: "font family",
Value::Str(string) => Self::Named(string.to_lowercase())
}
typify! {
value! {
FontStyle: "font style",
}
typify! {
value! {
FontWeight: "font weight",
Value::Int(number) => {
let [min, max] = [Self::THIN, Self::BLACK];
@ -172,7 +172,7 @@ typify! {
},
}
typify! {
value! {
FontStretch: "font stretch",
Value::Relative(relative) => {
let [min, max] = [Self::ULTRA_CONDENSED, Self::ULTRA_EXPANDED];
@ -193,6 +193,6 @@ typify! {
},
}
typify! {
value! {
VerticalFontMetric: "vertical font metric",
}

View File

@ -1,6 +1,6 @@
//! The standard library.
//!
//! Call [`_new`] to obtain a [`Scope`] containing all standard library
//! Call [`new`] to obtain a [`Scope`] containing all standard library
//! definitions.
mod align;
@ -40,7 +40,7 @@ use crate::geom::*;
use crate::syntax::{Node, Spanned};
/// Construct a scope containing all standard library definitions.
pub fn _new() -> Scope {
pub fn new() -> Scope {
let mut std = Scope::new();
macro_rules! func {
@ -120,6 +120,6 @@ pub fn _new() -> Scope {
std
}
typify! {
value! {
Dir: "direction"
}

View File

@ -40,7 +40,7 @@ fn main() -> anyhow::Result<()> {
let mut env = Env::new(loader);
let scope = library::_new();
let scope = library::new();
let state = State::default();
let Pass { output: frames, diags } = typeset(&mut env, &src, &scope, state);

View File

@ -211,7 +211,7 @@ fn test_part(
let (local_compare_ref, ref_diags) = parse_metadata(src, &map);
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![]));
register_helpers(&mut scope, Rc::clone(&panics));