use crate::table::{CodecTag, KeyMaterialKind, MULTICODEC_TABLE};
#[derive(Debug, Clone)]
pub struct CodecLookupResult {
pub name: &'static str,
pub tag: CodecTag,
pub key_material: KeyMaterialKind,
pub alg: &'static str,
pub codec: &'static [u8],
pub key_length: usize,
}
pub fn lookup_codec_prefix(bytes: &[u8]) -> Option<CodecLookupResult> {
for (name, spec) in MULTICODEC_TABLE {
let prefix = spec.codec;
if bytes.len() >= prefix.len() && bytes.starts_with(prefix) {
return Some(CodecLookupResult {
name,
tag: spec.tag,
key_material: spec.key_material,
alg: spec.alg,
codec: spec.codec,
key_length: spec.key_length,
});
}
}
None
}
pub fn strip_codec_prefix(bytes: &[u8]) -> &[u8] {
if let Some(found) = lookup_codec_prefix(bytes) {
&bytes[found.codec.len()..]
} else {
bytes
}
}