use std::{
env::current_dir,
path::{Path, PathBuf},
};
use relative_path::RelativePath;
use crate::{Compose, ComposeBuilderError};
#[derive(Default)]
pub struct ComposeBuilder {
path: Option<String>,
}
impl ComposeBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn path(mut self, path: impl ToString) -> Self {
self.path = Some(path.to_string());
self
}
pub fn build(self) -> Result<Compose, ComposeBuilderError> {
let path = match self.path {
Some(path) => path,
None => return Err(ComposeBuilderError::MissingField("path".to_string())),
};
let path = match Path::new(&path).is_absolute() {
true => PathBuf::from(path),
false => {
let base = current_dir()?;
RelativePath::new(&path).to_logical_path(base)
}
};
if path.exists() {
Ok(Compose {
path: path.to_string_lossy().to_string(),
})
} else {
Err(ComposeBuilderError::FileNotFound(
path.to_string_lossy().to_string(),
))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_compose_builder() {
let compose = Compose::builder().path("non-existent.yml").build();
matches!(compose, Err(ComposeBuilderError::FileNotFound(_)));
}
}