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

Location of the first character of the word.

Implementations

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("".to_string())]);
let mut word = Word::from_str("~foo").unwrap();
word.parse_tilde_front();
assert_eq!(word.units, [Tilde("foo".to_string())]);

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()).

The tilde expansion are delimited by an unquoted slash or colon. This is also not strictly POSIX-conforming since POSIX allows colons to be included in the tilde expansion.

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.

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("".to_string()),
        Unquoted(Literal(':')),
        Tilde("a".to_string()),
        Unquoted(Literal('/')),
        Unquoted(Literal('b')),
        Unquoted(Literal(':')),
        Tilde("c".to_string()),
    ]
);

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

Formats the value using the given formatter. Read more

Converts a string to a word.

This function 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.

The associated error which can be returned from parsing.

Checks if self is literal and, if so, converts to a string and appends it to result. Read more

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

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

This method tests for !=.

Fallible conversion from a word into an assignment.

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.

The type returned in the event of a conversion error.

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

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

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)

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

Converts the given value to a String. 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.