Add defaults for Value

This commit is contained in:
soup 2024-06-09 18:06:57 -04:00
parent c61c2dba37
commit 417e36e75b
No known key found for this signature in database

View file

@ -22,34 +22,55 @@ use internal::{
/// The primary trait exposed by this crate. Implement this trait if you would /// The primary trait exposed by this crate. Implement this trait if you would
/// like to be able to render a type in a template. /// like to be able to render a type in a template.
///
/// NOTE: There are defaults implemented for every method on this trait:
/// - print: prints nothing
/// - lookup: always returns None
/// - has: always returns None
/// - is_truthy: always returns true
/// - index: always returns None
///
/// You only need to implement the methods whose behaviors you want to change
pub trait Value: core::fmt::Debug { pub trait Value: core::fmt::Debug {
/// Print this value to the given string. /// Print this value to the given string.
/// ///
/// See also [`internal::parse::Print`] and [`internal::vm::ops::Op::ValuePrint`] /// See also [`internal::parse::Print`] and [`internal::vm::ops::Op::ValuePrint`]
fn print(&self, out: &mut String); fn print(&self, out: &mut String) {
let _ = out;
}
/// Lookup a field name in this value. The returned [`Value`] will be set as /// Lookup a field name in this value. The returned [`Value`] will be set as
/// the current value inside the block. /// the current value inside the block.
/// ///
/// See also [internal::vm::ops::Op::ValueLookup] /// See also [internal::vm::ops::Op::ValueLookup]
fn lookup(&self, name: &str) -> Option<&dyn Value>; fn lookup(&self, name: &str) -> Option<&dyn Value> {
let _ = name;
None
}
/// Like [`Option::map`], but for a value. The returned [`Value`] will be set /// Like [`Option::map`], but for a value. The returned [`Value`] will be set
/// as the current value inside the block. /// as the current value inside the block.
/// ///
/// See also [`internal::parse::Has`] and [`internal::vm::ops::Op::ValueHas`] /// See also [`internal::parse::Has`] and [`internal::vm::ops::Op::ValueHas`]
fn has(&self) -> Option<&dyn Value>; fn has(&self) -> Option<&dyn Value> {
None
}
/// Check if a value is truthy. /// Check if a value is truthy.
/// ///
/// See also [`internal::parse::If`] and [`internal::vm::ops::Op::ValueTruthy`] /// See also [`internal::parse::If`] and [`internal::vm::ops::Op::ValueTruthy`]
fn is_truthy(&self) -> bool; fn is_truthy(&self) -> bool {
true
}
/// Lookup the current index in this value. The returned [`Value`] will be /// Lookup the current index in this value. The returned [`Value`] will be
/// set as the current value inside the block. /// set as the current value inside the block.
/// ///
/// See also [`internal::parse::For`] and [`internal::vm::ops::Op::ValueIndex`] /// See also [`internal::parse::For`] and [`internal::vm::ops::Op::ValueIndex`]
fn index(&self, index: usize) -> Option<&dyn Value>; fn index(&self, index: usize) -> Option<&dyn Value> {
let _ = index;
None
}
} }
macro_rules! value_for_int { macro_rules! value_for_int {