Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

Trim pipe preset IDs during parsing (#3747)

Isaac Sanchez-Hawkins6f0e0e197042
diff --git a/crates/screenpipe-core/src/pipes/mod.rs b/crates/screenpipe-core/src/pipes/mod.rs
index 9088dc2c7..0d9e3013c 100644
--- a/crates/screenpipe-core/src/pipes/mod.rs
+++ b/crates/screenpipe-core/src/pipes/mod.rs
@@ -176,6 +176,11 @@ where
{
use serde::de;
+ fn normalized_preset(s: &str) -> Option<String> {
+ let trimmed = s.trim();
+ (!trimmed.is_empty()).then(|| trimmed.to_string())
+ }
+
struct PresetVisitor;
impl<'de> de::Visitor<'de> for PresetVisitor {
@@ -186,11 +191,7 @@ where
}
fn visit_str<E: de::Error>(self, v: &str) -> Result<Vec<String>, E> {
- if v.is_empty() {
- Ok(vec![])
- } else {
- Ok(vec![v.to_string()])
- }
+ Ok(normalized_preset(v).into_iter().collect())
}
fn visit_none<E: de::Error>(self) -> Result<Vec<String>, E> {
@@ -204,7 +205,7 @@ where
fn visit_seq<A: de::SeqAccess<'de>>(self, mut seq: A) -> Result<Vec<String>, A::Error> {
let mut result = Vec::new();
while let Some(s) = seq.next_element::<String>()? {
- if !s.is_empty() {
+ if let Some(s) = normalized_preset(&s) {
result.push(s);
}
}
@@ -4959,6 +4960,14 @@ mod tests {
assert!(config.provider.is_none());
}
+ #[test]
+ fn test_parse_frontmatter_trims_preset_ids() {
+ let content = "---\nschedule: every 1h\nenabled: true\npreset:\n - \" primary \"\n - \" \"\n - \"fallback \"\n---\n\nBody";
+ let (config, body) = parse_frontmatter(content).unwrap();
+ assert_eq!(config.preset, vec!["primary", "fallback"]);
+ assert_eq!(body, "Body");
+ }
+
#[test]
fn test_parse_frontmatter_missing_delimiter() {
let result = parse_frontmatter("no frontmatter here");
diff --git a/crates/screenpipe-core/src/pipes/preset_fallback.rs b/crates/screenpipe-core/src/pipes/preset_fallback.rs
index a5af45b50..b63c9e202 100644
--- a/crates/screenpipe-core/src/pipes/preset_fallback.rs
+++ b/crates/screenpipe-core/src/pipes/preset_fallback.rs
@@ -384,10 +384,15 @@ fn now_epoch() -> u64 {
/// ```
pub fn parse_preset_list(value: &serde_json::Value) -> Vec<String> {
match value {
- serde_json::Value::String(s) if !s.is_empty() => vec![s.clone()],
+ serde_json::Value::String(s) => s
+ .trim()
+ .is_empty()
+ .then(Vec::new)
+ .unwrap_or_else(|| vec![s.trim().to_string()]),
serde_json::Value::Array(arr) => arr
.iter()
.filter_map(|v| v.as_str())
+ .map(str::trim)
.filter(|s| !s.is_empty())
.map(|s| s.to_string())
.collect(),
@@ -423,6 +428,12 @@ mod tests {
assert!(parse_preset_list(&v).is_empty());
}
+ #[test]
+ fn test_parse_preset_list_trims_whitespace() {
+ let v = serde_json::json!([" primary ", " ", "fallback "]);
+ assert_eq!(parse_preset_list(&v), vec!["primary", "fallback"]);
+ }
+
#[test]
fn test_breaker_trip_and_recovery() {
let mut b = PresetBreaker::default();