Expand description
compose_spec is a library for (de)serializing from/to the Compose specification.
This library attempts to make interacting with and creating Compose files as idiomatic and correct as possible.
PathBufs are used for fields which denote a path.- Enums are used for fields which conflict with each other.
- Values are fully parsed and validated when they have a defined format.
- Lists that must contain unique values use
IndexSet, otherwise they areVecs. - Strings which represent a span of time are converted to/from
Durations, see thedurationmodule.
Note that the Deserialize implementations of many types make use of
Deserializer::deserialize_any(). This means that
you should only attempt to deserialize them from self-describing formats like YAML or JSON.
§Examples
use compose_spec::{Compose, Service, service::Image};
let yaml = "\
services:
caddy:
image: docker.io/library/caddy:latest
ports:
- 8000:80
- 8443:443
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- caddy-data:/data
volumes:
caddy-data:
";
// Deserialize `Compose`
let compose: Compose = serde_yaml::from_str(yaml)?;
// Serialize `Compose`
let value = serde_yaml::to_value(&compose)?;
// Get the `Image` of the "caddy" service
let caddy: Option<&Service> = compose.services.get("caddy");
let image: &Option<Image> = &caddy.unwrap().image;
let image: &Image = image.as_ref().unwrap();
assert_eq!(image, "docker.io/library/caddy:latest");
assert_eq!(image.name(), "docker.io/library/caddy");
assert_eq!(image.tag(), Some("latest"));§Short or Long Syntax Values
Many values within the Compose specification can be represented in either a short or long
syntax. The enum ShortOrLong is used to for these values. Conversion from the Short
syntax to the Long syntax is always possible. The AsShort trait is used for Long
syntax types which may be represented directly as the Short syntax type if additional
options are not set.
§Fragments
serde_yaml does use YAML anchors and aliases during deserialization. However, it does not
automatically merge the YAML merge type (<< keys). You
can use serde_yaml::Value::apply_merge() to merge << keys into the surrounding mapping.
Options::apply_merge() is available to do this for you.
use compose_spec::Compose;
let yaml = "\
services:
one:
environment: &env
FOO: foo
BAR: bar
two:
environment: *env
three:
environment:
<<: *env
BAR: baz
";
let compose = Compose::options()
.apply_merge(true)
.from_yaml_str(yaml)?;Re-exports§
pub use self::config::Config;pub use self::network::Network;pub use self::secret::Secret;pub use self::service::Service;
Modules§
- config
- Provides
Configfor the top-levelconfigsfield of aComposefile. - duration
- Utilities for working with
Durations. - network
- Provides
Networkfor the top-levelnetworksfield of aComposefile. - secret
- Provides
Secretfor the top-levelsecretsfield of aComposefile. - service
- Provides
Servicefor theComposetop-levelservicesfield.
Structs§
- Compose
- The Compose file is a YAML file defining a containers based application.
- Extension
Key - Valid extension key string.
- Identifier
- Validated identifier for use as a map key in a
Composefile. - Include
- A Compose sub-project to include.
- MapKey
- Valid map key string.
- Name
- Validated
Composeproject name. - Options
- Deserialization options builder for a
Composefile. - Validation
Error - Error returned when validation of a
Composefile fails. - Volume
- A named volume which can be reused across multiple
Services.
Enums§
- Invalid
Extension KeyError - Error returned when attempting to create a
ExtensionKey. - Invalid
Identifier Error - Error returned when attempting to create a
Identifier. - Invalid
MapKey Error - Error returned when attempting to create a
MapKey. - Invalid
Name Error - Error returned when attempting to create a
Name. - Item
OrList - A single item or a list of unique items.
- List
OrMap - A list of unique strings or a map with optional single values.
- Number
- A numerical
Value. - Parse
Number Error - Error returned when parsing a
Numberfrom a string. - Resource
- A resource managed either externally or by the compose implementation, e.g.
a
NetworkorVolume. - Short
OrLong - Wrapper for types which may be represented as a
ShortorLongsyntax. - String
OrNumber - A string or number value.
- TryFrom
Number Error - Error returned when failing to convert a
Numberinto another type. - TryFrom
Value Error - Error returned when attempting to convert
Valueinto another type. - Value
- A single string, number, or boolean value.
- Yaml
Value - Represents any valid YAML value.
Traits§
- AsShort
- Trait for types that represent a long syntax which could also be represented in a short syntax.
- AsShort
Iter - Trait similar to
AsShortexcept it returns anIteratorinstead of a reference.
Type Aliases§
- Configs
- Configs allow
Services to adapt their behavior without needing to rebuild the container image. - Extensions
- Extensions can be used to enable experimental features or make a
Composefile easier to maintain via anchors and aliases. - Map
- Map with optional single values.
- Networks
- Named networks which allow for
Services to communicate with each other. - Secrets
- Sensitive data that a
Servicemay be granted access to. - Volumes
- Named volumes which can be reused across multiple
Services.