Struct compose_yml::v2::RawOr [] [src]

pub struct RawOr<T>(_)
where
    T: InterpolatableValue
;

Either an unparsed interpolation string, or a fully-parsed value. We use this representation because:

  1. Almost any string value in docker-compose.yml may contain an environment variable interpolation of the form "$VAR" or "${VAR}", and we normally want to preserve these values in their uninterpolated form when manipulating docker-compose.yml files.
  2. When we do actually need to manipate a complex string field of a docker-compose.yml file, we prefer to do it using the parsed representation.

Hence RawOr<T>, which can represent both unparsed and parsed values, and switch between them in a controlled fashion.

We normally create RawOr<T> values using one of value, escape or raw, as shown below.

use std::string::ToString;
use compose_yml::v2 as dc;

// We can call `escape`, `value` and `raw` with explicit type
// parameters using the following syntax.
assert_eq!("bridge",
           dc::escape::<dc::NetworkMode, _>("bridge").unwrap().to_string());

// But typically, when working with `RawOr`, we'll be passing values
// into a context where the type is known, allowing type interference
// to supply type parameters to the `value`, `escape` and `raw` functions
// automatically.  So let's simulate that using a helper function.
fn nm_string(nm: dc::RawOr<dc::NetworkMode>) -> String {
  nm.to_string()
}

// This is how we'll normally create `RawOr` values.
assert_eq!("bridge", nm_string(dc::value(dc::NetworkMode::Bridge)));
assert_eq!("bridge", nm_string(dc::escape("bridge").unwrap()));
assert_eq!("container:$$FOO", nm_string(dc::escape("container:$FOO").unwrap()));
assert_eq!("$NETWORK_MODE", nm_string(dc::raw("$NETWORK_MODE").unwrap()));

// If we call `escape`, we have to pass it a string which parses to
// correct type, or it will return an error.  Similar rules apply to `raw`
// if no actual interpolations are present in the string.  This is part of
// our "verify as much as possible" philosophy.
assert!(dc::escape::<dc::NetworkMode, _>("invalid").is_err());
assert!(dc::raw::<dc::NetworkMode, _>("invalid").is_err());

Methods

impl<T> RawOr<T> where
    T: InterpolatableValue, 
[src]

Either return a &T for this RawOr<T>, or return an error if parsing the value would require performing interpolation.

use compose_yml::v2 as dc;

let bridge = dc::value(dc::NetworkMode::Bridge);
assert_eq!(bridge.value().unwrap(), &dc::NetworkMode::Bridge);

Either return a mutable &mut T for this RawOr<T>, or return an error if parsing the value would require performing interpolation.

use compose_yml::v2 as dc;

let mut mode = dc::value(dc::NetworkMode::Bridge);
*mode.value_mut().unwrap() = dc::NetworkMode::Host;
assert_eq!(mode.value_mut().unwrap(), &dc::NetworkMode::Host);

Return a &mut T for this RawOr<T>, performing any necessary environment variable interpolations using the supplied env object and updating the value in place.

Return a &mut T for this RawOr<T>, performing any necessary environment variable interpolations using the system environment and updating the value in place.

use std::env;
use std::str::FromStr;
use compose_yml::v2 as dc;

env::set_var("NETWORK_MODE", "host");
let mut mode: dc::RawOr<dc::NetworkMode> =
  FromStr::from_str("$NETWORK_MODE").unwrap();

// Before interpolation.
assert_eq!("$NETWORK_MODE", mode.to_string());

// Interpolate.
assert_eq!(mode.interpolate().unwrap(), &dc::NetworkMode::Host);

// After interpolation.
assert_eq!("host", mode.to_string());

Trait Implementations

impl<T: Debug> Debug for RawOr<T> where
    T: InterpolatableValue, 
[src]

Formats the value using the given formatter.

impl<T: Clone> Clone for RawOr<T> where
    T: InterpolatableValue, 
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<T: PartialEq> PartialEq for RawOr<T> where
    T: InterpolatableValue, 
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<T: Eq> Eq for RawOr<T> where
    T: InterpolatableValue, 
[src]

impl<T: InterpolatableValue> MergeOverride for RawOr<T>
[src]

InterpolatableValue is basically just a string that we parse for internal use, so we can merge it as though it were a simple string, without getting into the internal details of whatever it might contain. So go ahead and use the default implementation of MergeOverride as if we were a primitive type.

Given this value and an override value, merge the override value into this one, producing a new value. Read more

impl<T> Display for RawOr<T> where
    T: InterpolatableValue, 
[src]

Formats the value using the given formatter. Read more

impl<T> Serialize for RawOr<T> where
    T: InterpolatableValue, 
[src]

Serializes this value into this serializer.

impl<T> FromStr for RawOr<T> where
    T: InterpolatableValue, 
[src]

The associated error which can be returned from parsing.

Parses a string s to return a value of this type. Read more

impl<T> Deserialize for RawOr<T> where
    T: InterpolatableValue, 
[src]

Deserialize this value given this Deserializer.

impl<T: InterpolatableValue> InterpolateAll for RawOr<T>
[src]

Recursively walk over this type, interpolating all RawOr values containing references to the environment. The default implementation leaves a value unchanged. Read more