use serde::de::DeserializeOwned;
use serde::Serialize;
use crate::descriptor::WireFormat;
#[cfg(debug_assertions)]
pub const WIRE_FORMAT: WireFormat = WireFormat::Json;
#[cfg(not(debug_assertions))]
pub const WIRE_FORMAT: WireFormat = WireFormat::Bincode;
#[derive(Debug, thiserror::Error)]
pub enum WireError {
#[error("json wire error: {0}")]
Json(#[from] serde_json::Error),
#[error("bincode wire error: {0}")]
Bincode(#[from] bincode::Error),
}
#[cfg(debug_assertions)]
pub fn serialize<T: Serialize>(val: &T) -> Result<Vec<u8>, WireError> {
serde_json::to_vec(val).map_err(WireError::Json)
}
#[cfg(debug_assertions)]
pub fn deserialize<T: DeserializeOwned>(bytes: &[u8]) -> Result<T, WireError> {
serde_json::from_slice(bytes).map_err(WireError::Json)
}
#[cfg(not(debug_assertions))]
pub fn serialize<T: Serialize>(val: &T) -> Result<Vec<u8>, WireError> {
bincode::serialize(val).map_err(WireError::Bincode)
}
#[cfg(not(debug_assertions))]
pub fn deserialize<T: DeserializeOwned>(bytes: &[u8]) -> Result<T, WireError> {
bincode::deserialize(bytes).map_err(WireError::Bincode)
}