//! 分配结果定义

use core::{fmt::Display, result};

/// 分配错误
#[derive(Debug)]
pub enum KallocError {
    /// 内存不足
    Enomem,
    /// order 无效
    Eorder,
    /// 被分配
    Ealloced,
    /// 检查错误
    Echeck,
    /// 不是尾页
    Enotail,
    /// 不是复合页
    Enocomp,
}

/// 分配结果
pub type Result<T> = result::Result<T, KallocError>;

impl Display for KallocError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let error = match self {
            Self::Enomem => "Out of memory",
            Self::Eorder => "Order invalid",
            Self::Ealloced => "Alloced",
            Self::Echeck => "Check at free flag(s) set",
            Self::Enotail => "PageTail not set",
            Self::Enocomp => "Comp head not consistent",
        };
        write!(f, "{error}")
    }
}
