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:
- 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 manipulatingdocker-compose.yml
files. - 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]
T: InterpolatableValue,
fn value(&self) -> Result<&T>
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);
fn value_mut(&mut self) -> Result<&mut T>
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);
fn interpolate_env(&mut self, env: &Environment) -> Result<&mut T>
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.
fn interpolate(&mut self) -> Result<&mut T>
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]
T: InterpolatableValue,
impl<T: Clone> Clone for RawOr<T> where
T: InterpolatableValue,
[src]
T: InterpolatableValue,
fn clone(&self) -> RawOr<T>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<T: PartialEq> PartialEq for RawOr<T> where
T: InterpolatableValue,
[src]
T: InterpolatableValue,
fn eq(&self, __arg_0: &RawOr<T>) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &RawOr<T>) -> bool
This method tests for !=
.
impl<T: Eq> Eq for RawOr<T> where
T: InterpolatableValue,
[src]
T: InterpolatableValue,
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.
fn merge_override(&self, ovr: &Self) -> Self
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]
T: InterpolatableValue,
impl<T> Serialize for RawOr<T> where
T: InterpolatableValue,
[src]
T: InterpolatableValue,
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where
S: Serializer,
S: Serializer,
Serializes this value into this serializer.
impl<T> FromStr for RawOr<T> where
T: InterpolatableValue,
[src]
T: InterpolatableValue,
type Err = Error
The associated error which can be returned from parsing.
fn from_str(s: &str) -> Result<Self, Self::Err>
Parses a string s
to return a value of this type. Read more
impl<T> Deserialize for RawOr<T> where
T: InterpolatableValue,
[src]
T: InterpolatableValue,
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error> where
D: Deserializer,
D: Deserializer,
Deserialize this value given this Deserializer
.
impl<T: InterpolatableValue> InterpolateAll for RawOr<T>
[src]
fn interpolate_all(&mut self) -> Result<()>
Recursively walk over this type, interpolating all RawOr
values containing references to the environment. The default implementation leaves a value unchanged. Read more