use super::text::{NormalizationError, normalize};
use ic_host_artifacts::artifact::{ArtifactError, ArtifactIdentity};
use ic_host_fs::read::hash_file;
use ic_host_process::tool::{
AdmittedTool, CompletedExecution, ExecutionContext, OutputLimits, ToolError,
};
use std::{fmt, path::Path};
#[cfg(test)]
mod tests;
#[derive(Debug)]
pub enum ExtractionError {
SourcePath,
Input(ArtifactError),
Tool(ToolError),
SourceInspection {
source: ArtifactError,
evidence: Box<CompletedExecution>,
},
SourceChanged {
before: ArtifactIdentity,
after: ArtifactIdentity,
evidence: Box<CompletedExecution>,
},
Normalize {
source: NormalizationError,
evidence: Box<CompletedExecution>,
},
}
impl fmt::Display for ExtractionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::SourcePath => f.write_str("Candid source path must be absolute"),
Self::Input(source) => write!(f, "Candid source inspection failed: {source}"),
Self::Tool(source) => source.fmt(f),
Self::SourceInspection { .. } => f.write_str("Candid source re-inspection failed"),
Self::SourceChanged { .. } => f.write_str("Candid source changed during extraction"),
Self::Normalize { source, .. } => source.fmt(f),
}
}
}
impl std::error::Error for ExtractionError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Input(source) | Self::SourceInspection { source, .. } => Some(source),
Self::Tool(source) => Some(source),
Self::Normalize { source, .. } => Some(source),
_ => None,
}
}
}
pub struct ExtractedCandid {
pub text: String,
pub source_identity: ArtifactIdentity,
pub tool_identity: ArtifactIdentity,
pub evidence: CompletedExecution,
}
impl fmt::Debug for ExtractedCandid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ExtractedCandid")
.field("text_bytes", &self.text.len())
.field("source_identity", &self.source_identity)
.field("tool_identity", &self.tool_identity)
.field("evidence", &self.evidence)
.finish()
}
}
pub fn extract(
tool: &AdmittedTool,
source: &Path,
context: &ExecutionContext<'_>,
source_bytes: u64,
output: OutputLimits,
) -> Result<ExtractedCandid, ExtractionError> {
if !source.is_absolute() {
return Err(ExtractionError::SourcePath);
}
let before = hash_file(source, source_bytes).map_err(ExtractionError::Input)?;
let evidence = tool
.run(&[source.as_os_str().to_owned()], context, output)
.and_then(CompletedExecution::require_success)
.and_then(CompletedExecution::require_complete)
.map_err(ExtractionError::Tool)?;
let after = match hash_file(source, source_bytes) {
Ok(identity) => identity,
Err(source) => {
return Err(ExtractionError::SourceInspection {
source,
evidence: Box::new(evidence),
});
}
};
if before != after {
return Err(ExtractionError::SourceChanged {
before,
after,
evidence: Box::new(evidence),
});
}
let text = match normalize(&evidence.stdout, output.stdout.retained_bytes()) {
Ok(text) => text,
Err(source) => {
return Err(ExtractionError::Normalize {
source,
evidence: Box::new(evidence),
});
}
};
Ok(ExtractedCandid {
text,
source_identity: before,
tool_identity: tool.identity(),
evidence,
})
}