Enum spinoso_symbol::IdentifierType [−][src]
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
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));
Identifier that is a global variable name.
Global variables are prefixed with the sigil $
. There are two types of
global variables:
$
followed by aIdentifierType::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()));
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()));
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()));
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));
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));
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]
fn clone(&self) -> IdentifierType
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl Copy for IdentifierType
[src]
impl Debug for IdentifierType
[src]
impl Default for IdentifierType
[src]
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.
fn from_str(s: &str) -> Result<Self, Self::Err>
[src]
impl Hash for IdentifierType
[src]
fn hash<__H: Hasher>(&self, state: &mut __H)
[src]
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl Ord for IdentifierType
[src]
fn cmp(&self, other: &IdentifierType) -> Ordering
[src]
#[must_use]pub fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]
impl PartialEq<IdentifierType> for IdentifierType
[src]
fn eq(&self, other: &IdentifierType) -> bool
[src]
#[must_use]pub fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl PartialOrd<IdentifierType> for IdentifierType
[src]
fn partial_cmp(&self, other: &IdentifierType) -> Option<Ordering>
[src]
#[must_use]pub fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn le(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn ge(&self, other: &Rhs) -> bool
1.0.0[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.
fn try_from(value: &[u8]) -> Result<Self, Self::Error>
[src]
impl TryFrom<&'_ str> for IdentifierType
[src]
Auto Trait Implementations
impl RefUnwindSafe for IdentifierType
impl Send for IdentifierType
impl Sync for IdentifierType
impl Unpin for IdentifierType
impl UnwindSafe for IdentifierType
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,