@@ -36,6 +36,38 @@ pub enum TemplateRenderingError {
36
36
MissingKeys { missing_keys : Vec < String > } ,
37
37
}
38
38
39
+ /// The source of a template. May be a constant (such as numeric values) or a template string.
40
+ #[ derive( Clone , Debug ) ]
41
+ #[ configurable_component]
42
+ #[ serde( untagged) ]
43
+ enum TemplateSource {
44
+ /// A signed number constant.
45
+ SignedNumber ( i64 ) ,
46
+ /// An unsigned number constant.
47
+ UnsignedNumber ( u64 ) ,
48
+ /// A floating-point number constant.
49
+ FloatingPointNumber ( f64 ) ,
50
+ /// A string, which may be a template.
51
+ String ( String ) ,
52
+ }
53
+
54
+ impl Default for TemplateSource {
55
+ fn default ( ) -> Self {
56
+ Self :: String ( Default :: default ( ) )
57
+ }
58
+ }
59
+
60
+ impl ToString for TemplateSource {
61
+ fn to_string ( & self ) -> String {
62
+ match self {
63
+ Self :: SignedNumber ( i) => i. to_string ( ) ,
64
+ Self :: UnsignedNumber ( u) => u. to_string ( ) ,
65
+ Self :: FloatingPointNumber ( f) => f. to_string ( ) ,
66
+ Self :: String ( s) => s. clone ( ) ,
67
+ }
68
+ }
69
+ }
70
+
39
71
/// A templated field.
40
72
///
41
73
/// In many cases, components can be configured so that part of the component's functionality can be
@@ -50,7 +82,7 @@ pub enum TemplateRenderingError {
50
82
#[ configurable_component]
51
83
#[ configurable( metadata( docs:: templateable) ) ]
52
84
#[ derive( Clone , Debug , Default , Eq , Hash , PartialEq ) ]
53
- #[ serde( try_from = "String " , into = "String " ) ]
85
+ #[ serde( try_from = "TemplateSource " , into = "TemplateSource " ) ]
54
86
pub struct Template {
55
87
src : String ,
56
88
@@ -67,6 +99,20 @@ pub struct Template {
67
99
tz_offset : Option < FixedOffset > ,
68
100
}
69
101
102
+ impl TryFrom < TemplateSource > for Template {
103
+ type Error = TemplateParseError ;
104
+
105
+ fn try_from ( src : TemplateSource ) -> Result < Self , Self :: Error > {
106
+ Template :: try_from ( src. to_string ( ) )
107
+ }
108
+ }
109
+
110
+ impl Into < TemplateSource > for Template {
111
+ fn into ( self ) -> TemplateSource {
112
+ TemplateSource :: String ( self . src . clone ( ) )
113
+ }
114
+ }
115
+
70
116
impl TryFrom < & str > for Template {
71
117
type Error = TemplateParseError ;
72
118
0 commit comments