Skip to content

Commit cf3fea5

Browse files
committed
fix: allow config Template to be a constant number (u64, i64, f64)
1 parent cbe70c3 commit cf3fea5

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/template.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,38 @@ pub enum TemplateRenderingError {
3636
MissingKeys { missing_keys: Vec<String> },
3737
}
3838

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+
3971
/// A templated field.
4072
///
4173
/// In many cases, components can be configured so that part of the component's functionality can be
@@ -50,7 +82,7 @@ pub enum TemplateRenderingError {
5082
#[configurable_component]
5183
#[configurable(metadata(docs::templateable))]
5284
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
53-
#[serde(try_from = "String", into = "String")]
85+
#[serde(try_from = "TemplateSource", into = "TemplateSource")]
5486
pub struct Template {
5587
src: String,
5688

@@ -67,6 +99,20 @@ pub struct Template {
6799
tz_offset: Option<FixedOffset>,
68100
}
69101

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+
70116
impl TryFrom<&str> for Template {
71117
type Error = TemplateParseError;
72118

0 commit comments

Comments
 (0)