use log::debug;
use crate::{
error::{HenError, HenErrorKind, HenResult},
parser,
request::Request,
};
use std::path::PathBuf;
#[derive(Debug)]
pub struct Collection {
pub name: String,
pub description: String,
pub requests: Vec<Request>,
}
impl Collection {
pub fn new(collection_path: PathBuf) -> HenResult<Self> {
let working_dir = collection_path
.parent()
.map(|path| path.to_path_buf())
.unwrap_or_else(|| PathBuf::from(""));
let working_dir = if working_dir.as_os_str().is_empty() {
std::env::current_dir().map_err(|err| {
HenError::new(HenErrorKind::Io, "Failed to determine current directory")
.with_detail(err.to_string())
})?
} else {
working_dir
};
debug!("Working dir: {:?}", working_dir);
let input = std::fs::read_to_string(&collection_path).map_err(|err| {
HenError::new(
HenErrorKind::Io,
format!("Failed to read collection {}", collection_path.display()),
)
.with_detail(err.to_string())
})?;
parser::parse_collection(input.as_str(), working_dir)
.map_err(HenError::from)
.map_err(|err| {
err.with_detail(format!(
"While parsing collection file {}",
collection_path.display()
))
})
}
}