Skip to content

Commit

Permalink
Merge pull request #34 from k9withabone/service-network_mode-container
Browse files Browse the repository at this point in the history
feat(service)!: support `network_mode: container:{name}`
  • Loading branch information
k9withabone authored Oct 15, 2024
2 parents de3af7f + 34b86f3 commit 95f2a93
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/service/network_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ pub enum NetworkMode {
/// Gives the container access to the specified service only.
Service(Identifier),

/// Gives the service container access to the specified container.
Container(Identifier),

/// Other network mode.
Other(String),
}
Expand All @@ -217,6 +220,9 @@ impl NetworkMode {
/// [`Self::Service`] string prefix.
const SERVICE_PREFIX: &'static str = "service:";

/// [`Self::Container`] string prefix.
const CONTAINER_PREFIX: &'static str = "container:";

/// Parse a [`NetworkMode`] from a string.
///
/// # Errors
Expand All @@ -234,6 +240,8 @@ impl NetworkMode {
Ok(Self::Host)
} else if let Some(service) = s.strip_prefix(Self::SERVICE_PREFIX) {
service.parse().map(Self::Service).map_err(Into::into)
} else if let Some(container) = s.strip_prefix(Self::CONTAINER_PREFIX) {
container.parse().map(Self::Container).map_err(Into::into)
} else {
Ok(Self::Other(network_mode.into()))
}
Expand Down Expand Up @@ -275,6 +283,26 @@ impl NetworkMode {
}
}

/// Returns `true` if the network mode is [`Container`].
///
/// [`Container`]: NetworkMode::Container
#[must_use]
pub const fn is_container(&self) -> bool {
matches!(self, Self::Container(..))
}

/// Returns [`Some`] if the network mode is [`Container`].
///
/// [`Container`]: NetworkMode::Container
#[must_use]
pub const fn as_container(&self) -> Option<&Identifier> {
if let Self::Container(v) = self {
Some(v)
} else {
None
}
}

/// Returns `true` if the network mode is [`Other`].
///
/// [`Other`]: NetworkMode::Other
Expand All @@ -298,7 +326,7 @@ impl NetworkMode {

/// Error returned when [parsing](NetworkMode::parse()) a [`NetworkMode`] from a string.
#[derive(Error, Debug, Clone, Copy, PartialEq, Eq)]
#[error("error parsing service network mode")]
#[error("error parsing service or container network mode")]
pub struct ParseNetworkModeError(#[from] InvalidIdentifierError);

impl_from_str!(NetworkMode => ParseNetworkModeError);
Expand All @@ -309,6 +337,7 @@ impl Display for NetworkMode {
Self::None => f.write_str(Self::NONE),
Self::Host => f.write_str(Self::HOST),
Self::Service(service) => write!(f, "{}{service}", Self::SERVICE_PREFIX),
Self::Container(container) => write!(f, "{}{container}", Self::CONTAINER_PREFIX),
Self::Other(other) => f.write_str(other),
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/test-full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ services:
network_mode-service:
network_mode: service:service

network_mode-container:
network_mode: container:container

network_mode-other:
network_mode: other

Expand Down

0 comments on commit 95f2a93

Please sign in to comment.