move debug_serialize to garage_util::encode

This commit is contained in:
Alex Auvolat
2023-01-03 15:29:29 +01:00
parent 8d5505514f
commit a54b67740d
4 changed files with 18 additions and 17 deletions
+16
View File
@@ -24,3 +24,19 @@ where
{
rmp_serde::decode::from_read_ref::<_, T>(bytes)
}
/// Serialize to JSON, truncating long result
pub fn debug_serialize<T: Serialize>(x: T) -> String {
match serde_json::to_string(&x) {
Ok(ss) => {
if ss.len() > 100 {
// TODO this can panic if 100 is not a codepoint boundary, but inside a 2 Bytes
// (or more) codepoint
ss[..100].to_string()
} else {
ss
}
}
Err(e) => format!("<JSON serialization error: {}>", e),
}
}