MCPcopy Create free account
hub / github.com/chanced/jsonptr / replace

Method replace

src/pointer.rs:1006–1035  ·  view source on GitHub ↗

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>>,
    )

Source from the content-addressed store, hash-verified

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) {

Callers 1

replace_tokenFunction · 0.80

Calls 7

newFunction · 0.85
is_rootMethod · 0.80
countMethod · 0.80
tokensMethod · 0.80
lenMethod · 0.80
encodedMethod · 0.80
getMethod · 0.45

Tested by 1

replace_tokenFunction · 0.64