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
impl Word
Sourcepub fn parse_tilde_front(&mut self)
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.
Sourcepub fn parse_tilde_everywhere(&mut self)
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.
Sourcepub fn parse_tilde_everywhere_after(&mut self, index: usize)
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 FromStr for Word
Converts a string to a word.
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§impl MaybeLiteral for Word
impl MaybeLiteral for Word
Source§fn extend_literal<T: Extend<char>>(
&self,
result: &mut T,
) -> Result<(), NotLiteral>
fn extend_literal<T: Extend<char>>( &self, result: &mut T, ) -> Result<(), NotLiteral>
self
to an extendable object. Read moreSource§fn to_string_if_literal(&self) -> Option<String>
fn to_string_if_literal(&self) -> Option<String>
self
is literal and, if so, converts to a string.Source§impl TryFrom<Word> for Assign
Fallible conversion from a word into an assignment
impl TryFrom<Word> for Assign
Fallible conversion from a word into an assignment
Source§fn try_from(word: Word) -> Result<Assign, Word>
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
.
impl Eq for Word
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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