what2watch/src/util.rs

38 lines
1,020 B
Rust
Raw Normal View History

use std::{error::Error, ops::Range};
use unicode_segmentation::UnicodeSegmentation;
pub fn validate_optional_length<E: Error>(
opt: &Option<String>,
len_range: Range<usize>,
err: E,
) -> Result<Option<String>, E> {
if let Some(opt) = opt {
let opt = opt.trim();
let len = opt.graphemes(true).size_hint().1.unwrap();
if !len_range.contains(&len) {
Err(err)
} else {
Ok(Some(opt.to_string()))
}
} else {
Ok(None)
}
2023-05-29 00:55:16 +00:00
}
/// Serde deserialization decorator to map empty Strings to None,
pub fn empty_string_as_none<'de, D, T>(de: D) -> Result<Option<T>, D::Error>
where
D: serde::Deserializer<'de>,
T: std::str::FromStr,
T::Err: std::fmt::Display,
{
let opt = <Option<String> as serde::Deserialize>::deserialize(de)?;
match opt.as_deref() {
None | Some("") => Ok(None),
Some(s) => std::str::FromStr::from_str(s)
.map_err(serde::de::Error::custom)
.map(Some),
}
}