use crate::{
prelude::{FromBytes, Network, ProgramID, ToBytes},
synthesizer::Program,
};
use anyhow::{anyhow, ensure, Result};
use std::{
fs::{self, File},
io::Write,
path::Path,
};
static AVM_FILE_EXTENSION: &str = "avm";
pub struct AVMFile<N: Network> {
file_name: String,
program: Program<N>,
}
impl<N: Network> AVMFile<N> {
pub fn create(directory: &Path, program: Program<N>, is_main: bool) -> Result<Self> {
ensure!(directory.exists(), "The program directory does not exist: '{}'", directory.display());
ensure!(
!Program::is_reserved_keyword(program.id().name()),
"Program name is invalid (reserved): {}",
program.id()
);
let file_name = if is_main { Self::main_file_name() } else { format!("{}.{AVM_FILE_EXTENSION}", program.id()) };
let path = directory.join(file_name);
File::create(&path)?.write_all(&program.to_bytes_le()?)?;
Self::from_filepath(&path)
}
pub fn open(directory: &Path, program_id: &ProgramID<N>, is_main: bool) -> Result<Self> {
ensure!(directory.exists(), "The build directory does not exist: '{}'", directory.display());
let file_name = if is_main { Self::main_file_name() } else { format!("{program_id}.{AVM_FILE_EXTENSION}") };
let path = directory.join(file_name);
ensure!(path.exists(), "The AVM file is missing: '{}'", path.display());
let avm_file = Self::from_filepath(&path)?;
Ok(avm_file)
}
pub fn exists_at(&self, file_path: &Path) -> bool {
Self::check_path(file_path).is_ok() && file_path.exists()
}
pub fn main_exists_at(directory: &Path) -> bool {
let path = directory.join(Self::main_file_name());
path.is_file() && path.exists()
}
pub fn main_file_name() -> String {
format!("main.{AVM_FILE_EXTENSION}")
}
pub fn file_name(&self) -> &str {
&self.file_name
}
pub const fn program(&self) -> &Program<N> {
&self.program
}
pub fn remove(&self, path: &Path) -> Result<()> {
if !path.exists() {
Ok(())
} else {
Self::check_path(path)?;
if path.exists() {
fs::remove_file(path)?;
}
Ok(())
}
}
}
impl<N: Network> AVMFile<N> {
fn check_path(path: &Path) -> Result<()> {
ensure!(path.is_file(), "The path is not a file.");
let extension = path.extension().ok_or_else(|| anyhow!("File extension not found."))?;
ensure!(extension == AVM_FILE_EXTENSION, "File extension is incorrect.");
Ok(())
}
fn from_filepath(file: &Path) -> Result<Self> {
Self::check_path(file)?;
ensure!(file.exists(), "File does not exist: {}", file.display());
let file_name = file
.file_stem()
.ok_or_else(|| anyhow!("File name not found."))?
.to_str()
.ok_or_else(|| anyhow!("File name not found."))?
.to_string();
let program_bytes = fs::read(file)?;
let program = Program::from_bytes_le(&program_bytes)?;
Ok(Self { file_name, program })
}
pub fn write_to(&self, path: &Path) -> Result<()> {
Self::check_path(path)?;
let file_name = path
.file_stem()
.ok_or_else(|| anyhow!("File name not found."))?
.to_str()
.ok_or_else(|| anyhow!("File name not found."))?
.to_string();
ensure!(file_name == self.file_name, "File name does not match.");
Ok(File::create(path)?.write_all(&self.program.to_bytes_le()?)?)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::prelude::Parser;
type CurrentNetwork = snarkvm_console::network::Testnet3;
fn temp_dir() -> std::path::PathBuf {
tempfile::tempdir().expect("Failed to open temporary directory").into_path()
}
#[test]
fn test_from_path() {
let directory = temp_dir();
let program_string = r"
program token.aleo;
record token:
owner as address.private;
token_amount as u64.private;
function compute:
input r0 as token.record;
add r0.token_amount r0.token_amount into r1;
output r1 as u64.private;";
let (string, program) = Program::<CurrentNetwork>::parse(program_string).unwrap();
assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
let path = directory.join("token.avm");
let mut file = File::create(&path).unwrap();
file.write_all(&program.to_bytes_le().unwrap()).unwrap();
let file = AVMFile::from_filepath(&path).unwrap();
assert_eq!("token", file.file_name());
assert_eq!(&program, file.program());
}
}