use std::fs::read;
use std::path::{Path, PathBuf};
use async_trait::async_trait;
use novax_data::Address;
use crate::code::bytes::AsBytesValue;
use crate::errors::{CodeError, NovaXError};
pub struct FileCode(PathBuf);
#[async_trait]
impl AsBytesValue for &FileCode {
async fn into_bytes_value(self) -> Result<Vec<u8>, NovaXError> {
read(&self.0)
.map_err(|_| CodeError::UnableToReadCodeFromFile.into())
}
}
#[async_trait]
impl AsBytesValue for &str {
async fn into_bytes_value(self) -> Result<Vec<u8>, NovaXError> {
if self.starts_with("erd") {
if let Ok(address) = Address::from_bech32_string(self) {
return Ok(address.to_bytes().to_vec());
}
}
FileCode(Path::new(self).to_path_buf()).into_bytes_value().await
}
}