mirror of
https://github.com/typst/typst
synced 2025-05-15 09:35:28 +08:00
Change default parameter of Cast
This commit is contained in:
parent
17e3353483
commit
3d965ae6a4
@ -171,7 +171,10 @@ impl Args {
|
|||||||
///
|
///
|
||||||
/// Returns a `missing argument: {what}` error if no positional argument is
|
/// Returns a `missing argument: {what}` error if no positional argument is
|
||||||
/// left.
|
/// left.
|
||||||
pub fn expect<T: Cast>(&mut self, what: &str) -> TypResult<T> {
|
pub fn expect<T>(&mut self, what: &str) -> TypResult<T>
|
||||||
|
where
|
||||||
|
T: Cast<Spanned<Value>>,
|
||||||
|
{
|
||||||
match self.eat()? {
|
match self.eat()? {
|
||||||
Some(v) => Ok(v),
|
Some(v) => Ok(v),
|
||||||
None => bail!(self.span, "missing argument: {}", what),
|
None => bail!(self.span, "missing argument: {}", what),
|
||||||
@ -179,7 +182,10 @@ impl Args {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Consume and cast the first positional argument if there is one.
|
/// Consume and cast the first positional argument if there is one.
|
||||||
pub fn eat<T: Cast>(&mut self) -> TypResult<Option<T>> {
|
pub fn eat<T>(&mut self) -> TypResult<Option<T>>
|
||||||
|
where
|
||||||
|
T: Cast<Spanned<Value>>,
|
||||||
|
{
|
||||||
for (i, slot) in self.items.iter().enumerate() {
|
for (i, slot) in self.items.iter().enumerate() {
|
||||||
if slot.name.is_none() {
|
if slot.name.is_none() {
|
||||||
let value = self.items.remove(i).value;
|
let value = self.items.remove(i).value;
|
||||||
@ -191,7 +197,10 @@ impl Args {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Find and consume the first castable positional argument.
|
/// Find and consume the first castable positional argument.
|
||||||
pub fn find<T: Cast>(&mut self) -> TypResult<Option<T>> {
|
pub fn find<T>(&mut self) -> TypResult<Option<T>>
|
||||||
|
where
|
||||||
|
T: Cast<Spanned<Value>>,
|
||||||
|
{
|
||||||
for (i, slot) in self.items.iter().enumerate() {
|
for (i, slot) in self.items.iter().enumerate() {
|
||||||
if slot.name.is_none() && T::is(&slot.value) {
|
if slot.name.is_none() && T::is(&slot.value) {
|
||||||
let value = self.items.remove(i).value;
|
let value = self.items.remove(i).value;
|
||||||
@ -203,7 +212,10 @@ impl Args {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Find and consume all castable positional arguments.
|
/// Find and consume all castable positional arguments.
|
||||||
pub fn all<T: Cast>(&mut self) -> TypResult<Vec<T>> {
|
pub fn all<T>(&mut self) -> TypResult<Vec<T>>
|
||||||
|
where
|
||||||
|
T: Cast<Spanned<Value>>,
|
||||||
|
{
|
||||||
let mut list = vec![];
|
let mut list = vec![];
|
||||||
while let Some(value) = self.find()? {
|
while let Some(value) = self.find()? {
|
||||||
list.push(value);
|
list.push(value);
|
||||||
@ -213,7 +225,10 @@ impl Args {
|
|||||||
|
|
||||||
/// Cast and remove the value for the given named argument, returning an
|
/// Cast and remove the value for the given named argument, returning an
|
||||||
/// error if the conversion fails.
|
/// error if the conversion fails.
|
||||||
pub fn named<T: Cast>(&mut self, name: &str) -> TypResult<Option<T>> {
|
pub fn named<T>(&mut self, name: &str) -> TypResult<Option<T>>
|
||||||
|
where
|
||||||
|
T: Cast<Spanned<Value>>,
|
||||||
|
{
|
||||||
// We don't quit once we have a match because when multiple matches
|
// We don't quit once we have a match because when multiple matches
|
||||||
// exist, we want to remove all of them and use the last one.
|
// exist, we want to remove all of them and use the last one.
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
@ -231,7 +246,10 @@ impl Args {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Same as named, but with fallback to find.
|
/// Same as named, but with fallback to find.
|
||||||
pub fn named_or_find<T: Cast>(&mut self, name: &str) -> TypResult<Option<T>> {
|
pub fn named_or_find<T>(&mut self, name: &str) -> TypResult<Option<T>>
|
||||||
|
where
|
||||||
|
T: Cast<Spanned<Value>>,
|
||||||
|
{
|
||||||
match self.named(name)? {
|
match self.named(name)? {
|
||||||
Some(value) => Ok(Some(value)),
|
Some(value) => Ok(Some(value)),
|
||||||
None => self.find(),
|
None => self.find(),
|
||||||
@ -266,10 +284,7 @@ impl Args {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Reinterpret these arguments as actually being a single castable thing.
|
/// Reinterpret these arguments as actually being a single castable thing.
|
||||||
fn into_castable<T>(self, what: &str) -> TypResult<T>
|
fn into_castable<T: Cast>(self, what: &str) -> TypResult<T> {
|
||||||
where
|
|
||||||
T: Cast<Value>,
|
|
||||||
{
|
|
||||||
let mut iter = self.items.into_iter();
|
let mut iter = self.items.into_iter();
|
||||||
let value = match iter.next() {
|
let value = match iter.next() {
|
||||||
Some(Arg { name: None, value, .. }) => value.v.cast().at(value.span)?,
|
Some(Arg { name: None, value, .. }) => value.v.cast().at(value.span)?,
|
||||||
|
@ -97,10 +97,7 @@ impl Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Try to cast the value into a specific type.
|
/// Try to cast the value into a specific type.
|
||||||
pub fn cast<T>(self) -> StrResult<T>
|
pub fn cast<T: Cast>(self) -> StrResult<T> {
|
||||||
where
|
|
||||||
T: Cast<Value>,
|
|
||||||
{
|
|
||||||
T::cast(self)
|
T::cast(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -327,7 +324,7 @@ pub trait Type {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Cast from a value to a specific type.
|
/// Cast from a value to a specific type.
|
||||||
pub trait Cast<V = Spanned<Value>>: Sized {
|
pub trait Cast<V = Value>: Sized {
|
||||||
/// Check whether the value is castable to `Self`.
|
/// Check whether the value is castable to `Self`.
|
||||||
fn is(value: &V) -> bool;
|
fn is(value: &V) -> bool;
|
||||||
|
|
||||||
@ -345,7 +342,7 @@ macro_rules! primitive {
|
|||||||
const TYPE_NAME: &'static str = $name;
|
const TYPE_NAME: &'static str = $name;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Cast<Value> for $type {
|
impl Cast for $type {
|
||||||
fn is(value: &Value) -> bool {
|
fn is(value: &Value) -> bool {
|
||||||
matches!(value, Value::$variant(_) $(| Value::$other(_))*)
|
matches!(value, Value::$variant(_) $(| Value::$other(_))*)
|
||||||
}
|
}
|
||||||
@ -448,7 +445,7 @@ primitive! { Func: "function", Func, Class(v) => v.constructor() }
|
|||||||
primitive! { Args: "arguments", Args }
|
primitive! { Args: "arguments", Args }
|
||||||
primitive! { Class: "class", Class }
|
primitive! { Class: "class", Class }
|
||||||
|
|
||||||
impl Cast<Value> for Value {
|
impl Cast for Value {
|
||||||
fn is(_: &Value) -> bool {
|
fn is(_: &Value) -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
@ -458,10 +455,7 @@ impl Cast<Value> for Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Cast for T
|
impl<T: Cast> Cast<Spanned<Value>> for T {
|
||||||
where
|
|
||||||
T: Cast<Value>,
|
|
||||||
{
|
|
||||||
fn is(value: &Spanned<Value>) -> bool {
|
fn is(value: &Spanned<Value>) -> bool {
|
||||||
T::is(&value.v)
|
T::is(&value.v)
|
||||||
}
|
}
|
||||||
@ -471,10 +465,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Cast for Spanned<T>
|
impl<T: Cast> Cast<Spanned<Value>> for Spanned<T> {
|
||||||
where
|
|
||||||
T: Cast<Value>,
|
|
||||||
{
|
|
||||||
fn is(value: &Spanned<Value>) -> bool {
|
fn is(value: &Spanned<Value>) -> bool {
|
||||||
T::is(&value.v)
|
T::is(&value.v)
|
||||||
}
|
}
|
||||||
@ -511,10 +502,7 @@ impl<T> Default for Smart<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Cast<Value> for Option<T>
|
impl<T: Cast> Cast for Option<T> {
|
||||||
where
|
|
||||||
T: Cast<Value>,
|
|
||||||
{
|
|
||||||
fn is(value: &Value) -> bool {
|
fn is(value: &Value) -> bool {
|
||||||
matches!(value, Value::None) || T::is(value)
|
matches!(value, Value::None) || T::is(value)
|
||||||
}
|
}
|
||||||
@ -527,10 +515,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Cast<Value> for Smart<T>
|
impl<T: Cast> Cast for Smart<T> {
|
||||||
where
|
|
||||||
T: Cast<Value>,
|
|
||||||
{
|
|
||||||
fn is(value: &Value) -> bool {
|
fn is(value: &Value) -> bool {
|
||||||
matches!(value, Value::Auto) || T::is(value)
|
matches!(value, Value::Auto) || T::is(value)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user