commit 0354ed712923d3d18e9cc317ca36d8a5bf3d6ea5
parent 1f0fe381dba78e85fa4a5fc244c61816d9b4efca
Author: triesap <triesap@radroots.dev>
Date: Mon, 19 Jan 2026 06:49:00 +0000
app-utils: add str_cap helper
- add str_cap helper for title casing
- handle empty or missing input values
- export str_cap from utils text module
- add unit tests for capitalization
Diffstat:
2 files changed, 25 insertions(+), 2 deletions(-)
diff --git a/crates/utils/src/lib.rs b/crates/utils/src/lib.rs
@@ -6,5 +6,5 @@ pub mod text;
pub mod types;
pub use errors::{err_msg, handle_err, throw_err, ERR_PREFIX_APP, ERR_PREFIX_UTILS};
-pub use text::{text_dec, text_enc, ROOT_SYMBOL};
+pub use text::{str_cap, text_dec, text_enc, ROOT_SYMBOL};
pub use types::{resolve_err, resolve_ok, ResolveError, ResultPass};
diff --git a/crates/utils/src/text/mod.rs b/crates/utils/src/text/mod.rs
@@ -10,9 +10,22 @@ pub fn text_dec(data: &[u8]) -> String {
String::from_utf8_lossy(data).to_string()
}
+pub fn str_cap(value: Option<&str>) -> String {
+ let Some(value) = value else {
+ return String::new();
+ };
+ let mut chars = value.chars();
+ let Some(first) = chars.next() else {
+ return String::new();
+ };
+ let mut output = first.to_uppercase().collect::<String>();
+ output.push_str(chars.as_str());
+ output
+}
+
#[cfg(test)]
mod tests {
- use super::{text_dec, text_enc, ROOT_SYMBOL};
+ use super::{str_cap, text_dec, text_enc, ROOT_SYMBOL};
#[test]
fn root_symbol_matches_spec() {
@@ -26,4 +39,14 @@ mod tests {
let decoded = text_dec(&encoded);
assert_eq!(decoded, "radroots");
}
+
+ #[test]
+ fn str_cap_handles_none() {
+ assert_eq!(str_cap(None), "");
+ }
+
+ #[test]
+ fn str_cap_uppercases_first_letter() {
+ assert_eq!(str_cap(Some("radroots")), "Radroots");
+ }
}