use crate::outputs::OUTPUTS_DIRECTORY_NAME;
use leo_errors::{PackageError, Result};
use serde::Deserialize;
use std::{borrow::Cow, fmt, fs, path::Path};
#[derive(Deserialize)]
pub enum Snapshot {
Initial,
ImportsResolved,
TypeInference,
Canonicalization,
}
impl fmt::Display for Snapshot {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
match self {
Self::Initial => "initial_ast",
Self::ImportsResolved => "imports_resolved_ast",
Self::TypeInference => "type_inferenced_ast",
Self::Canonicalization => "canonicalization_ast",
}
)
}
}
pub static AST_SNAPSHOT_FILE_EXTENSION: &str = ".json";
#[derive(Deserialize)]
pub struct SnapshotFile {
pub package_name: String,
pub snapshot: Snapshot,
}
impl SnapshotFile {
pub fn new(package_name: &str, snapshot: Snapshot) -> Self {
Self {
package_name: package_name.to_string(),
snapshot,
}
}
pub fn exists_at(&self, path: &Path) -> bool {
let path = self.snapshot_file_path(path);
path.exists()
}
pub fn read_from(&self, path: &Path) -> Result<String> {
let path = self.snapshot_file_path(path);
let result =
fs::read_to_string(&path).map_err(|_| PackageError::failed_to_read_snapshot_file(path.into_owned()))?;
Ok(result)
}
pub fn remove(&self, path: &Path) -> Result<bool> {
let path = self.snapshot_file_path(path);
if !path.exists() {
return Ok(false);
}
fs::remove_file(&path).map_err(|_| PackageError::failed_to_remove_snapshot_file(path.into_owned()))?;
Ok(true)
}
fn snapshot_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> {
let mut path = Cow::from(path);
if path.is_dir() {
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
path.to_mut().push(OUTPUTS_DIRECTORY_NAME);
}
path.to_mut()
.push(format!("{}{AST_SNAPSHOT_FILE_EXTENSION}", self.snapshot));
}
path
}
}