Add tests and a Sizable trait for ContextElement.

master
Tom Alexander 4 years ago
parent 76193bf806
commit 1a54e35736
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

@ -0,0 +1,9 @@
Excerpt from [the DustJS tutorial](https://github.com/linkedin/dustjs/wiki/Dust-Tutorial#size_keyxxx___size_helper_Available_in_Dust_V11_release):
Array - number of elements, [1,2,3,4] has size=4
String - length of the string, "abcdef" has size=6
Object - Number of properties in the object, {a:4, b:8, c:15, d:16} has size=4
Number - Value of the number, 23 has size 23 and 3.14 has size 3.14
Undefined, 0, empty string - zero
Any other value - length after conversion to string

@ -0,0 +1,7 @@
{
"val": [
"Alice",
"Bob",
"Chris"
]
}

@ -0,0 +1,6 @@
{
"val": {
"pet": "cat",
"name": "fluffy"
}
}

@ -0,0 +1,3 @@
The size of val ({val|js|s}) is {@size key=val /}{~n}
The size of "{val}" is {@size key="{val}" /}{~n}
The size with no key is {@size /}{~n}

@ -15,6 +15,7 @@ use renderer::Loopable;
use renderer::MathNumber;
use renderer::RenderError;
use renderer::Renderable;
use renderer::Sizable;
use renderer::Truthiness;
use renderer::WalkError;
use renderer::Walkable;
@ -313,6 +314,12 @@ impl Loopable for serde_json::Value {
}
}
impl Sizable for serde_json::Value {
fn get_size<'a>(&'a self) -> Option<IceResult<'a>> {
todo!()
}
}
impl Castable for serde_json::Value {
fn cast_to_type<'a>(&'a self, target: &str) -> Option<IceResult<'a>> {
match (self, target) {

@ -22,6 +22,7 @@ pub trait ContextElement:
+ FromContextElement
+ IntoRcIce
+ Castable
+ Sizable
{
}
@ -63,6 +64,10 @@ pub trait Castable {
fn cast_to_type<'a>(&'a self, target: &str) -> Option<IceResult<'a>>;
}
pub trait Sizable {
fn get_size<'a>(&'a self) -> Option<IceResult<'a>>;
}
pub trait CastToAny {
fn to_any(&self) -> &dyn Any;
}

@ -18,6 +18,7 @@ pub use context_element::IceResult;
pub use context_element::IntoContextElement;
pub use context_element::Loopable;
pub use context_element::Renderable;
pub use context_element::Sizable;
pub use context_element::Truthiness;
pub use context_element::Walkable;
pub use errors::CompileError;

@ -14,6 +14,7 @@ use crate::renderer::DustRenderer;
use crate::renderer::Loopable;
use crate::renderer::RenderError;
use crate::renderer::Renderable;
use crate::renderer::Sizable;
use crate::renderer::Truthiness;
use crate::renderer::WalkError;
use crate::renderer::Walkable;
@ -189,6 +190,12 @@ impl Walkable for OwnedLiteral {
}
}
impl Sizable for OwnedLiteral {
fn get_size<'a>(&'a self) -> Option<IceResult<'a>> {
todo!()
}
}
impl Castable for OwnedLiteral {
fn cast_to_type<'a>(&'a self, target: &str) -> Option<IceResult<'a>> {
match (self, target) {

Loading…
Cancel
Save