Skip to content

Commit e6bf7e6

Browse files
committed
added to_float
1 parent 6a12a29 commit e6bf7e6

4 files changed

Lines changed: 49 additions & 3 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
* 0.2.6
22
* `Either` now works by doing truthiness checks on state
33
* `Backend::full_screen()` convenience function
4+
* `to_float` template function
5+
* `PathBuf`s can now be used as templates
46
* 0.2.5
57
* BUGFIX: component reuse in if / else
68
* `with` statement

anathema-templates/src/components.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,33 @@ pub trait ToSourceKind {
2929
}
3030
}
3131

32-
impl<T: AsRef<str>> ToSourceKind for T {
32+
impl ToSourceKind for String {
3333
fn to_path(self) -> SourceKind {
34-
SourceKind::Path(self.as_ref().into())
34+
SourceKind::Path(self.into())
3535
}
3636

3737
fn to_template(self) -> SourceKind {
38-
SourceKind::Str(self.as_ref().into())
38+
SourceKind::Str(self)
39+
}
40+
}
41+
42+
impl ToSourceKind for &str {
43+
fn to_path(self) -> SourceKind {
44+
SourceKind::Path(self.into())
45+
}
46+
47+
fn to_template(self) -> SourceKind {
48+
SourceKind::Str(self.into())
49+
}
50+
}
51+
52+
impl ToSourceKind for PathBuf {
53+
fn to_path(self) -> SourceKind {
54+
SourceKind::Path(self)
55+
}
56+
57+
fn to_template(self) -> SourceKind {
58+
panic!("PathBuf can not be a template, only a path to one")
3959
}
4060
}
4161

anathema-value-resolver/src/functions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ impl FunctionTable {
5959
inner.insert("to_lower".into(), Function::from(string::to_lower));
6060
inner.insert("to_str".into(), Function::from(string::to_str));
6161
inner.insert("to_int".into(), Function::from(number::to_int));
62+
inner.insert("to_float".into(), Function::from(number::to_float));
6263
inner.insert("round".into(), Function::from(number::round));
6364
inner.insert("contains".into(), Function::from(list::contains));
6465
Self { inner }

anathema-value-resolver/src/functions/number.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,29 @@ pub(super) fn to_int<'bp>(args: &[ValueKind<'bp>]) -> ValueKind<'bp> {
3030
}
3131
}
3232

33+
pub(super) fn to_float<'bp>(args: &[ValueKind<'bp>]) -> ValueKind<'bp> {
34+
if args.len() != 1 {
35+
return ValueKind::Null;
36+
}
37+
38+
match &args[0] {
39+
ValueKind::Int(i) => ValueKind::Float(*i as f64),
40+
ValueKind::Float(f) => ValueKind::Float(*f),
41+
ValueKind::Bool(b) if !b => ValueKind::Float(0.0),
42+
ValueKind::Bool(_) => ValueKind::Float(1.0),
43+
ValueKind::Char(c) => match c.to_digit(10) {
44+
Some(i) => ValueKind::Float(i as f64),
45+
None => ValueKind::Null,
46+
},
47+
ValueKind::Hex(hex) => ValueKind::Float(hex.as_u32() as f64),
48+
ValueKind::Str(s) => match s.parse() {
49+
Ok(i) => ValueKind::Float(i),
50+
Err(_) => ValueKind::Null,
51+
},
52+
_ => ValueKind::Null,
53+
}
54+
}
55+
3356
pub(super) fn round<'bp>(args: &[ValueKind<'bp>]) -> ValueKind<'bp> {
3457
if args.is_empty() || args.len() > 2 {
3558
return ValueKind::Null;

0 commit comments

Comments
 (0)