use serde::{Deserialize, Serialize};
use xet_core_structures::merklehash::{DataHashError, MerkleHash};
use xet_runtime::error_printer::ErrorPrinter;
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "python", pyo3::pyclass(get_all, from_py_object))]
pub struct XetFileInfo {
pub hash: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub file_size: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub sha256: Option<String>,
}
#[cfg_attr(feature = "python", pyo3::pymethods)]
impl XetFileInfo {
#[cfg(feature = "python")]
#[new]
#[pyo3(signature = (hash, file_size=None))]
fn py_new(hash: String, file_size: Option<u64>) -> Self {
Self {
hash,
file_size,
sha256: None,
}
}
}
impl XetFileInfo {
pub fn new(hash: String, file_size: u64) -> Self {
Self {
hash,
file_size: Some(file_size),
sha256: None,
}
}
pub fn new_with_sha256(hash: String, file_size: u64, sha256: String) -> Self {
Self {
hash,
file_size: Some(file_size),
sha256: Some(sha256),
}
}
pub fn new_hash_only(hash: String) -> Self {
Self {
hash,
file_size: None,
sha256: None,
}
}
pub fn hash(&self) -> &str {
&self.hash
}
pub fn merkle_hash(&self) -> std::result::Result<MerkleHash, DataHashError> {
MerkleHash::from_hex(&self.hash).log_error("Error parsing hash value for file info")
}
pub fn file_size(&self) -> Option<u64> {
self.file_size
}
pub fn sha256(&self) -> Option<&str> {
self.sha256.as_deref()
}
pub fn as_pointer_file(&self) -> std::result::Result<String, serde_json::Error> {
serde_json::to_string(self)
}
}