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 crate::ffi;
#[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 bits(self) -> u32 {
let mut b = 0;
for (on, bit) in [
(self.hex, ffi::FIG_SPELL_HEX),
(self.octal, ffi::FIG_SPELL_OCTAL),
(self.binary, ffi::FIG_SPELL_BINARY),
(self.underscores, ffi::FIG_SPELL_UNDERSCORES),
(self.leading_zero, ffi::FIG_SPELL_LEADING_ZERO),
(self.bare_dot, ffi::FIG_SPELL_BARE_DOT),
(self.plus, ffi::FIG_SPELL_PLUS),
] {
if on {
b |= bit;
}
}
b
}
}
pub fn spellable(raw: &str, s: Spelling) -> bool {
unsafe { ffi::fig_kit_number_spellable(raw.as_ptr(), raw.len(), s.bits()) }
}
pub fn canonical(raw: &str) -> String {
let mut out = vec![0u8; raw.len() + ffi::FIG_KIT_ANSWER_ROOM];
let n = unsafe {
ffi::fig_kit_number_canonical(raw.as_ptr(), raw.len(), out.as_mut_ptr(), out.len())
};
assert!(
n <= out.len(),
"fig_kit_number_canonical outgrew FIG_KIT_ANSWER_ROOM"
);
out.truncate(n);
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 unsafe { ffi::fig_kit_number_non_finite(raw.as_ptr(), raw.len()) } {
ffi::FIG_NON_FINITE_INF => Some(NonFinite::Inf),
ffi::FIG_NON_FINITE_NEG_INF => Some(NonFinite::NegInf),
ffi::FIG_NON_FINITE_NAN => Some(NonFinite::NaN),
_ => None,
}
}
}
pub mod datetime {
use crate::ExtKind;
use crate::ffi;
#[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 bits(self) -> u32 {
(if self.minute_precision {
ffi::FIG_DATETIME_MINUTE_PRECISION
} else {
0
}) | (if self.time_only {
ffi::FIG_DATETIME_TIME_ONLY
} else {
0
})
}
}
pub fn classify(raw: &str, opts: Options) -> Option<ExtKind> {
let kind = unsafe { ffi::fig_kit_datetime_classify(raw.as_ptr(), raw.len(), opts.bits()) };
if kind < 0 {
None
} else {
ExtKind::from_c(kind)
}
}
pub fn valid_date(s: &str) -> bool {
unsafe { ffi::fig_kit_datetime_valid_date(s.as_ptr(), s.len()) }
}
pub fn valid_time(s: &str, opts: Options) -> bool {
unsafe { ffi::fig_kit_datetime_valid_time(s.as_ptr(), s.len(), opts.bits()) }
}
pub fn valid_offset(s: &str) -> bool {
unsafe { ffi::fig_kit_datetime_valid_offset(s.as_ptr(), s.len()) }
}
}
#[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"));
}
}