ryo-symbol 0.1.0

Symbol system for Rust codebase - unique identifiers and file path management
Documentation
//! Variable scope for InSymbol (variables, parameters, fields)

use serde::{Deserialize, Serialize};

/// Variable scope type for InSymbol
///
/// Used to distinguish different kinds of "inner" symbols within
/// a containing symbol (function, struct, etc.).
///
/// # Path Format
/// - Parameter: `my_crate::my_fn::$param::x`
/// - Local variable: `my_crate::my_fn::$var::result`
/// - Struct field: `my_crate::MyStruct::$field::name`
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum VarScope {
    /// Function parameter: `$param`
    Param,
    /// Local variable: `$var`
    Local,
    /// Struct/enum field: `$field`
    Field,
}

impl VarScope {
    /// Get the scope segment string
    ///
    /// # Examples
    /// ```
    /// # use ryo_symbol::VarScope;
    /// assert_eq!(VarScope::Param.segment(), "$param");
    /// assert_eq!(VarScope::Local.segment(), "$var");
    /// assert_eq!(VarScope::Field.segment(), "$field");
    /// ```
    pub fn segment(&self) -> &'static str {
        match self {
            VarScope::Param => "$param",
            VarScope::Local => "$var",
            VarScope::Field => "$field",
        }
    }

    /// Parse from a segment string
    ///
    /// # Examples
    /// ```
    /// # use ryo_symbol::VarScope;
    /// assert_eq!(VarScope::from_segment("$param"), Some(VarScope::Param));
    /// assert_eq!(VarScope::from_segment("$var"), Some(VarScope::Local));
    /// assert_eq!(VarScope::from_segment("$field"), Some(VarScope::Field));
    /// assert_eq!(VarScope::from_segment("foo"), None);
    /// ```
    pub fn from_segment(s: &str) -> Option<Self> {
        match s {
            "$param" => Some(VarScope::Param),
            "$var" => Some(VarScope::Local),
            "$field" => Some(VarScope::Field),
            _ => None,
        }
    }

    /// Check if a string is a valid scope marker
    pub fn is_scope_marker(s: &str) -> bool {
        Self::from_segment(s).is_some()
    }
}

impl std::fmt::Display for VarScope {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.segment())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_var_scope_segment() {
        assert_eq!(VarScope::Param.segment(), "$param");
        assert_eq!(VarScope::Local.segment(), "$var");
        assert_eq!(VarScope::Field.segment(), "$field");
    }

    #[test]
    fn test_var_scope_from_segment() {
        assert_eq!(VarScope::from_segment("$param"), Some(VarScope::Param));
        assert_eq!(VarScope::from_segment("$var"), Some(VarScope::Local));
        assert_eq!(VarScope::from_segment("$field"), Some(VarScope::Field));
        assert_eq!(VarScope::from_segment("invalid"), None);
        assert_eq!(VarScope::from_segment("$invalid"), None);
    }

    #[test]
    fn test_var_scope_display() {
        assert_eq!(format!("{}", VarScope::Param), "$param");
        assert_eq!(format!("{}", VarScope::Local), "$var");
        assert_eq!(format!("{}", VarScope::Field), "$field");
    }
}