1use serde::{Deserialize, Deserializer};
2use lexica::{Blob, StrongRef};
3
4// see https://deer.social/profile/did:plc:63y3oh7iakdueqhlj6trojbq/post/3ltuv4skhqs2h
5pub fn safe_string<'de, D: Deserializer<'de>>(deserializer: D) -> Result<String, D::Error> {
6 let str = String::deserialize(deserializer)?;
7
8 Ok(str.replace('\u{0000}', ""))
9}
10
11pub fn blob_ref(blob: Option<Blob>) -> Option<String> {
12 blob.map(|blob| blob.cid.to_string())
13}
14
15pub fn strongref_to_parts(
16 strongref: Option<&StrongRef>,
17) -> (Option<String>, Option<String>) {
18 strongref
19 .map(|sr| (sr.uri.clone(), sr.cid.to_string()))
20 .unzip()
21}
22
23pub fn empty_str_as_none(input: String) -> Option<String> {
24 match input.is_empty() {
25 true => None,
26 false => Some(input),
27 }
28}
29
30pub fn u64_from_ivec(val: sled::IVec) -> Option<u64> {
31 if val.len() == 8 {
32 let bytes = val[0..8].try_into().ok()?;
33 Some(u64::from_le_bytes(bytes))
34 } else {
35 None
36 }
37}