Attempts to replace a `Token` by the index, returning the replaced `Token` if it already exists. Returns `None` otherwise. ## Errors A [`ReplaceError`] is returned if the index is out of bounds.
(
&mut self,
index: usize,
token: impl Into<Token<'t>>,
)
| 1004 | /// ## Errors |
| 1005 | /// A [`ReplaceError`] is returned if the index is out of bounds. |
| 1006 | pub fn replace<'t>( |
| 1007 | &mut self, |
| 1008 | index: usize, |
| 1009 | token: impl Into<Token<'t>>, |
| 1010 | ) -> Result<Option<Token>, ReplaceError> { |
| 1011 | if self.is_root() { |
| 1012 | return Err(ReplaceError { |
| 1013 | count: self.count(), |
| 1014 | index, |
| 1015 | }); |
| 1016 | } |
| 1017 | let mut tokens = self.tokens().collect::<Vec<_>>(); |
| 1018 | if index >= tokens.len() { |
| 1019 | return Err(ReplaceError { |
| 1020 | count: tokens.len(), |
| 1021 | index, |
| 1022 | }); |
| 1023 | } |
| 1024 | let old = tokens.get(index).map(super::token::Token::to_owned); |
| 1025 | tokens[index] = token.into(); |
| 1026 | |
| 1027 | let mut buf = String::new(); |
| 1028 | for token in tokens { |
| 1029 | buf.push('/'); |
| 1030 | buf.push_str(token.encoded()); |
| 1031 | } |
| 1032 | self.0 = buf; |
| 1033 | |
| 1034 | Ok(old) |
| 1035 | } |
| 1036 | |
| 1037 | /// Clears the `Pointer`, setting it to root (`""`). |
| 1038 | pub fn clear(&mut self) { |