Coverage for op
@@ -30,6 +30,14 @@ use regex::Regex;
use std::borrow::Cow;
use tremor_script::Value;
+lazy_static::lazy_static! {
+ static ref LINE_REGEXP: Regex = {
+ #[allow(clippy::unwrap_used)]
+ // ALLOW: we tested this
+ Regex::new(r" at line \d+ column \d+$").unwrap()
+ };
+}
+
/// Response type for operator callbacks returning both events and insights
#[derive(Default, Clone, PartialEq, Debug)]
pub struct EventAndInsights {
@@ -137,15 +145,30 @@ pub trait ConfigImpl {
// serialize the YAML config and deserialize it again, so that we get extra info on
// YAML errors here (eg: name of the config key where the errror occured). can just
// use serde_yaml::from_value() here, but the error message there is limited.
- match serde_yaml::from_str(&serde_yaml::to_string(config)?) {
- Ok(c) => Ok(c),
- Err(e) => {
+ serde_yaml::from_str(&serde_yaml::to_string(config)?).map_err(|e| {
// remove the potentially misleading "at line..." info, since it does not
// correspond to the numbers in the file now
- let re = Regex::new(r" at line \d+ column \d+$")?;
- let e_cleaned = re.replace(&e.to_string(), "").into_owned();
- Err(e_cleaned.into())
+ LINE_REGEXP.replace(&e.to_string(), "").to_string().into()
+ })
}
}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ #[test]
+ fn error() {
+ #[derive(serde::Deserialize)]
+ struct C {
+ _s: String,
+ };
+ impl ConfigImpl for C {};
+
+ let y: serde_yaml::Value = serde_yaml::from_str("5").unwrap();
+
+ let e = C::new(&y).err().unwrap().to_string();
+
+ assert_eq!(e, "invalid type: integer `5`, expected struct C")
}
}
