use std::fs::File;
use std::io;
use std::path::PathBuf;
use super::{Result, ScriptError};
use crate::parser::DgParser;
use crate::{InteractionMap, ParseResult};
#[derive(Clone, Debug)]
pub struct ScriptPath(pub PathBuf);
impl ScriptPath {
pub fn make_append(&self, path: PathBuf) -> Self {
Self(self.0.parent().unwrap().join(path))
}
pub fn read(&self) -> Result<String> {
File::open(&self.0)
.and_then(|file| io::read_to_string(file))
.map_err(|_| ScriptError::FileOpen(self.0.clone()))
}
pub fn parse_import(&self) -> ParseResult<InteractionMap> {
let contents = self.read()?;
let mut parser = DgParser::new(self.0.clone());
parser.parse_all(&contents)
}
}