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
No known key found for this signature in database
GPG key ID: 0E496D15096376BE
4 changed files with 18 additions and 17 deletions

View file

@ -14,7 +14,7 @@ use tokio::sync::{mpsc, watch};
use garage_util::background::*;
use garage_util::data::*;
use garage_util::encode::nonversioned_encode;
use garage_util::encode::{nonversioned_encode, debug_serialize};
use garage_util::error::{Error, OkOrMessage};
use garage_rpc::ring::*;

View file

@ -140,19 +140,3 @@ pub fn fasthash(data: &[u8]) -> FastHash {
pub fn gen_uuid() -> Uuid {
rand::thread_rng().gen::<[u8; 32]>().into()
}
/// 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),
}
}

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),
}
}

View file

@ -7,6 +7,7 @@ use err_derive::Error;
use serde::{de::Visitor, Deserialize, Deserializer, Serialize, Serializer};
use crate::data::*;
use crate::encode::debug_serialize;
/// Regroup all Garage errors
#[derive(Debug, Error)]