Skip to main content

kute_types/
lib.rs

1//! Shared data model for the Kannaka Universal Tokenization Engine.
2//!
3//! This crate has no tokenization logic. It defines the values every other
4//! crate exchanges: identifiers, canonical inputs, token sequences, training
5//! configuration and the in-memory vocabulary model that KVF serializes.
6
7pub mod config;
8pub mod error;
9pub mod event;
10pub mod id;
11pub mod input;
12pub mod vocab;
13
14pub use config::{AdapterConfig, Pretokenizer, TokenizerCapabilities, TrainingConfig};
15pub use error::{KuteError, Result};
16pub use event::{EventRecord, EventSchema, EventStream, TimeUnit};
17pub use id::VocabularyId;
18pub use input::{CanonicalInput, Modality, Payload, PayloadKind, TokenSequence, UnknownPolicy, MAX_ESCAPES};
19pub use semver::Version;
20pub use vocab::{
21    AdapterSpec, Integrity, Manifest, MergeRule, Provenance, RepresentationRef, Signature,
22    TestVector, TokenEntry, TokenKind, TokenPayload, TokenRecord, Vocabulary, KVF_VERSION,
23};
24
25/// Version of the KUTE software that produced an artifact.
26pub const KUTE_VERSION: &str = env!("CARGO_PKG_VERSION");
27
28/// Hex SHA-256 with the `sha256:` prefix used throughout KUTE.
29pub fn sha256_tagged(bytes: &[u8]) -> String {
30    use sha2::{Digest, Sha256};
31    format!("sha256:{}", hex(&Sha256::digest(bytes)))
32}
33
34/// Lowercase hex encoding.
35pub fn hex(bytes: &[u8]) -> String {
36    const DIGITS: &[u8; 16] = b"0123456789abcdef";
37    let mut out = String::with_capacity(bytes.len() * 2);
38    for b in bytes {
39        out.push(DIGITS[(b >> 4) as usize] as char);
40        out.push(DIGITS[(b & 0x0f) as usize] as char);
41    }
42    out
43}
44
45/// Serde helper: `Vec<u8>` as standard base64 text.
46pub mod b64 {
47    use base64::{engine::general_purpose::STANDARD, Engine};
48    use serde::{Deserialize, Deserializer, Serializer};
49
50    pub fn serialize<S: Serializer>(v: &[u8], s: S) -> Result<S::Ok, S::Error> {
51        s.serialize_str(&STANDARD.encode(v))
52    }
53
54    pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<u8>, D::Error> {
55        let s = String::deserialize(d)?;
56        STANDARD
57            .decode(s.as_bytes())
58            .map_err(serde::de::Error::custom)
59    }
60
61    pub fn encode(v: &[u8]) -> String {
62        STANDARD.encode(v)
63    }
64}