Skip to content

Commit 8d3c82b

Browse files
committed
boot/categories: Trim description fields
1 parent f9d3a74 commit 8d3c82b

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

src/boot/categories.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ fn optional_string_from_toml<'a>(toml: &'a toml::value::Table, key: &str) -> &'a
4747
toml.get(key).and_then(toml::Value::as_str).unwrap_or("")
4848
}
4949

50+
fn process_description(description: &str) -> &str {
51+
description.trim()
52+
}
53+
5054
fn categories_from_toml(
5155
categories: &toml::value::Table,
5256
parent: Option<&Category>,
@@ -58,10 +62,13 @@ fn categories_from_toml(
5862
.as_table()
5963
.with_context(|| format!("category {slug} was not a TOML table"))?;
6064

65+
let description = optional_string_from_toml(details, "description");
66+
let description = process_description(description);
67+
6168
let category = Category::from_parent(
6269
slug,
6370
required_string_from_toml(details, "name")?,
64-
optional_string_from_toml(details, "description"),
71+
description,
6572
parent,
6673
);
6774

@@ -120,3 +127,43 @@ pub async fn sync_with_connection(toml_str: &str, conn: &mut AsyncPgConnection)
120127
})
121128
.await
122129
}
130+
131+
#[cfg(test)]
132+
mod tests {
133+
use super::*;
134+
135+
#[test]
136+
fn test_process_description() {
137+
// Leading whitespace
138+
assert_eq!(process_description(" description"), "description");
139+
assert_eq!(process_description("\tdescription"), "description");
140+
assert_eq!(process_description("\n\ndescription"), "description");
141+
142+
// Trailing whitespace
143+
assert_eq!(process_description("description "), "description");
144+
assert_eq!(process_description("description\t"), "description");
145+
assert_eq!(process_description("description\n\n"), "description");
146+
147+
// Both leading and trailing whitespace
148+
assert_eq!(process_description(" description "), "description");
149+
assert_eq!(process_description("\tdescription\t"), "description");
150+
assert_eq!(
151+
process_description("\n description with spaces \n"),
152+
"description with spaces"
153+
);
154+
155+
// Preserves internal whitespace
156+
assert_eq!(
157+
process_description(" multi word description "),
158+
"multi word description"
159+
);
160+
assert_eq!(
161+
process_description(" description\nwith\nnewlines "),
162+
"description\nwith\nnewlines"
163+
);
164+
165+
// Empty string
166+
assert_eq!(process_description(""), "");
167+
assert_eq!(process_description(" "), "");
168+
}
169+
}

0 commit comments

Comments
 (0)