File tree Expand file tree Collapse file tree
anathema-value-resolver/src/functions Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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+
3356pub ( super ) fn round < ' bp > ( args : & [ ValueKind < ' bp > ] ) -> ValueKind < ' bp > {
3457 if args. is_empty ( ) || args. len ( ) > 2 {
3558 return ValueKind :: Null ;
You can’t perform that action at this time.
0 commit comments