use alloc::string::String;
#[cfg(feature = "serde_json")]
use alloc::vec::Vec;
use crate::compiled::plan::CompiledPlan;
use crate::err::{Error, Result};
pub const ARTIFACT_FORMAT_VERSION: u32 = 1;
pub const ARTIFACT_MAGIC: [u8; 8] = *b"INCIN\x00\x01\x00";
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct ArtifactVersion {
pub major: u32,
pub minor: u32,
pub patch: u32,
pub format: u32,
}
impl ArtifactVersion {
#[must_use]
pub const fn new(major: u32, minor: u32, patch: u32) -> Self {
Self {
major,
minor,
patch,
format: ARTIFACT_FORMAT_VERSION,
}
}
#[must_use]
pub fn is_compatible_with(&self, other: &Self) -> bool {
self.format == other.format && self.major == other.major && self.minor == other.minor
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ArtifactHeader {
pub version: ArtifactVersion,
pub checksum: u32,
pub label: String,
}
#[cfg(feature = "serde_json")]
fn adler32(data: &[u8]) -> u32 {
const MOD: u32 = 65521;
let mut a: u32 = 1;
let mut b: u32 = 0;
for &byte in data {
a = (a + u32::from(byte)) % MOD;
b = (b + a) % MOD;
}
(b << 16) | a
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct CompiledArtifact {
pub header: ArtifactHeader,
pub plan: CompiledPlan,
}
impl CompiledArtifact {
#[cfg(feature = "serde_json")]
pub fn new(plan: CompiledPlan, version: ArtifactVersion, label: String) -> Result<Self> {
let plan_bytes = Self::serialize_plan(&plan)?;
let checksum = adler32(&plan_bytes);
Ok(Self {
header: ArtifactHeader {
version,
checksum,
label,
},
plan,
})
}
#[cfg(feature = "serde_json")]
fn serialize_plan(plan: &CompiledPlan) -> Result<Vec<u8>> {
serde_json::to_vec(plan).map_err(|e| Error::Msg(alloc::format!("serialize: {e}")))
}
#[cfg(feature = "serde_json")]
pub fn serialize(&self) -> Result<Vec<u8>> {
let json_bytes =
serde_json::to_vec(self).map_err(|e| Error::Msg(alloc::format!("serialize: {e}")))?;
let mut framed = Vec::with_capacity(ARTIFACT_MAGIC.len() + json_bytes.len());
framed.extend_from_slice(&ARTIFACT_MAGIC);
framed.extend_from_slice(&json_bytes);
Ok(framed)
}
#[cfg(feature = "serde_json")]
pub fn deserialize(bytes: &[u8]) -> Result<Self> {
let payload = if bytes.starts_with(&ARTIFACT_MAGIC) {
&bytes[ARTIFACT_MAGIC.len()..]
} else {
bytes
};
serde_json::from_slice(payload).map_err(|e| Error::Msg(alloc::format!("deserialize: {e}")))
}
#[cfg(feature = "serde_json")]
pub fn verify_integrity(&self) -> Result<()> {
let plan_bytes = Self::serialize_plan(&self.plan)?;
let computed = adler32(&plan_bytes);
if computed != self.header.checksum {
return Err(Error::Msg(alloc::format!(
"artifact integrity check failed: stored checksum {:#010x}, computed {:#010x}",
self.header.checksum,
computed
)));
}
Ok(())
}
pub fn check_compatibility(&self, required: &ArtifactVersion) -> Result<()> {
if !self.header.version.is_compatible_with(required) {
return Err(Error::Msg(alloc::format!(
"artifact version {:?} is incompatible with required {:?}",
self.header.version,
required
)));
}
Ok(())
}
pub fn verify_semantics(&self) -> Result<()> {
let nodes = &self.plan.graph.nodes;
if nodes.len() > 100_000 {
return Err(Error::Msg(alloc::format!(
"Artifact node count {} exceeds maximum allowed limit 100,000",
nodes.len()
)));
}
for (i, node) in nodes.iter().enumerate() {
if node.id != i {
return Err(Error::Msg(alloc::format!(
"Artifact semantic failure: node at position {} has non-topological ID {}",
i,
node.id
)));
}
}
Ok(())
}
#[cfg(feature = "serde_json")]
pub fn load(bytes: &[u8], required_version: &ArtifactVersion) -> Result<Self> {
Self::load_with_limits(
bytes,
required_version,
&crate::resource::ResourceLimits::compile_time_defaults(),
)
}
#[cfg(feature = "serde_json")]
pub fn load_with_limits(
bytes: &[u8],
required_version: &ArtifactVersion,
limits: &crate::resource::ResourceLimits,
) -> Result<Self> {
if (bytes.len() as u64) > limits.max_file_bytes {
return Err(Error::Msg(alloc::format!(
"Artifact file size {} bytes exceeds maximum limit {}",
bytes.len(),
limits.max_file_bytes
)));
}
let artifact = Self::deserialize(bytes)?;
artifact.verify_integrity()?;
artifact.check_compatibility(required_version)?;
artifact.verify_semantics()?;
Ok(artifact)
}
}