Enum spinoso_symbol::IdentifierType[][src]

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

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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Construct a “junk” identifier type.

Examples

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

The associated error which can be returned from parsing.

Parses a string s to return a value of this type. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.