token_count/tokenizers/
mod.rs1pub mod claude;
43pub mod openai;
44pub mod registry;
45
46use std::fmt;
47
48#[derive(Debug, Clone, Copy, PartialEq, Eq)]
50pub enum TokenCount {
51 Estimated(usize),
53
54 Exact(usize),
56}
57
58impl TokenCount {
59 pub fn value(&self) -> usize {
61 match self {
62 Self::Estimated(n) | Self::Exact(n) => *n,
63 }
64 }
65
66 pub fn is_estimated(&self) -> bool {
68 matches!(self, Self::Estimated(_))
69 }
70
71 pub fn is_exact(&self) -> bool {
73 matches!(self, Self::Exact(_))
74 }
75}
76
77impl fmt::Display for TokenCount {
78 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79 match self {
80 Self::Estimated(n) => write!(f, "~{}", n),
81 Self::Exact(n) => write!(f, "{}", n),
82 }
83 }
84}
85
86pub trait Tokenizer: Send + Sync {
88 fn count_tokens(&self, text: &str) -> anyhow::Result<usize>;
90
91 fn get_model_info(&self) -> ModelInfo;
93}
94
95#[derive(Debug, Clone)]
97pub struct ModelInfo {
98 pub name: String,
99 pub encoding: String,
100 pub context_window: usize,
101 pub description: String,
102}
103
104impl fmt::Display for ModelInfo {
105 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106 write!(f, "{} ({})", self.name, self.encoding)
107 }
108}
109
110#[derive(Debug, Clone)]
112pub struct TokenizationResult {
113 pub token_count: usize,
114 pub model_info: ModelInfo,
115}
116
117#[cfg(test)]
118mod tests {
119 use super::*;
120
121 #[test]
122 fn test_token_count_display_estimated() {
123 let count = TokenCount::Estimated(42);
124 assert_eq!(format!("{}", count), "~42");
125 }
126
127 #[test]
128 fn test_token_count_display_exact() {
129 let count = TokenCount::Exact(42);
130 assert_eq!(format!("{}", count), "42");
131 }
132
133 #[test]
134 fn test_token_count_value() {
135 assert_eq!(TokenCount::Estimated(42).value(), 42);
136 assert_eq!(TokenCount::Exact(42).value(), 42);
137 }
138
139 #[test]
140 fn test_token_count_is_estimated() {
141 assert!(TokenCount::Estimated(42).is_estimated());
142 assert!(!TokenCount::Exact(42).is_estimated());
143 }
144
145 #[test]
146 fn test_token_count_is_exact() {
147 assert!(!TokenCount::Estimated(42).is_exact());
148 assert!(TokenCount::Exact(42).is_exact());
149 }
150
151 #[test]
152 fn test_token_count_equality() {
153 assert_eq!(TokenCount::Estimated(42), TokenCount::Estimated(42));
154 assert_eq!(TokenCount::Exact(42), TokenCount::Exact(42));
155 assert_ne!(TokenCount::Estimated(42), TokenCount::Exact(42));
156 }
157}