Crate compose_spec

source ·
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 are Vecs.
  • Strings which represent a span of time are converted to/from Durations, see the duration module.

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.

Re-exports§

Modules§

Structs§

Enums§

Traits§

  • Trait for types that represent a long syntax which could also be represented in a short syntax.
  • Trait similar to AsShort except it returns an Iterator instead of a reference.

Type Aliases§