use log::debug;
use crate::{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) -> Result<Self, pest::error::Error<parser::Rule>> {
let working_dir = collection_path.parent().unwrap().to_path_buf();
let working_dir = if working_dir.to_str().unwrap() == "" {
std::env::current_dir().unwrap()
} else {
working_dir
};
debug!("Working dir: {:?}", working_dir);
let input = std::fs::read_to_string(collection_path).map_err(|e| {
pest::error::Error::new_from_span(
pest::error::ErrorVariant::CustomError {
message: e.to_string(),
},
pest::Span::new("", 0, 0).unwrap(),
)
})?;
parser::parse_collection(input.as_str(), working_dir)
}
}