use anyhow::Context;
use camino::{Utf8Path, Utf8PathBuf};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct TargetDirectory(pub Utf8PathBuf);
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct TargetPackage(pub Utf8PathBuf);
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct MappedPath {
pub from: Utf8PathBuf,
pub to: Utf8PathBuf,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum BuildInput {
AddInMemoryFile {
dst_path: Utf8PathBuf,
contents: String,
},
AddDirectory(TargetDirectory),
AddFile {
mapped_path: MappedPath,
len: u64,
},
AddBlob {
path: MappedPath,
blob: crate::blob::Source,
},
AddPackage(TargetPackage),
}
impl BuildInput {
pub fn input_path(&self) -> Option<&Utf8Path> {
match self {
BuildInput::AddInMemoryFile { .. } => None,
BuildInput::AddDirectory(_target) => None,
BuildInput::AddFile { mapped_path, .. } => Some(&mapped_path.from),
BuildInput::AddBlob { path, .. } => Some(&path.from),
BuildInput::AddPackage(target_package) => Some(&target_package.0),
}
}
pub fn add_file(mapped_path: MappedPath) -> anyhow::Result<Self> {
let src = &mapped_path.from;
let len = src
.metadata()
.with_context(|| format!("Failed to get length of {src}"))?
.len();
Ok(Self::AddFile { mapped_path, len })
}
}
pub struct BuildInputs(pub Vec<BuildInput>);
impl BuildInputs {
pub fn new() -> Self {
Self(vec![])
}
}
impl Default for BuildInputs {
fn default() -> Self {
Self::new()
}
}