inspect-core 0.1.0

Core types and traits for the inspect-rs introspection system
Documentation
//! Kind enumeration for inspectable values.

#[cfg(not(feature = "std"))]
use alloc::borrow::Cow;
#[cfg(feature = "std")]
use std::borrow::Cow;

/// The kind of value being inspected.
///
/// This represents the fundamental shape and type of a value, including
/// scalar types, containers, and structured data.
#[derive(Debug, Clone, PartialEq)]
pub enum Kind<'a> {
    /// The unit type `()`.
    Unit,

    /// A boolean value.
    Bool(bool),

    /// A character.
    Char(char),

    /// A signed 8-bit integer.
    I8(i8),

    /// A signed 16-bit integer.
    I16(i16),

    /// A signed 32-bit integer.
    I32(i32),

    /// A signed 64-bit integer.
    I64(i64),

    /// A signed 128-bit integer.
    I128(i128),

    /// A pointer-sized signed integer.
    Isize(isize),

    /// An unsigned 8-bit integer.
    U8(u8),

    /// An unsigned 16-bit integer.
    U16(u16),

    /// An unsigned 32-bit integer.
    U32(u32),

    /// An unsigned 64-bit integer.
    U64(u64),

    /// An unsigned 128-bit integer.
    U128(u128),

    /// A pointer-sized unsigned integer.
    Usize(usize),

    /// A 32-bit floating point number.
    F32(f32),

    /// A 64-bit floating point number.
    F64(f64),

    /// A string value.
    Str(Cow<'a, str>),

    /// A byte sequence.
    Bytes(&'a [u8]),

    /// A struct with named fields.
    Struct,

    /// A tuple with positional fields.
    Tuple,

    /// A tuple struct with positional fields.
    TupleStruct,

    /// A sequence (Vec, slice, array).
    Sequence,

    /// A map (HashMap, BTreeMap).
    Map,

    /// A set (HashSet, BTreeSet).
    Set,

    /// An enum with a selected variant.
    Enum,

    /// An Option value.
    Option,

    /// A Result value.
    Result,

    /// An opaque value that doesn't expose internal structure.
    Opaque,
}

impl<'a> Kind<'a> {
    /// Returns a human-readable name for this kind.
    pub fn name(&self) -> &'static str {
        match self {
            Kind::Unit => "()",
            Kind::Bool(_) => "bool",
            Kind::Char(_) => "char",
            Kind::I8(_) => "i8",
            Kind::I16(_) => "i16",
            Kind::I32(_) => "i32",
            Kind::I64(_) => "i64",
            Kind::I128(_) => "i128",
            Kind::Isize(_) => "isize",
            Kind::U8(_) => "u8",
            Kind::U16(_) => "u16",
            Kind::U32(_) => "u32",
            Kind::U64(_) => "u64",
            Kind::U128(_) => "u128",
            Kind::Usize(_) => "usize",
            Kind::F32(_) => "f32",
            Kind::F64(_) => "f64",
            Kind::Str(_) => "str",
            Kind::Bytes(_) => "bytes",
            Kind::Struct => "struct",
            Kind::Tuple => "tuple",
            Kind::TupleStruct => "tuple_struct",
            Kind::Sequence => "sequence",
            Kind::Map => "map",
            Kind::Set => "set",
            Kind::Enum => "enum",
            Kind::Option => "Option",
            Kind::Result => "Result",
            Kind::Opaque => "opaque",
        }
    }

    /// Returns true if this is a scalar kind (primitives without children).
    pub fn is_scalar(&self) -> bool {
        matches!(
            self,
            Kind::Unit
                | Kind::Bool(_)
                | Kind::Char(_)
                | Kind::I8(_)
                | Kind::I16(_)
                | Kind::I32(_)
                | Kind::I64(_)
                | Kind::I128(_)
                | Kind::Isize(_)
                | Kind::U8(_)
                | Kind::U16(_)
                | Kind::U32(_)
                | Kind::U64(_)
                | Kind::U128(_)
                | Kind::Usize(_)
                | Kind::F32(_)
                | Kind::F64(_)
                | Kind::Str(_)
                | Kind::Bytes(_)
        )
    }
}