Add tests and a Sizable trait for ContextElement.

This commit is contained in:
Tom Alexander 2020-06-14 12:54:03 -04:00
parent 76193bf806
commit 1a54e35736
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
15 changed files with 66 additions and 0 deletions

View File

@ -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

View File

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

View File

@ -0,0 +1,3 @@
{
"val": ""
}

View File

@ -0,0 +1,3 @@
{
"val": false
}

View File

@ -0,0 +1,3 @@
{
"notval": "Alice"
}

View File

@ -0,0 +1,3 @@
{
"val": 7.21
}

View File

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

View File

@ -0,0 +1,3 @@
{
"val": "Alice"
}

View File

@ -0,0 +1,3 @@
{
"val": true
}

View File

@ -0,0 +1,3 @@
{
"val": 0
}

View File

@ -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}

View File

@ -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) {

View File

@ -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;
}

View File

@ -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;

View File

@ -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) {