use std::{
fs::File,
io::{Read, Write},
path::Path,
str::FromStr,
};
use crate::{VmfError, VmfResult, parser};
use super::VmfFile;
impl VmfFile {
pub fn parse(content: &str) -> VmfResult<Self> {
parser::parse_vmf(content)
}
pub fn parse_file(file: &mut impl Read) -> VmfResult<Self> {
let mut content = Vec::new();
file.read_to_end(&mut content)?;
let content = String::from_utf8_lossy(&content);
VmfFile::parse(&content)
}
pub fn open(path: impl AsRef<Path>) -> VmfResult<Self> {
let path_str = path.as_ref().to_string_lossy().to_string();
let mut file = File::open(path)?;
let mut content = Vec::new();
file.read_to_end(&mut content)?;
let content = String::from_utf8_lossy(&content);
let mut vmf_file = VmfFile::parse(&content)?;
vmf_file.path = Some(path_str);
Ok(vmf_file)
}
pub fn save(&self, path: impl AsRef<Path>) -> VmfResult<()> {
let mut file = File::create(path)?;
file.write_all(self.to_vmf_string().as_bytes())?;
Ok(())
}
}
impl FromStr for VmfFile {
type Err = VmfError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
VmfFile::parse(s)
}
}