Struct nondestructive::yaml::ValueMut
source · pub struct ValueMut<'a> { /* private fields */ }
Expand description
A mutable value inside of a document.
Implementations§
source§impl<'a> ValueMut<'a>
impl<'a> ValueMut<'a>
sourcepub fn into_any_mut(self) -> AnyMut<'a>
pub fn into_any_mut(self) -> AnyMut<'a>
Coerce into AnyMut
to help discriminate the value type.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(
r#"
Hello World
"#
)?;
assert!(matches!(doc.as_mut().into_any_mut(), yaml::AnyMut::Scalar(..)));
let mut doc = yaml::from_slice(
r#"
number1: 10
number2: 20
"#
)?;
assert!(matches!(doc.as_mut().into_any_mut(), yaml::AnyMut::Mapping(..)));
let mut doc = yaml::from_slice(
r#"
- 10
- 20
"#
)?;
assert!(matches!(doc.as_mut().into_any_mut(), yaml::AnyMut::Sequence(..)));
sourcepub fn as_ref(&self) -> Value<'_>
pub fn as_ref(&self) -> Value<'_>
Coerce a mutable value as an immutable Value.
This is useful to be able to directly use methods only available on Value.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(
r#"
number1: 10
number2: 20
mapping:
inner: 400
string3: "I am a quoted string!"
"#
)?;
let mut root = doc.as_mut().into_mapping_mut().context("missing root mapping")?;
assert_eq!(root.get_mut("number1").and_then(|v| v.as_ref().as_u32()), Some(10));
assert_eq!(root.get_mut("number2").and_then(|v| v.as_ref().as_u32()), Some(20));
let mut mapping = root.get_mut("mapping").and_then(|v| v.into_mapping_mut()).context("missing inner mapping")?;
assert_eq!(mapping.get_mut("inner").and_then(|v| v.as_ref().as_u32()), Some(400));
assert_eq!(root.get_mut("string3").and_then(|v| v.into_ref().as_str()), Some("I am a quoted string!"));
sourcepub fn into_ref(self) -> Value<'a>
pub fn into_ref(self) -> Value<'a>
Coerce a mutable value into an immutable Value with the lifetime of the current reference.
This is useful to be able to directly use methods only available on Value.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(
r#"
number1: 10
number2: 20
mapping:
inner: 400
string3: "I am a quoted string!"
"#
)?;
let mut root = doc.as_mut().into_mapping_mut().context("missing root mapping")?;
assert_eq!(root.get_mut("number1").and_then(|v| v.into_ref().as_u32()), Some(10));
assert_eq!(root.get_mut("number2").and_then(|v| v.into_ref().as_u32()), Some(20));
let mut mapping = root.get_mut("mapping").and_then(|v| v.into_mapping_mut()).context("missing inner mapping")?;
assert_eq!(mapping.get_mut("inner").and_then(|v| v.into_ref().as_u32()), Some(400));
assert_eq!(root.get_mut("string3").and_then(|v| v.into_ref().as_str()), Some("I am a quoted string!"));
sourcepub fn as_mapping_mut(&mut self) -> Option<MappingMut<'_>>
pub fn as_mapping_mut(&mut self) -> Option<MappingMut<'_>>
Convert the value into a mutable MappingMut
.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(
r#"
number1: 10
number2: 20
mapping:
inner: 400
string3: "I am a quoted string!"
"#
)?;
let mut root = doc.as_mut();
let mut root = root.as_mapping_mut().context("missing root mapping")?;
root.get_mut("number2").context("missing number2")?.set_u32(30);
assert_eq!(
doc.to_string(),
r#"
number1: 10
number2: 30
mapping:
inner: 400
string3: "I am a quoted string!"
"#
);
sourcepub fn into_mapping_mut(self) -> Option<MappingMut<'a>>
pub fn into_mapping_mut(self) -> Option<MappingMut<'a>>
Convert the value into a mutable MappingMut
with the same lifetime as
the one associated with this value.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(
r#"
number1: 10
number2: 20
mapping:
inner: 400
string3: "I am a quoted string!"
"#
)?;
let mut root = doc.as_mut().into_mapping_mut().context("missing root mapping")?;
root.get_mut("number2").context("missing number2")?.set_u32(30);
root.get_mut("string3").context("missing string3")?.set_string("i-am-a-bare-string");
assert_eq!(
doc.to_string(),
r#"
number1: 10
number2: 30
mapping:
inner: 400
string3: i-am-a-bare-string
"#
);
let mut root = doc.as_mut().into_mapping_mut().context("missing root mapping")?;
root.get_mut("string3").context("missing string3")?.set_string("It's \n a good day!");
assert_eq!(
doc.to_string(),
r#"
number1: 10
number2: 30
mapping:
inner: 400
string3: "It's \n a good day!"
"#
);
sourcepub fn as_sequence_mut(&mut self) -> Option<SequenceMut<'_>>
pub fn as_sequence_mut(&mut self) -> Option<SequenceMut<'_>>
Convert the value into a mutable SequenceMut
.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(
r#"
- 10
- 20
- inner: 400
- "I am a quoted string!"
"#
)?;
let mut root = doc.as_mut();
let mut root = root.as_sequence_mut().context("missing root sequence")?;
root.get_mut(1).context("missing inner mapping")?.set_u32(30);
assert_eq!(
doc.to_string(),
r#"
- 10
- 30
- inner: 400
- "I am a quoted string!"
"#
);
sourcepub fn into_sequence_mut(self) -> Option<SequenceMut<'a>>
pub fn into_sequence_mut(self) -> Option<SequenceMut<'a>>
Convert the value into a mutable SequenceMut
with the same lifetime as
the one associated with this value.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(
r#"
- 10
- 20
- inner: 400
- "I am a quoted string!"
"#
)?;
let mut root = doc.as_mut();
let mut root = root.into_sequence_mut().context("missing root sequence")?;
root.get_mut(1).context("missing inner mapping")?.set_u32(30);
assert_eq!(
doc.to_string(),
r#"
- 10
- 30
- inner: 400
- "I am a quoted string!"
"#
);
source§impl<'a> ValueMut<'a>
impl<'a> ValueMut<'a>
sourcepub fn set_null(&mut self, kind: Null)
pub fn set_null(&mut self, kind: Null)
Replace the current value with the specified null value.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(" string")?;
doc.as_mut().set_null(yaml::Null::Keyword);
assert_eq!(doc.to_string(), " null");
doc.as_mut().set_null(yaml::Null::Tilde);
assert_eq!(doc.to_string(), " ~");
doc.as_mut().set_null(yaml::Null::Empty);
assert_eq!(doc.to_string(), " ");
sourcepub fn set_string<S>(&mut self, string: S)
pub fn set_string<S>(&mut self, string: S)
Set the value as a string.
The StringKind
used will follow a fairly simple heuristic documented
below, if this is not suitable then ValueMut::set_string_with
may be
used.
The heuristic used is:
StringKind::Single
will be used if the leading digit is numeric.StringKind::Double
will be used if either a single'
is encounted, or a non-graphical component that requires escaping.- Otherwise,
StringKind::Bare
is used.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(" string")?;
doc.as_mut().set_string("i-am-a-string");
assert_eq!(doc.to_string(), " i-am-a-string");
doc.as_mut().set_string("I am a string");
assert_eq!(doc.to_string(), " I am a string");
doc.as_mut().set_string("I am a\n string");
assert_eq!(doc.to_string(), " \"I am a\\n string\"");
doc.as_mut().set_string("I am a string with \"quotes\"");
assert_eq!(doc.to_string(), " I am a string with \"quotes\"");
doc.as_mut().set_string("null");
assert_eq!(doc.to_string(), " 'null'");
doc.as_mut().set_string("1.65");
assert_eq!(doc.to_string(), " '1.65'");
doc.as_mut().set_string("rust@1.65");
assert_eq!(doc.to_string(), " rust@1.65");
sourcepub fn set_string_with<S>(&mut self, string: S, kind: StringKind)
pub fn set_string_with<S>(&mut self, string: S, kind: StringKind)
Set the value as a string with a custom StringKind
.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(" string")?;
doc.as_mut().set_string_with("i-am-a-string", yaml::StringKind::Double);
assert_eq!(doc.to_string(), " \"i-am-a-string\"");
doc.as_mut().set_string_with("It's a great success!", yaml::StringKind::Single);
assert_eq!(doc.to_string(), " 'It''s a great success!'");
sourcepub fn set_block<I>(&mut self, iter: I, block: Block)
pub fn set_block<I>(&mut self, iter: I, block: Block)
Set the value as a literal block.
This takes an iterator, which will be used to construct the block. The
underlying value type produced is in fact a string, and can be read
through methods such as Value::as_str
.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(
r#"
string
"#
)?;
doc.as_mut().set_block(["foo", "bar", "baz"], yaml::Block::Literal(yaml::Chomp::Clip));
assert_eq!(doc.as_ref().as_str(), Some("foo\nbar\nbaz\n"));
assert_eq!(
doc.to_string(),
r#"
|
foo
bar
baz
"#
);
doc.as_mut().set_block(["foo", "bar", "baz"], yaml::Block::Literal(yaml::Chomp::Keep));
assert_eq!(doc.as_ref().as_str(), Some("foo\nbar\nbaz\n"));
assert_eq!(
doc.to_string(),
r#"
|+
foo
bar
baz
"#
);
doc.as_mut().set_block(["foo", "bar", "baz"], yaml::Block::Literal(yaml::Chomp::Strip));
assert_eq!(doc.as_ref().as_str(), Some("foo\nbar\nbaz"));
assert_eq!(
doc.to_string(),
r#"
|-
foo
bar
baz
"#
);
doc.as_mut().set_block(["foo", "bar", "baz"], yaml::Block::Folded(yaml::Chomp::Clip));
assert_eq!(doc.as_ref().as_str(), Some("foo bar baz\n"));
assert_eq!(
doc.to_string(),
r#"
>
foo
bar
baz
"#
);
doc.as_mut().set_block(["foo", "bar", "baz"], yaml::Block::Folded(yaml::Chomp::Keep));
assert_eq!(doc.as_ref().as_str(), Some("foo bar baz\n"));
assert_eq!(
doc.to_string(),
r#"
>+
foo
bar
baz
"#
);
doc.as_mut().set_block(["foo", "bar", "baz"], yaml::Block::Folded(yaml::Chomp::Strip));
assert_eq!(doc.as_ref().as_str(), Some("foo bar baz"));
assert_eq!(
doc.to_string(),
r#"
>-
foo
bar
baz
"#
);
sourcepub fn set_bool(&mut self, value: bool)
pub fn set_bool(&mut self, value: bool)
Set the value as a boolean.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(" string")?;
doc.as_mut().set_bool(true);
assert_eq!(doc.to_string(), " true");
sourcepub fn set_f32(&mut self, value: f32)
pub fn set_f32(&mut self, value: f32)
Set the value as a 32-bit float.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice("10")?;
let value = doc.as_mut().set_f32(10.42);
assert_eq!(doc.to_string(), "10.42");
sourcepub fn set_f64(&mut self, value: f64)
pub fn set_f64(&mut self, value: f64)
Set the value as a 64-bit float.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice("10")?;
let value = doc.as_mut().set_f64(10.42);
assert_eq!(doc.to_string(), "10.42");
sourcepub fn set_u8(&mut self, value: u8)
pub fn set_u8(&mut self, value: u8)
Set the value as a 8-bit unsigned integer.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(" 10")?;
let value = doc.as_mut().set_u8(42);
assert_eq!(doc.to_string(), " 42");
sourcepub fn set_i8(&mut self, value: i8)
pub fn set_i8(&mut self, value: i8)
Set the value as a 8-bit signed integer.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(" 10")?;
let value = doc.as_mut().set_i8(-42);
assert_eq!(doc.to_string(), " -42");
sourcepub fn set_u16(&mut self, value: u16)
pub fn set_u16(&mut self, value: u16)
Set the value as a 16-bit unsigned integer.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(" 10")?;
let value = doc.as_mut().set_u16(42);
assert_eq!(doc.to_string(), " 42");
sourcepub fn set_i16(&mut self, value: i16)
pub fn set_i16(&mut self, value: i16)
Set the value as a 16-bit signed integer.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(" 10")?;
let value = doc.as_mut().set_i16(-42);
assert_eq!(doc.to_string(), " -42");
sourcepub fn set_u32(&mut self, value: u32)
pub fn set_u32(&mut self, value: u32)
Set the value as a 32-bit unsigned integer.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(" 10")?;
let value = doc.as_mut().set_u32(42);
assert_eq!(doc.to_string(), " 42");
sourcepub fn set_i32(&mut self, value: i32)
pub fn set_i32(&mut self, value: i32)
Set the value as a 32-bit signed integer.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(" 10")?;
let value = doc.as_mut().set_i32(-42);
assert_eq!(doc.to_string(), " -42");
sourcepub fn set_u64(&mut self, value: u64)
pub fn set_u64(&mut self, value: u64)
Set the value as a 64-bit unsigned integer.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(" 10")?;
let value = doc.as_mut().set_u64(42);
assert_eq!(doc.to_string(), " 42");
sourcepub fn set_i64(&mut self, value: i64)
pub fn set_i64(&mut self, value: i64)
Set the value as a 64-bit signed integer.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(" 10")?;
let value = doc.as_mut().set_i64(-42);
assert_eq!(doc.to_string(), " -42");
sourcepub fn set_u128(&mut self, value: u128)
pub fn set_u128(&mut self, value: u128)
Set the value as a 128-bit unsigned integer.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(" 10")?;
let value = doc.as_mut().set_u128(42);
assert_eq!(doc.to_string(), " 42");
sourcepub fn set_i128(&mut self, value: i128)
pub fn set_i128(&mut self, value: i128)
Set the value as a 128-bit signed integer.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(" 10")?;
let value = doc.as_mut().set_i128(-42);
assert_eq!(doc.to_string(), " -42");
sourcepub fn make_mapping(self) -> MappingMut<'a>
pub fn make_mapping(self) -> MappingMut<'a>
Make the value into a mapping, unless it already is one.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(
r#"
string
"#
)?;
let mut mapping = doc.as_mut().make_mapping();
mapping.insert_u32("first", 1);
mapping.insert_u32("second", 2);
assert_eq!(
doc.to_string(),
r#"
first: 1
second: 2
"#
);
let mut doc = yaml::from_slice(
r#"
first: second
"#
)?;
let mut mapping = doc.as_mut().into_mapping_mut().and_then(|m| Some(m.get_into_mut("first")?.make_mapping())).context("missing first")?;
mapping.insert_u32("second", 2);
mapping.insert_u32("third", 3);
assert_eq!(
doc.to_string(),
r#"
first:
second: 2
third: 3
"#
);
sourcepub fn make_sequence(self) -> SequenceMut<'a>
pub fn make_sequence(self) -> SequenceMut<'a>
Make the value into a sequence, unless it already is one.
§Examples
use anyhow::Context;
use nondestructive::yaml;
let mut doc = yaml::from_slice(
r#"
string
"#
)?;
let mut sequence = doc.as_mut().make_sequence();
sequence.push_u32(1);
sequence.push_u32(2);
assert_eq!(
doc.to_string(),
r#"
- 1
- 2
"#
);
let mut doc = yaml::from_slice(
r#"
first: second
"#
)?;
let mut sequence = doc.as_mut().into_mapping_mut().and_then(|m| Some(m.get_into_mut("first")?.make_sequence())).context("missing first")?;
sequence.push_u32(2);
sequence.push_u32(3);
assert_eq!(
doc.to_string(),
r#"
first:
- 2
- 3
"#
);