use alloc::{format, string::String};
use crate::{policy::PolicyError, storage::StorageError};
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum VmPolicyError {
#[error("deserialize error: {0}")]
Deserialization(#[from] postcard::Error),
#[error("policy error: {0}")]
PolicyError(#[from] PolicyError),
#[error("storage error: {0}")]
StorageError(#[from] StorageError),
#[error("atribute error: {0}")]
Attribute(#[from] AttributeError),
#[error("unknown error")]
Unknown,
}
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
#[error("{0}")]
pub struct AttributeError(pub(crate) String);
impl AttributeError {
pub(crate) fn type_mismatch(cmd: &str, attr: &str, expected: &str, actual: &str) -> Self {
Self(format!("{cmd}::{attr} should be {expected}, was {actual}"))
}
pub(crate) fn exclusive(cmd: &str, attr1: &str, attr2: &str) -> Self {
Self(format!(
"{cmd} has both exclusive attributes {attr1} and {attr2}"
))
}
pub(crate) fn int_range(cmd: &str, attr: &str, min: i64, max: i64) -> Self {
Self(format!("{cmd}::{attr} must be within [{min}, {max}]"))
}
pub(crate) fn missing(cmd: &str, attrs: &str) -> Self {
Self(format!("{cmd} is missing {attrs}"))
}
}