Starting an OwnedLiteral type instead of using rust's built-in primitives.

This commit is contained in:
Tom Alexander 2020-05-16 21:52:44 -04:00
parent dd160c5708
commit 6519add838
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -45,6 +45,22 @@ impl From<&RValue<'_>> for OwnedRValue {
}
}
#[derive(Clone, Debug)]
pub enum OwnedLiteral {
LString(String),
LPositiveInteger(u64),
}
impl From<&OwnedRValue> for OwnedLiteral {
fn from(original: &OwnedRValue) -> Self {
match original {
OwnedRValue::RVPath(_) => panic!("Cannot convert a path to a literal"),
OwnedRValue::RVString(text) => OwnedLiteral::LString(text.clone()),
OwnedRValue::RVPositiveInteger(num) => OwnedLiteral::LPositiveInteger(num.clone()),
}
}
}
#[derive(Debug)]
pub struct ParametersContext {
params: HashMap<String, OwnedRValue>,
@ -129,6 +145,50 @@ impl CompareContextElement for ParametersContext {
}
}
impl ContextElement for OwnedLiteral {}
impl Renderable for OwnedLiteral {
fn render(&self, _filters: &Vec<Filter>) -> Result<String, RenderError> {
match self {
OwnedLiteral::LString(text) => Ok(text.clone()),
OwnedLiteral::LPositiveInteger(num) => Ok(num.to_string()),
}
}
}
impl Loopable for OwnedLiteral {
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
match self {
OwnedLiteral::LString(text) => {
if text.is_empty() {
Vec::new()
} else {
vec![text]
}
}
OwnedLiteral::LPositiveInteger(num) => vec![num],
}
}
}
impl Walkable for OwnedLiteral {
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, WalkError> {
Err(WalkError::CantWalk)
}
}
impl CompareContextElement for OwnedLiteral {
fn equals(&self, other: &dyn ContextElement) -> bool {
// TODO
false
}
fn partial_compare(&self, other: &dyn ContextElement) -> Option<Ordering> {
// TODO
None
}
}
impl ContextElement for String {}
impl Renderable for String {