inspect-core 0.1.0

Core types and traits for the inspect-rs introspection system
Documentation
//! Field metadata.

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

use crate::Sensitivity;

/// Metadata about a struct field or tuple element.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldInfo<'a> {
    name: Option<Cow<'a, str>>,
    index: usize,
    type_name: Option<Cow<'a, str>>,
    sensitivity: Sensitivity,
}

impl<'a> FieldInfo<'a> {
    /// Create field information for a named field.
    pub fn named(name: impl Into<Cow<'a, str>>, index: usize) -> Self {
        Self { name: Some(name.into()), index, type_name: None, sensitivity: Sensitivity::Normal }
    }

    /// Create field information for a tuple field.
    pub fn tuple(index: usize) -> Self {
        Self { name: None, index, type_name: None, sensitivity: Sensitivity::Normal }
    }

    /// Set the type name for this field.
    pub fn with_type_name(mut self, type_name: impl Into<Cow<'a, str>>) -> Self {
        self.type_name = Some(type_name.into());
        self
    }

    /// Set the sensitivity level for this field.
    pub fn with_sensitivity(mut self, sensitivity: Sensitivity) -> Self {
        self.sensitivity = sensitivity;
        self
    }

    /// The field's name, if it has one (struct fields).
    pub fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }

    /// The field's index in its parent container.
    pub fn index(&self) -> usize {
        self.index
    }

    /// The field's type name, if available.
    pub fn type_name(&self) -> Option<&str> {
        self.type_name.as_deref()
    }

    /// The field's sensitivity classification.
    pub fn sensitivity(&self) -> Sensitivity {
        self.sensitivity
    }
}