testcontainers-build 0.1.0

Build Testcontainers images from folders, remote contexts, and pluggable Git providers
Documentation
use crate::{ImageBuildBackend, Result};
use std::sync::Arc;
use testcontainers::{ContainerRequest, GenericImage};

/// Hold the unique local image reference.
/// Remove all containers before you call remove.
#[must_use = "call remove().await after disposing containers to clean up the built image"]
pub struct BuiltImage {
    reference: String,
    tag: String,
    backend: Arc<dyn ImageBuildBackend>,
    removed: bool,
}

impl BuiltImage {
    pub(crate) fn new(reference: String, tag: String, backend: Arc<dyn ImageBuildBackend>) -> Self {
        Self {
            reference,
            tag,
            backend,
            removed: false,
        }
    }

    /// Get the unique local image reference.
    /// Keep the image until all containers are removed.
    pub fn reference(&self) -> &str {
        &self.reference
    }

    /// Create a native Testcontainers image.
    /// Use it to add wait conditions and container settings.
    pub fn image(&self) -> GenericImage {
        GenericImage::new("testcontainers-build", &self.tag)
    }

    /// Create a request for the local image.
    /// Testcontainers can attempt a pull if the image is missing.
    pub fn container_request(&self) -> ContainerRequest<GenericImage> {
        self.image().into()
    }

    /// Remove the owned image reference.
    /// Retry this operation if removal fails.
    pub async fn remove(&mut self) -> Result<()> {
        if !self.removed {
            self.backend.remove(&self.reference).await?;
            self.removed = true;
        }
        Ok(())
    }
}