[][src]Enum spinoso_symbol::IdentifierType

pub enum IdentifierType {
    Junk,
    Global,
    Instance,
    Class,
    AttrSet,
    Constant,
    Local,
}

Valid types for Ruby identifiers.

Spinoso symbol parses bytestrings to determine if they are valid idents for the Inspect iterator (which requires the inspect Cargo feature to be enabled). Symbols that are valid idents do not get wrapped in " when generating their debug output.

See variant documentation for the set of ident types.

IdentifierType's primary interface is through the TryFrom and FromStr conversion traits. Parsing &str and &[u8] is supported.

Examples – local variable

assert_eq!("spinoso".parse::<IdentifierType>(), Ok(IdentifierType::Local));
assert_eq!("spinoso_symbol_features".parse::<IdentifierType>(), Ok(IdentifierType::Local));

Examples – constant

assert_eq!("Spinoso".parse::<IdentifierType>(), Ok(IdentifierType::Constant));
assert_eq!("SpinosoSymbol".parse::<IdentifierType>(), Ok(IdentifierType::Constant));
assert_eq!("SPINOSO_SYMBOL_FEATURES".parse::<IdentifierType>(), Ok(IdentifierType::Constant));

Examples – global

assert_eq!("$use_spinoso_symbol".parse::<IdentifierType>(), Ok(IdentifierType::Global));
assert_eq!("$USE_SPINOSO_SYMBOL".parse::<IdentifierType>(), Ok(IdentifierType::Global));

Examples – instance and class variables

assert_eq!("@artichoke".parse::<IdentifierType>(), Ok(IdentifierType::Instance));
assert_eq!("@@rumble".parse::<IdentifierType>(), Ok(IdentifierType::Class));

Example – attribute setter

Attribute setters are local idents that end in =.

assert_eq!("artichoke=".parse::<IdentifierType>(), Ok(IdentifierType::AttrSet));
assert_eq!("spinoso_symbol=".parse::<IdentifierType>(), Ok(IdentifierType::AttrSet));

Variants

Junk

Identifier that contains "junk".

Junk mostly equates to non-sigil ASCII symbols. Identifiers like empty? and flatten! are junk idents. All special symbolic Ruby methods like <=> and !~ are junk identifiers.

Examples

assert_eq!("empty?".parse::<IdentifierType>(), Ok(IdentifierType::Junk));
assert_eq!("flatten!".parse::<IdentifierType>(), Ok(IdentifierType::Junk));
assert_eq!("<=>".parse::<IdentifierType>(), Ok(IdentifierType::Junk));
assert_eq!("!~".parse::<IdentifierType>(), Ok(IdentifierType::Junk));
assert_eq!("[]".parse::<IdentifierType>(), Ok(IdentifierType::Junk));
assert_eq!("[]=".parse::<IdentifierType>(), Ok(IdentifierType::Junk));
assert_eq!("=~".parse::<IdentifierType>(), Ok(IdentifierType::Junk));
assert_eq!("==".parse::<IdentifierType>(), Ok(IdentifierType::Junk));
assert_eq!("===".parse::<IdentifierType>(), Ok(IdentifierType::Junk));
Global

Identifier that is a global variable name.

Global variables are prefixed with the sigil $. There are two types of global variables:

Examples

assert_eq!("$".parse::<IdentifierType>(), Err(ParseIdentifierError::new()));
assert_eq!("$foo".parse::<IdentifierType>(), Ok(IdentifierType::Global));
assert_eq!("$@foo".parse::<IdentifierType>(), Err(ParseIdentifierError::new()));
assert_eq!("$0".parse::<IdentifierType>(), Ok(IdentifierType::Global));
assert_eq!("$1".parse::<IdentifierType>(), Ok(IdentifierType::Global));
assert_eq!("$9".parse::<IdentifierType>(), Ok(IdentifierType::Global));
assert_eq!("$-w".parse::<IdentifierType>(), Ok(IdentifierType::Global));
assert_eq!("$-www".parse::<IdentifierType>(), Err(ParseIdentifierError::new()));
Instance

Identifier that is an instance variable name.

Instance variables are prefixed with a single @ sigil. The remaining bytes must be a valid Constant or Local ident.

Examples

assert_eq!("@".parse::<IdentifierType>(), Err(ParseIdentifierError::new()));
assert_eq!("@foo".parse::<IdentifierType>(), Ok(IdentifierType::Instance));
assert_eq!("@Foo".parse::<IdentifierType>(), Ok(IdentifierType::Instance));
assert_eq!("@FOO".parse::<IdentifierType>(), Ok(IdentifierType::Instance));
assert_eq!("@foo_bar".parse::<IdentifierType>(), Ok(IdentifierType::Instance));
assert_eq!("@FooBar".parse::<IdentifierType>(), Ok(IdentifierType::Instance));
assert_eq!("@FOO_BAR".parse::<IdentifierType>(), Ok(IdentifierType::Instance));
assert_eq!("@$foo".parse::<IdentifierType>(), Err(ParseIdentifierError::new()));
assert_eq!("@0".parse::<IdentifierType>(), Err(ParseIdentifierError::new()));
assert_eq!("@1".parse::<IdentifierType>(), Err(ParseIdentifierError::new()));
assert_eq!("@9".parse::<IdentifierType>(), Err(ParseIdentifierError::new()));
Class

Identifier that is a class variable name.

Class variables are prefixed with a double @@ sigil. The remaining bytes must be a valid Constant or Local ident.

Examples

assert_eq!("@@".parse::<IdentifierType>(), Err(ParseIdentifierError::new()));
assert_eq!("@@foo".parse::<IdentifierType>(), Ok(IdentifierType::Class));
assert_eq!("@@Foo".parse::<IdentifierType>(), Ok(IdentifierType::Class));
assert_eq!("@@FOO".parse::<IdentifierType>(), Ok(IdentifierType::Class));
assert_eq!("@@foo_bar".parse::<IdentifierType>(), Ok(IdentifierType::Class));
assert_eq!("@@FooBar".parse::<IdentifierType>(), Ok(IdentifierType::Class));
assert_eq!("@@FOO_BAR".parse::<IdentifierType>(), Ok(IdentifierType::Class));
assert_eq!("@@$foo".parse::<IdentifierType>(), Err(ParseIdentifierError::new()));
assert_eq!("@@0".parse::<IdentifierType>(), Err(ParseIdentifierError::new()));
assert_eq!("@@1".parse::<IdentifierType>(), Err(ParseIdentifierError::new()));
assert_eq!("@@9".parse::<IdentifierType>(), Err(ParseIdentifierError::new()));
AttrSet

Identifier that is an "attribute setter" method name.

AttrSet end in the = sigil and are otherwise valid Local or Constant idents. AttrSet idents cannot have any other "junk" symbols.

Examples

assert_eq!("Foo=".parse::<IdentifierType>(), Ok(IdentifierType::AttrSet));
assert_eq!("foo=".parse::<IdentifierType>(), Ok(IdentifierType::AttrSet));
assert_eq!("foo_bar=".parse::<IdentifierType>(), Ok(IdentifierType::AttrSet));
assert_eq!("foo_bar?=".parse::<IdentifierType>(), Err(ParseIdentifierError::new()));
assert_eq!("ω=".parse::<IdentifierType>(), Ok(IdentifierType::AttrSet));
Constant

Identifier that is a constant name.

Constant names can be either ASCII or Unicode and must start with a uppercase character.

Examples

assert_eq!("Foo".parse::<IdentifierType>(), Ok(IdentifierType::Constant));
assert_eq!("FOO".parse::<IdentifierType>(), Ok(IdentifierType::Constant));
assert_eq!("FooBar".parse::<IdentifierType>(), Ok(IdentifierType::Constant));
assert_eq!("FOO_BAR".parse::<IdentifierType>(), Ok(IdentifierType::Constant));
assert_eq!("Ω".parse::<IdentifierType>(), Ok(IdentifierType::Constant));
Local

Identifier that is a local variable or method name.

Local names can be either ASCII or Unicode and must start with a lowercase character.

Examples

assert_eq!("foo".parse::<IdentifierType>(), Ok(IdentifierType::Local));
assert_eq!("fOO".parse::<IdentifierType>(), Ok(IdentifierType::Local));
assert_eq!("fooBar".parse::<IdentifierType>(), Ok(IdentifierType::Local));
assert_eq!("foo_bar".parse::<IdentifierType>(), Ok(IdentifierType::Local));
assert_eq!("ω".parse::<IdentifierType>(), Ok(IdentifierType::Local));

Implementations

impl IdentifierType[src]

#[must_use]pub const fn new() -> Self[src]

Return a new, default IdentifierType.

Prefer to use new() over default() since new() is a const fn.

Examples

const ID_TYPE: IdentifierType = IdentifierType::new();
assert_eq!(ID_TYPE, IdentifierType::Junk);
assert_eq!(ID_TYPE, IdentifierType::default());

Trait Implementations

impl Clone for IdentifierType[src]

impl Copy for IdentifierType[src]

impl Debug for IdentifierType[src]

impl Default for IdentifierType[src]

pub fn default() -> Self[src]

Construct a "junk" identifier type.

Examples

const ID_TYPE: IdentifierType = IdentifierType::new();
assert_eq!(ID_TYPE, IdentifierType::Junk);
assert_eq!(ID_TYPE, IdentifierType::default());

impl Eq for IdentifierType[src]

impl FromStr for IdentifierType[src]

type Err = ParseIdentifierError

The associated error which can be returned from parsing.

impl Hash for IdentifierType[src]

impl Ord for IdentifierType[src]

impl PartialEq<IdentifierType> for IdentifierType[src]

impl PartialOrd<IdentifierType> for IdentifierType[src]

impl StructuralEq for IdentifierType[src]

impl StructuralPartialEq for IdentifierType[src]

impl TryFrom<&'_ [u8]> for IdentifierType[src]

type Error = ParseIdentifierError

The type returned in the event of a conversion error.

impl TryFrom<&'_ str> for IdentifierType[src]

type Error = ParseIdentifierError

The type returned in the event of a conversion error.

Auto Trait Implementations

impl RefUnwindSafe for IdentifierType[src]

impl Send for IdentifierType[src]

impl Sync for IdentifierType[src]

impl Unpin for IdentifierType[src]

impl UnwindSafe for IdentifierType[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.