use std::path::PathBuf;
fn _default_context() -> Option<String> {
Some(".".to_string())
}
fn _default_workdir() -> String {
crate::DEFAULT_WORKDIR.to_string()
}
#[derive(
Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize,
)]
#[serde(default)]
pub struct Scope {
pub(crate) context: Option<String>,
#[serde(default = "_default_workdir")]
pub(crate) workdir: String,
}
impl Scope {
pub fn new(workdir: impl ToString) -> Self {
Self {
context: None,
workdir: workdir.to_string(),
}
}
get!(context: Option<String>, workdir: String);
setwith!(workdir: String);
pub fn as_path(&self) -> PathBuf {
let mut path = PathBuf::new();
self.context().clone().map(|context| path.push(context));
path.push(self.workdir());
debug_assert!(path.is_dir());
path
}
pub fn as_path_str(&self) -> String {
self.as_path().display().to_string()
}
pub fn set_cwd(&self) {
std::env::set_current_dir(self.as_path()).unwrap();
}
pub fn set_context(&mut self, context: impl ToString) {
self.context = Some(context.to_string());
}
pub fn with_context(self, context: impl ToString) -> Self {
Self {
context: Some(context.to_string()),
..self
}
}
pub fn set_some_context(&mut self, rhs: Option<impl ToString>) {
rhs.map(|data| self.set_context(data));
}
pub fn set_some_workdir(&mut self, rhs: Option<impl ToString>) {
rhs.map(|data| self.set_workdir(data));
}
}
impl Default for Scope {
fn default() -> Self {
Self {
context: None,
workdir: "dist".into(),
}
}
}
impl core::fmt::Display for Scope {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{path}", path = self.as_path().display())
}
}
impl core::str::FromStr for Scope {
type Err = std::io::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::new(s))
}
}