pub struct ValueMut<'a> { /* private fields */ }
Expand description

A mutable value inside of a document.

Implementations§

source§

impl<'a> ValueMut<'a>

source

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(..)));
source

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!"));
source

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!"));
source

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!"
    "#
);
source

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!"
    "#
);
source

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!"
    "#
);
source

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>

source

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(), "  ");
source

pub fn set_string<S>(&mut self, string: S)
where S: AsRef<str>,

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:

§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");
source

pub fn set_string_with<S>(&mut self, string: S, kind: StringKind)
where S: AsRef<str>,

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!'");
source

pub fn set_block<I>(&mut self, iter: I, block: Block)
where I: IntoIterator, I::Item: AsRef<str>,

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
    "#
);
source

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");
source

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");
source

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");
source

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");
source

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");
source

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");
source

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");
source

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");
source

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");
source

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");
source

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");
source

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");
source

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");
source

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
    "#
);
source

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
    "#
);

Auto Trait Implementations§

§

impl<'a> Freeze for ValueMut<'a>

§

impl<'a> RefUnwindSafe for ValueMut<'a>

§

impl<'a> Send for ValueMut<'a>

§

impl<'a> Sync for ValueMut<'a>

§

impl<'a> Unpin for ValueMut<'a>

§

impl<'a> !UnwindSafe for ValueMut<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V