use std::borrow::Cow;
use sha2::Digest;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum TargetEncoding {
Os,
Url,
}
pub(crate) fn encode(content: &str, target: TargetEncoding) -> String {
let encoded = urlencoding::encode(content);
let encoded = encoded.replace("%20", " ");
let encoded = encoded.replace("%40", "@");
if target == TargetEncoding::Url {
urlencoding::encode(&encoded).to_string()
} else {
encoded
}
}
pub(crate) fn limit_str_len<'a>(s: &'a str) -> Cow<'a, str> {
const MAX_PATH_PART_LEN: usize = 200;
if s.len() > MAX_PATH_PART_LEN {
let mut hash = sha2::Sha256::new();
hash.update(s.as_bytes());
Cow::Owned(base16ct::lower::encode_string(&hash.finalize()))
} else {
Cow::Borrowed(s)
}
}