testcontainers_build/
options.rs1use crate::{Error, Result, validate_relative};
2use std::{collections::BTreeMap, time::Duration};
3
4#[derive(Debug, Clone)]
7pub struct BuildOptions {
8 pub dockerfile: String,
11 pub build_arguments: BTreeMap<String, String>,
14 pub target: Option<String>,
17 pub platform: Option<String>,
20 pub no_cache: bool,
23 pub timeout: Duration,
26}
27
28impl BuildOptions {
29 pub(crate) fn validate(&self) -> Result<()> {
30 validate_relative(&self.dockerfile)?;
31 if self.timeout.is_zero() || self.timeout > Duration::from_millis(u32::MAX as u64 - 1) {
32 return Err(Error::InvalidInput(
33 "use a positive timeout of at most 4294967294 milliseconds".into(),
34 ));
35 }
36 for (key, value) in &self.build_arguments {
37 if key.trim().is_empty() || key.contains(['=', '\0']) || value.contains('\0') {
38 return Err(Error::InvalidInput("invalid build argument".into()));
39 }
40 }
41 for value in [&self.target, &self.platform].into_iter().flatten() {
42 if value.trim().is_empty() || value.contains('\0') {
43 return Err(Error::InvalidInput(
44 "invalid target stage or platform".into(),
45 ));
46 }
47 }
48 Ok(())
49 }
50}
51
52impl Default for BuildOptions {
53 fn default() -> Self {
54 Self {
55 dockerfile: "Dockerfile".into(),
56 build_arguments: BTreeMap::new(),
57 target: None,
58 platform: None,
59 no_cache: false,
60 timeout: Duration::from_secs(600),
61 }
62 }
63}