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

Valid types for Ruby identifiers.

Spinoso symbol parses byte strings 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:

  • $ followed by a IdentifierType::Ident sequence.
  • Special global variables, which include Regexp globals ($1..$9) and $-w type globals.

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 idents 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§

source§

impl IdentifierType

source

pub const fn new() -> Self

Return a new, default IdentifierType.

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

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

Trait Implementations§

source§

impl Clone for IdentifierType

source§

fn clone(&self) -> IdentifierType

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for IdentifierType

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for IdentifierType

source§

fn default() -> Self

Construct a “junk” identifier type.

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

impl FromStr for IdentifierType

§

type Err = ParseIdentifierError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

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

impl Hash for IdentifierType

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where
    H: Hasher,
    Self: Sized,

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

impl Ord for IdentifierType

source§

fn cmp(&self, other: &IdentifierType) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere
    Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere
    Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere
    Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<IdentifierType> for IdentifierType

source§

fn eq(&self, other: &IdentifierType) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<IdentifierType> for IdentifierType

source§

fn partial_cmp(&self, other: &IdentifierType) -> Option<Ordering>

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

fn lt(&self, other: &Rhs) -> bool

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

fn le(&self, other: &Rhs) -> bool

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

fn gt(&self, other: &Rhs) -> bool

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

fn ge(&self, other: &Rhs) -> bool

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

impl TryFrom<&[u8]> for IdentifierType

§

type Error = ParseIdentifierError

The type returned in the event of a conversion error.
source§

fn try_from(value: &[u8]) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<&str> for IdentifierType

§

type Error = ParseIdentifierError

The type returned in the event of a conversion error.
source§

fn try_from(value: &str) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl Copy for IdentifierType

source§

impl Eq for IdentifierType

source§

impl StructuralEq for IdentifierType

source§

impl StructuralPartialEq for IdentifierType

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere
    T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
    U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere
    T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
    U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.