Add some additional impls
This commit is contained in:
parent
728d013c6d
commit
3f08af78d0
80
src/lib.rs
80
src/lib.rs
|
|
@ -1,4 +1,4 @@
|
||||||
use core::fmt::{Display, Write};
|
use core::fmt::Write;
|
||||||
|
|
||||||
pub mod internal;
|
pub mod internal;
|
||||||
|
|
||||||
|
|
@ -7,7 +7,7 @@ use internal::{
|
||||||
parse::{parse, Ast},
|
parse::{parse, Ast},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait Value: core::fmt::Debug {
|
pub trait Value {
|
||||||
fn print(&self, out: &mut String);
|
fn print(&self, out: &mut String);
|
||||||
fn lookup(&self, name: &str) -> Option<&dyn Value>;
|
fn lookup(&self, name: &str) -> Option<&dyn Value>;
|
||||||
fn has(&self) -> Option<&dyn Value>;
|
fn has(&self) -> Option<&dyn Value>;
|
||||||
|
|
@ -15,6 +15,48 @@ pub trait Value: core::fmt::Debug {
|
||||||
fn for_(&self, index: usize) -> Option<&dyn Value>;
|
fn for_(&self, index: usize) -> Option<&dyn Value>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! value_for_int {
|
||||||
|
($tp:ty) => {
|
||||||
|
impl Value for $tp {
|
||||||
|
fn print(&self, out: &mut String) {
|
||||||
|
let _ = write!(out, "{self}");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lookup(&self, _: &str) -> Option<&dyn Value> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn has(&self) -> Option<&dyn Value> {
|
||||||
|
if self.if_() {
|
||||||
|
Some(self)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn if_(&self) -> bool {
|
||||||
|
*self != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
fn for_(&self, _: usize) -> Option<&dyn Value> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
value_for_int!(u8);
|
||||||
|
value_for_int!(u16);
|
||||||
|
value_for_int!(u32);
|
||||||
|
value_for_int!(u64);
|
||||||
|
value_for_int!(u128);
|
||||||
|
value_for_int!(usize);
|
||||||
|
value_for_int!(i8);
|
||||||
|
value_for_int!(i16);
|
||||||
|
value_for_int!(i32);
|
||||||
|
value_for_int!(i64);
|
||||||
|
value_for_int!(i128);
|
||||||
|
value_for_int!(isize);
|
||||||
|
|
||||||
impl<'a, T> Value for &'a T
|
impl<'a, T> Value for &'a T
|
||||||
where
|
where
|
||||||
T: Value,
|
T: Value,
|
||||||
|
|
@ -70,6 +112,36 @@ impl Value for &str {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Value for String {
|
||||||
|
fn print(&self, out: &mut String) {
|
||||||
|
out.push_str(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lookup(&self, _: &str) -> Option<&dyn Value> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn has(&self) -> Option<&dyn Value> {
|
||||||
|
if self.if_() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn if_(&self) -> bool {
|
||||||
|
!self.is_empty()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn for_(&self, index: usize) -> Option<&dyn Value> {
|
||||||
|
if index == 0 {
|
||||||
|
Some(self)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Value for (&str, &dyn Value) {
|
impl Value for (&str, &dyn Value) {
|
||||||
fn print(&self, out: &mut String) {
|
fn print(&self, out: &mut String) {
|
||||||
out.push('(');
|
out.push('(');
|
||||||
|
|
@ -300,10 +372,10 @@ mod test {
|
||||||
"{for .items}{for .names}{.}, {/for}{/for}",
|
"{for .items}{for .names}{.}, {/for}{/for}",
|
||||||
values_list_map! {
|
values_list_map! {
|
||||||
"items": values![values_list_map! {
|
"items": values![values_list_map! {
|
||||||
"names": values!["Bob", "Larry"],
|
"names": values!["Bob", "Larry", 0],
|
||||||
}]
|
}]
|
||||||
},
|
},
|
||||||
"Bob, Larry, ",
|
"Bob, Larry, 0, ",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue