use std::{env, path::PathBuf};
use anyhow::Context;
use cargo::CargoResult;
use cargo::util::context::GlobalContext;
use crate::{Cloner, ClonerSource};
#[derive(Debug, Default)]
pub struct ClonerBuilder {
context: Option<GlobalContext>,
directory: Option<PathBuf>,
source: ClonerSource,
use_git: bool,
}
impl ClonerBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn with_context(self, context: GlobalContext) -> Self {
Self {
context: Some(context),
..self
}
}
pub fn with_directory(self, directory: impl Into<PathBuf>) -> Self {
Self {
directory: Some(directory.into()),
..self
}
}
pub fn with_source(self, source: ClonerSource) -> Self {
Self { source, ..self }
}
pub fn with_git(self, use_git: bool) -> Self {
Self { use_git, ..self }
}
pub fn build(self) -> CargoResult<Cloner> {
let context = match self.context {
Some(context) => context,
None => GlobalContext::default().context("Unable to get cargo context.")?,
};
let directory = match self.directory {
Some(directory) => directory,
None => env::current_dir().context("Unable to get current directory.")?,
};
let srcid = self
.source
.cargo_source
.to_source_id(&context)
.context("can't determine the source id")?;
Ok(Cloner {
context,
directory,
srcid,
use_git: self.use_git,
})
}
}