use super::{Artifact, ArtifactCreator, ArtifactSaver};
use crate::Error;
use std::fs;
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TlaConfigFile {
path: PathBuf,
content: String,
}
impl TlaConfigFile {
pub(crate) fn new<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
let path = path.as_ref().to_path_buf();
crate::util::check_file_existence(&path)?;
let content: String = fs::read_to_string(&path)?;
Ok(Self { path, content })
}
pub fn filename(&self) -> String {
let res = self.path.as_path().file_name();
if res.is_none() {
panic!("TODO: tla config file should not fail to provide filename");
}
return res.unwrap().to_str().unwrap().to_owned();
}
pub const fn path(&self) -> &PathBuf {
&self.path
}
pub fn content(&self) -> &str {
&self.content
}
pub fn set_path(&mut self, path: &Path) {
self.path = path.into();
}
}
impl std::fmt::Display for TlaConfigFile {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", &self.content)
}
}
impl ArtifactCreator for TlaConfigFile {
fn from_string(s: &str) -> Result<Self, Error> {
Ok(Self {
path: PathBuf::new(),
content: s.to_string(),
})
}
}
impl Artifact for TlaConfigFile {
fn as_string(&self) -> String {
self.content.clone()
}
}
impl ArtifactSaver for TlaConfigFile {
fn filename(&self) -> String {
self.filename()
}
}