Struct Word

Source
pub struct Word {
    pub units: Vec<WordUnit>,
    pub location: Location,
}
Expand description

Token that may involve expansions and quotes

A word is a sequence of word units. It depends on context whether an empty word is valid or not. It is your responsibility to ensure a word is non-empty in a context where it cannot.

The difference between words and texts is that only words can contain single- and double-quotes and tilde expansions. Compare WordUnit and TextUnit.

Fields§

§units: Vec<WordUnit>

Word units that constitute the word

§location: Location

Position of the word in the source code

Implementations§

Source§

impl Word

Source

pub fn parse_tilde_front(&mut self)

Parses a tilde expansion at the beginning of the word.

This function checks if self.units begins with an unquoted tilde character, i.e., WordUnit::Unquoted(TextUnit::Literal('~')). If so, the word unit is replaced with a WordUnit::Tilde value. Other unquoted characters that follow the tilde are together replaced to produce the value of the WordUnit::Tilde.

let mut word = Word::from_str("~").unwrap();
word.parse_tilde_front();
assert_eq!(word.units, [Tilde { name: "".to_string(), followed_by_slash: false }]);
let mut word = Word::from_str("~foo").unwrap();
word.parse_tilde_front();
assert_eq!(word.units, [Tilde { name: "foo".to_string(), followed_by_slash: false }]);

If there is no leading tilde, self.units will have the same content when this function returns.

let mut word = Word::from_str("X").unwrap();
assert_eq!(word.units, [Unquoted(Literal('X'))]);
word.parse_tilde_front();
assert_eq!(word.units, [Unquoted(Literal('X'))]);

This function parses a literal word units only, which differs from the strictly POSIX-conforming behavior. For example, POSIX requires the word ~$() to be regarded as a tilde expansion, but this function does not convert it to WordUnit::Tilde("$()".to_string()).

This function only parses a tilde expansion at the beginning of the word. If the word is a colon-separated list of paths, you might want to use parse_tilde_everywhere instead.

The tilde expansion is delimited by an unquoted slash. Unlike parse_tilde_everywhere, unquoted colons are not considered as delimiters.

Source

pub fn parse_tilde_everywhere(&mut self)

Parses tilde expansions in the word.

This function works the same as parse_tilde_front except that it parses tilde expansions not only at the beginning of the word but also after each unquoted colon.

let mut word = Word::from_str("~:~a/b:~c").unwrap();
word.parse_tilde_everywhere();
assert_eq!(
    word.units,
    [
        Tilde { name: "".to_string(), followed_by_slash: false },
        Unquoted(Literal(':')),
        Tilde { name: "a".to_string(), followed_by_slash: true },
        Unquoted(Literal('/')),
        Unquoted(Literal('b')),
        Unquoted(Literal(':')),
        Tilde { name: "c".to_string(), followed_by_slash: false },
    ]
);

See also parse_tilde_everywhere_after, which allows you to parse tilde expansions only after a specified index.

Source

pub fn parse_tilde_everywhere_after(&mut self, index: usize)

Parses tilde expansions in the word after the specified index.

This function works the same as parse_tilde_everywhere except that it starts parsing tilde expansions after the specified index of self.units. Tilde expansions are parsed at the specified index and after each unquoted colon.

let mut word = Word::from_str("~=~a/b:~c").unwrap();
word.parse_tilde_everywhere_after(2);
assert_eq!(
    word.units,
    [
        // The initial tilde is not parsed because it is before index 2.
        Unquoted(Literal('~')),
        Unquoted(Literal('=')),
        // This tilde is parsed because it is at index 2,
        // even though it is not after a colon.
        Tilde { name: "a".to_string(), followed_by_slash: true },
        Unquoted(Literal('/')),
        Unquoted(Literal('b')),
        Unquoted(Literal(':')),
        Tilde { name: "c".to_string(), followed_by_slash: false },
    ]
);

Compare parse_tilde_everywhere, which is equivalent to parse_tilde_everywhere_after(0).

Trait Implementations§

Source§

impl Clone for Word

Source§

fn clone(&self) -> Word

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 Word

Source§

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

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

impl Display for Word

Source§

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

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

impl FromStr for Word

Converts a string to a word.

This implementation does not parse any tilde expansions in the word. To parse them, you need to call Word::parse_tilde_front or Word::parse_tilde_everywhere on the resultant word.

Source§

type Err = Error

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

fn from_str(s: &str) -> Result<Word, Error>

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

impl MaybeLiteral for Word

Source§

fn extend_literal<T: Extend<char>>( &self, result: &mut T, ) -> Result<(), NotLiteral>

Appends the literal representation of self to an extendable object. Read more
Source§

fn to_string_if_literal(&self) -> Option<String>

Checks if self is literal and, if so, converts to a string.
Source§

impl PartialEq for Word

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl TryFrom<Word> for Assign

Fallible conversion from a word into an assignment

Source§

fn try_from(word: Word) -> Result<Assign, Word>

Converts a word into an assignment.

For a successful conversion, the word must be of the form name=value, where name is a non-empty literal word, = is an unquoted equal sign, and value is a word. If the input word does not match this syntax, it is returned intact in Err.

Source§

type Error = Word

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

impl Unquote for Word

Source§

fn write_unquoted<W: Write>(&self, w: &mut W) -> Result<bool, Error>

Converts self to a string with all quotes removed and writes to w.
Source§

fn unquote(&self) -> (String, bool)

Converts self to a string with all quotes removed. Read more
Source§

impl Eq for Word

Source§

impl StructuralPartialEq for Word

Auto Trait Implementations§

§

impl Freeze for Word

§

impl !RefUnwindSafe for Word

§

impl !Send for Word

§

impl !Sync for Word

§

impl Unpin for Word

§

impl !UnwindSafe for Word

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.