use aion_awl::doc::{ContractDoc, DocumentDoc, SourceState};
use aion_package::{ExtractionLimits, Package};
use aion_store::PackageRecord;
use super::types::DeployedError;
pub(super) fn read_doc(
archives: &[PackageRecord],
workflow_type: &str,
content_hash: &str,
loaded_types: &[String],
) -> Result<DocumentDoc, DeployedError> {
let not_found = || DeployedError::NotFound {
workflow_type: workflow_type.to_owned(),
content_hash: content_hash.to_owned(),
};
let Some(record) = archives
.iter()
.find(|record| record.content_hash == content_hash)
else {
if loaded_types.iter().any(|held| held == workflow_type) {
return Ok(aion_awl::doc::without_source(
workflow_type.to_owned(),
aion_awl::doc::FAMILY_UNKNOWN.to_owned(),
Some(content_hash.to_owned()),
SourceState::NotPersisted,
ContractDoc::committed(
None,
None,
None,
Vec::new(),
Vec::new(),
Vec::new(),
Vec::new(),
),
));
}
return Err(not_found());
};
let package = match Package::load_from_bytes(&record.archive, ExtractionLimits::unbounded()) {
Ok(package) => package,
Err(error) => {
return Ok(aion_awl::doc::without_source(
workflow_type.to_owned(),
aion_awl::doc::FAMILY_UNKNOWN.to_owned(),
Some(content_hash.to_owned()),
SourceState::Unreadable {
reason: error.to_string(),
},
ContractDoc::committed(
None,
None,
None,
Vec::new(),
Vec::new(),
Vec::new(),
Vec::new(),
),
));
}
};
if !super::document::declares_workflow_type(&package, workflow_type) {
return Err(not_found());
}
aion_awl::doc::from_package(&package, workflow_type).map_err(|error| DeployedError::DocModel {
workflow_type: workflow_type.to_owned(),
content_hash: content_hash.to_owned(),
reason: error.to_string(),
})
}