use super::adapter::Parser;
use crate::utils::{dir_to_string, file_to_string};
use anyhow::Result;
use rush_ecs_core::blueprint::Blueprint;
use std::{
fs::{canonicalize, metadata},
path::Path,
};
pub struct Loader {
parser: Box<dyn Parser>,
}
impl Loader {
pub fn new(parser: impl Parser) -> Self {
Self {
parser: Box::new(parser),
}
}
pub fn load_blueprint(&self, path: &Path) -> Result<Blueprint> {
let abs_path = canonicalize(path)?;
let md = metadata(abs_path).expect("invalid path");
let blueprint_string = match md.is_dir() {
true => dir_to_string(path),
false => file_to_string(path),
};
self.parser.parse_string(blueprint_string)
}
}