use serde::{Deserialize, Serialize};
use serde_json::Number;
use crate::pyfmt::float_repr;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub(super) enum Num {
Minus,
Zero,
Int,
Dot,
Frac,
E,
Sign,
Exp,
}
pub(super) fn next(at: Num, ch: char) -> Option<Num> {
Some(match (at, ch) {
(Num::Minus, '0') => Num::Zero,
(Num::Minus, '1'..='9') | (Num::Int, '0'..='9') => Num::Int,
(Num::Zero | Num::Int, '.') => Num::Dot,
(Num::Dot | Num::Frac, '0'..='9') => Num::Frac,
(Num::Zero | Num::Int | Num::Frac, 'e' | 'E') => Num::E,
(Num::E, '+' | '-') => Num::Sign,
(Num::E | Num::Sign | Num::Exp, '0'..='9') => Num::Exp,
_ => return None,
})
}
pub(super) fn whole(at: Num) -> bool {
matches!(at, Num::Zero | Num::Int | Num::Frac | Num::Exp)
}
pub(super) fn spelled(text: &str) -> Result<(), &'static str> {
let Ok(read) = serde_json::from_str::<Number>(text) else {
return Err("a number out of range");
};
let codec = if read.is_f64() {
text.parse::<f64>().ok().map(float_repr)
} else {
Some(read.to_string())
};
if codec.as_deref() == Some(text) {
Ok(())
} else {
Err("a number not spelled as the codec writes it")
}
}