use thiserror::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum CnResult {
#[error("Success")]
Success = 0,
#[error("Invalid asset type")]
AssetInvalidType,
#[error("Invalid state")]
InvalidState,
#[error("Invalid argument")]
InvalidArgument,
#[error("File I/O error")]
FileIo,
#[error("No state directory installed")]
NoStateRoot,
}
impl From<postcard::Error> for CnResult {
fn from(e: postcard::Error) -> Self {
tracing::error!("postcard error: {}", e);
CnResult::InvalidArgument
}
}
impl From<crate::blob::FrameError> for CnResult {
fn from(e: crate::blob::FrameError) -> Self {
tracing::error!("baked record did not decode: {}", e);
CnResult::InvalidArgument
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::string::{String, ToString};
#[test]
fn display_messages_are_stable() {
assert_eq!(CnResult::Success.to_string(), "Success");
assert_eq!(CnResult::AssetInvalidType.to_string(), "Invalid asset type");
assert_eq!(CnResult::InvalidState.to_string(), "Invalid state");
assert_eq!(CnResult::InvalidArgument.to_string(), "Invalid argument");
assert_eq!(CnResult::FileIo.to_string(), "File I/O error");
assert_eq!(
CnResult::NoStateRoot.to_string(),
"No state directory installed"
);
}
#[test]
fn frame_errors_map_to_invalid_argument() {
let bad = crate::blob::decode_exact::<String>(&[0xff]).unwrap_err();
assert_eq!(CnResult::from(bad), CnResult::InvalidArgument);
let trailing = crate::blob::decode_exact::<u8>(&[1, 2]).unwrap_err();
assert_eq!(CnResult::from(trailing), CnResult::InvalidArgument);
}
}