use crate::error::ApiError;
use serde::de::DeserializeOwned;
pub trait FromBytes: Sized {
fn from_bytes(bytes: Vec<u8>) -> Result<Self, ApiError>;
}
impl FromBytes for Vec<u8> {
fn from_bytes(bytes: Vec<u8>) -> Result<Self, ApiError> {
Ok(bytes)
}
}
impl FromBytes for String {
fn from_bytes(bytes: Vec<u8>) -> Result<Self, ApiError> {
String::from_utf8(bytes).map_err(|e| ApiError::Custom(e.to_string()))
}
}
impl FromBytes for bool {
fn from_bytes(bytes: Vec<u8>) -> Result<Self, ApiError> {
from_bytes_json(bytes)
}
}
pub fn from_bytes_json<T: DeserializeOwned>(bytes: Vec<u8>) -> Result<T, ApiError> {
serde_json::from_slice(&bytes).map_err(ApiError::from)
}