use crate::core::{LuciError, Result};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum QuantizationType {
None,
Int8,
Int4,
Bbq,
}
impl QuantizationType {
pub const DEFAULT: Self = Self::Int8;
pub fn from_es_name(name: &str) -> Result<Self> {
match name {
"none" => Ok(Self::None),
"int8" => Ok(Self::Int8),
"int4" => Err(LuciError::InvalidQuery(
"quantization \"int4\" is recognized but not yet implemented; \
supported values: \"none\", \"int8\""
.into(),
)),
"bbq" => Err(LuciError::InvalidQuery(
"quantization \"bbq\" is recognized but not yet implemented; \
supported values: \"none\", \"int8\""
.into(),
)),
other => Err(LuciError::InvalidQuery(format!(
"unknown quantization type: \"{other}\" \
(supported: \"none\", \"int8\")"
))),
}
}
pub fn es_name(self) -> &'static str {
match self {
Self::None => "none",
Self::Int8 => "int8",
Self::Int4 => "int4",
Self::Bbq => "bbq",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn supported_values_round_trip() {
for q in [QuantizationType::None, QuantizationType::Int8] {
let parsed = QuantizationType::from_es_name(q.es_name()).unwrap();
assert_eq!(parsed, q);
}
}
#[test]
fn int4_is_rejected_as_unimplemented() {
let err = QuantizationType::from_es_name("int4").unwrap_err();
let msg = format!("{err}");
assert!(msg.contains("int4"), "error must name the value: {msg}");
assert!(
msg.contains("not yet implemented"),
"error must explain why: {msg}"
);
}
#[test]
fn bbq_is_rejected_as_unimplemented() {
let err = QuantizationType::from_es_name("bbq").unwrap_err();
let msg = format!("{err}");
assert!(msg.contains("bbq"), "error must name the value: {msg}");
assert!(
msg.contains("not yet implemented"),
"error must explain why: {msg}"
);
}
#[test]
fn unknown_value_is_rejected() {
let err = QuantizationType::from_es_name("magic").unwrap_err();
let msg = format!("{err}");
assert!(msg.contains("magic"), "error must name the value: {msg}");
assert!(
msg.contains("supported"),
"error must list supported values: {msg}"
);
}
#[test]
fn empty_string_is_rejected() {
assert!(QuantizationType::from_es_name("").is_err());
}
#[test]
fn default_is_int8() {
assert_eq!(QuantizationType::DEFAULT, QuantizationType::Int8);
}
}