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