pub mod grammar;
pub mod json_string;
pub mod literal;
pub mod print;
pub mod rows;
pub mod scan;
pub mod tree;
pub mod xml;
pub mod number {
use std::borrow::Cow;
use fig_core::kit::number as core_number;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Spelling {
pub hex: bool,
pub octal: bool,
pub binary: bool,
pub underscores: bool,
pub leading_zero: bool,
pub bare_dot: bool,
pub plus: bool,
}
impl Spelling {
pub const NONE: Spelling = Spelling {
hex: false,
octal: false,
binary: false,
underscores: false,
leading_zero: false,
bare_dot: false,
plus: false,
};
pub const JSON: Spelling = Spelling::NONE;
pub const JSON5: Spelling = Spelling {
hex: true,
bare_dot: true,
plus: true,
..Spelling::NONE
};
pub const YAML_1_2: Spelling = Spelling {
hex: true,
octal: true,
leading_zero: true,
bare_dot: true,
plus: true,
..Spelling::NONE
};
fn to_core(self) -> core_number::Spelling {
core_number::Spelling {
hex: self.hex,
octal: self.octal,
binary: self.binary,
underscores: self.underscores,
leading_zero: self.leading_zero,
bare_dot: self.bare_dot,
plus: self.plus,
non_finite: None,
}
}
}
pub fn spellable(raw: &str, s: Spelling) -> bool {
core_number::spellable(raw.as_bytes(), s.to_core())
}
pub fn canonical(raw: &str) -> String {
let mut out = Vec::with_capacity(raw.len() + core_number::ANSWER_ROOM);
core_number::write_canonical(&mut out, raw.as_bytes());
assert!(
out.len() <= raw.len() + core_number::ANSWER_ROOM,
"the canonical number outgrew its answer room"
);
String::from_utf8(out)
.unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned())
}
pub fn text(raw: &str, s: Spelling) -> Cow<'_, str> {
if spellable(raw, s) {
Cow::Borrowed(raw)
} else {
Cow::Owned(canonical(raw))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum NonFinite {
Inf,
NegInf,
NaN,
}
pub fn non_finite(raw: &str) -> Option<NonFinite> {
match core_number::non_finite(raw.as_bytes())? {
core_number::NonFinite::Inf => Some(NonFinite::Inf),
core_number::NonFinite::NegInf => Some(NonFinite::NegInf),
core_number::NonFinite::Nan => Some(NonFinite::NaN),
}
}
}
pub mod datetime {
use crate::ExtKind;
use fig_core::kit::datetime as core_datetime;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Options {
pub minute_precision: bool,
pub time_only: bool,
}
impl Default for Options {
fn default() -> Self {
Options {
minute_precision: true,
time_only: true,
}
}
}
impl Options {
fn to_core(self) -> core_datetime::Options {
core_datetime::Options {
allow_minute_precision: self.minute_precision,
allow_time_only: self.time_only,
}
}
}
pub fn classify(raw: &str, opts: Options) -> Option<ExtKind> {
let kind = core_datetime::classify(raw.as_bytes(), opts.to_core()).ok()?;
ExtKind::from_c(kind as i32)
}
pub fn valid_date(s: &str) -> bool {
core_datetime::validate_date(s.as_bytes()).is_ok()
}
pub fn valid_time(s: &str, opts: Options) -> bool {
core_datetime::validate_time(s.as_bytes(), opts.to_core()).is_ok()
}
pub fn valid_offset(s: &str) -> bool {
core_datetime::validate_offset(s.as_bytes()).is_ok()
}
}
#[cfg(test)]
mod tests {
use super::datetime::{self, Options};
use super::number::{self, NonFinite, Spelling};
use crate::ExtKind;
#[test]
fn number_spelling() {
assert!(!number::spellable("0xff", Spelling::JSON));
assert!(number::spellable("0xff", Spelling::JSON5));
assert!(number::spellable("0o755", Spelling::YAML_1_2));
assert!(!number::spellable("1_000", Spelling::YAML_1_2));
let mut s = Spelling::default();
s.underscores = true;
assert!(number::spellable("1_000", s));
assert_eq!(number::text("0xff", Spelling::JSON), "255");
assert_eq!(number::text("0xff", Spelling::JSON5), "0xff");
}
#[test]
fn number_canonical() {
assert_eq!(number::canonical("0xdead_beef"), "3735928559");
assert_eq!(number::canonical(".5"), "0.5");
assert_eq!(number::canonical("+5."), "5.0");
assert_eq!(number::canonical("0xg"), "0xg");
let long = "9".repeat(100_000);
assert_eq!(number::canonical(&format!("+{long}")), long);
assert_eq!(number::canonical("1é"), "1é");
}
#[test]
fn number_non_finite() {
assert_eq!(number::non_finite("-.inf"), Some(NonFinite::NegInf));
assert_eq!(number::non_finite("Infinity"), Some(NonFinite::Inf));
assert_eq!(number::non_finite(".NaN"), Some(NonFinite::NaN));
assert_eq!(number::non_finite("1e999"), None);
}
#[test]
fn datetime_classify() {
let d = Options::default();
assert_eq!(
datetime::classify("1979-05-27T07:32:00Z", d),
Some(ExtKind::OffsetDateTime)
);
assert_eq!(
datetime::classify("1979-05-27 07:32:00", d),
Some(ExtKind::LocalDateTime)
);
assert_eq!(
datetime::classify("1979-05-27", d),
Some(ExtKind::LocalDate)
);
assert_eq!(datetime::classify("07:32:00", d), Some(ExtKind::LocalTime));
assert_eq!(datetime::classify("2002-02-30", d), None);
let mut yaml = d;
yaml.time_only = false;
assert_eq!(datetime::classify("07:32:00", yaml), None);
let mut toml_1_0 = d;
toml_1_0.minute_precision = false;
assert!(!datetime::valid_time("07:32", toml_1_0));
assert!(datetime::valid_date("2024-02-29"));
assert!(!datetime::valid_offset("+24:00"));
}
}