Skip to main content

testcontainers_build/sources/
folder.rs

1use crate::{BuildContext, BuildContextProvider, ContextLocation, Error, Result};
2use async_trait::async_trait;
3use std::path::PathBuf;
4
5/// Use an existing folder as a build context.
6/// Do not copy or delete the folder.
7pub struct FolderSource {
8    /// Set the caller-owned folder path.
9    /// The folder must exist when the source is resolved.
10    pub path: PathBuf,
11}
12
13impl FolderSource {
14    /// Set the folder path.
15    /// Validate the folder when the source is resolved.
16    pub fn new(path: impl Into<PathBuf>) -> Self {
17        Self { path: path.into() }
18    }
19}
20
21#[async_trait]
22impl BuildContextProvider for FolderSource {
23    async fn resolve(&self) -> Result<BuildContext> {
24        let path = dunce::canonicalize(&self.path)?;
25        if !path.is_dir() {
26            return Err(Error::InvalidInput(
27                "folder source must be a directory".into(),
28            ));
29        }
30        Ok(BuildContext::new(ContextLocation::Directory(path)))
31    }
32}