codec_multicodec/
lookup.rs1use crate::table::{CodecTag, KeyMaterialKind, MULTICODEC_TABLE};
6
7#[derive(Debug, Clone)]
9pub struct CodecLookupResult {
10 pub name: &'static str,
12 pub tag: CodecTag,
14 pub key_material: KeyMaterialKind,
16 pub alg: &'static str,
18 pub codec: &'static [u8],
20 pub key_length: usize,
22}
23
24pub fn lookup_codec_prefix(bytes: &[u8]) -> Option<CodecLookupResult> {
26 for (name, spec) in MULTICODEC_TABLE {
27 let prefix = spec.codec;
28 if bytes.len() >= prefix.len() && bytes.starts_with(prefix) {
29 return Some(CodecLookupResult {
30 name,
31 tag: spec.tag,
32 key_material: spec.key_material,
33 alg: spec.alg,
34 codec: spec.codec,
35 key_length: spec.key_length,
36 });
37 }
38 }
39 None
40}
41
42pub fn strip_codec_prefix(bytes: &[u8]) -> &[u8] {
46 if let Some(found) = lookup_codec_prefix(bytes) {
47 &bytes[found.codec.len()..]
48 } else {
49 bytes
50 }
51}