From f0c974519c4e357adf1ffe108eadcb860681ea2e Mon Sep 17 00:00:00 2001 From: mu2019 Date: Thu, 12 Oct 2023 11:22:19 +0800 Subject: [PATCH] feature(value): add JsonValue::array_insert Co-authored-by: mu2019 Co-authored-by: gierens --- src/value/mod.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/value/mod.rs b/src/value/mod.rs index fea7b4f..d0e8f34 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -609,6 +609,20 @@ impl JsonValue { } } + /// Works on `JsonValue::Array` - insert an entry at given index. + /// If the method is called on anything but an aray or if the index is out + /// of bounds, it will return an error. + pub fn array_insert(&mut self, index: usize, value: T) -> Result<()> + where T: Into { + match *self { + JsonValue::Array(ref mut vec) => { + vec.insert(index, value.into()); + Ok(()) + }, + _ => Err(Error::wrong_type("Array")) + } + } + /// When called on an array or an object, will wipe them clean. When called /// on a string will clear the string. Numbers and booleans become null. pub fn clear(&mut self) {