use crate::value::VectorCoordKind;
impl VectorCoordKind {
pub fn coordinate_tokens(text: &str) -> Option<Vec<VectorCoordToken>> {
let body = text.trim().strip_prefix('[')?.strip_suffix(']')?;
if body.trim().is_empty() {
return Some(Vec::new());
}
Some(body.split(',').map(VectorCoordToken::classify).collect())
}
pub fn parse_coordinates(text: &str) -> Option<Vec<f64>> {
Self::coordinate_tokens(text)?
.into_iter()
.map(|token| match token {
VectorCoordToken::Number(value) => Some(value),
VectorCoordToken::NonNumeric | VectorCoordToken::Malformed => None,
})
.collect()
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum VectorCoordToken {
Number(f64),
NonNumeric,
Malformed,
}
impl VectorCoordToken {
fn classify(token: &str) -> Self {
let token = token.trim();
if let Ok(value) = token.replace('_', "").parse::<f64>() {
return VectorCoordToken::Number(value);
}
let quoted = token.starts_with('\'') || token.starts_with('"');
let word = token
.chars()
.next()
.is_some_and(|first| first.is_ascii_alphabetic() || first == '_')
&& token.chars().all(|c| c.is_ascii_alphanumeric() || c == '_');
if quoted || word {
VectorCoordToken::NonNumeric
} else {
VectorCoordToken::Malformed
}
}
}
pub const VECTOR_DIMENSION_RANGE: std::ops::RangeInclusive<i64> = 1..=4096;
pub fn dimension_range_text() -> String {
format!(
"{} to {}",
VECTOR_DIMENSION_RANGE.start(),
VECTOR_DIMENSION_RANGE.end()
)
}