#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Instant {
pub epoch_seconds: i64,
}
impl Instant {
pub fn to_iso_string(self) -> String {
let days = self.epoch_seconds.div_euclid(86_400);
let secs_of_day = self.epoch_seconds.rem_euclid(86_400);
let (year, month, day) = civil_from_days(days);
let hour = secs_of_day / 3600;
let minute = (secs_of_day % 3600) / 60;
let second = secs_of_day % 60;
format!("{year:04}-{month:02}-{day:02}T{hour:02}:{minute:02}:{second:02}Z")
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum LimaValue {
Null,
Bool(bool),
Int(i64),
Float(f64),
String(String),
Instant(Instant),
Array(Vec<LimaValue>),
Mapping(Vec<(String, LimaValue)>),
}
impl LimaValue {
pub fn depth(&self) -> u32 {
match self {
LimaValue::Array(items) => 1 + items.iter().map(LimaValue::depth).max().unwrap_or(0),
LimaValue::Mapping(entries) => {
1 + entries.iter().map(|(_, v)| v.depth()).max().unwrap_or(0)
}
_ => 0,
}
}
}
pub fn set_mapping<V>(entries: &mut Vec<(String, V)>, key: String, value: V) {
if let Some(slot) = entries.iter_mut().find(|(k, _)| *k == key) {
slot.1 = value;
} else {
entries.push((key, value));
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct InsertedAt {
pub line: u32,
pub offset: usize,
pub token: String,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ReferenceSource {
pub raw: Option<String>,
pub line: u32,
pub col: usize,
pub tab_adjust: Option<Vec<usize>>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum PositionedValue {
Null {
line: u32,
inserted_at: Option<InsertedAt>,
prior_insertions: Vec<InsertedAt>,
},
Bool {
value: bool,
line: u32,
inserted_at: Option<InsertedAt>,
prior_insertions: Vec<InsertedAt>,
},
Int {
value: i64,
line: u32,
inserted_at: Option<InsertedAt>,
prior_insertions: Vec<InsertedAt>,
},
Float {
value: f64,
line: u32,
inserted_at: Option<InsertedAt>,
prior_insertions: Vec<InsertedAt>,
},
String {
value: String,
line: u32,
quoted: bool,
ref_source: Option<ReferenceSource>,
inserted_at: Option<InsertedAt>,
prior_insertions: Vec<InsertedAt>,
},
Instant {
value: Instant,
line: u32,
inserted_at: Option<InsertedAt>,
prior_insertions: Vec<InsertedAt>,
},
Array {
items: Vec<PositionedValue>,
line: u32,
inserted_at: Option<InsertedAt>,
prior_insertions: Vec<InsertedAt>,
},
Mapping {
entries: Vec<(String, PositionedValue)>,
line: u32,
inserted_at: Option<InsertedAt>,
prior_insertions: Vec<InsertedAt>,
},
}
impl PositionedValue {
pub fn line(&self) -> u32 {
match self {
PositionedValue::Null { line, .. }
| PositionedValue::Bool { line, .. }
| PositionedValue::Int { line, .. }
| PositionedValue::Float { line, .. }
| PositionedValue::String { line, .. }
| PositionedValue::Instant { line, .. }
| PositionedValue::Array { line, .. }
| PositionedValue::Mapping { line, .. } => *line,
}
}
pub fn inserted_at(&self) -> Option<&InsertedAt> {
match self {
PositionedValue::Null { inserted_at, .. }
| PositionedValue::Bool { inserted_at, .. }
| PositionedValue::Int { inserted_at, .. }
| PositionedValue::Float { inserted_at, .. }
| PositionedValue::String { inserted_at, .. }
| PositionedValue::Instant { inserted_at, .. }
| PositionedValue::Array { inserted_at, .. }
| PositionedValue::Mapping { inserted_at, .. } => inserted_at.as_ref(),
}
}
pub fn prior_insertions(&self) -> &[InsertedAt] {
match self {
PositionedValue::Null {
prior_insertions, ..
}
| PositionedValue::Bool {
prior_insertions, ..
}
| PositionedValue::Int {
prior_insertions, ..
}
| PositionedValue::Float {
prior_insertions, ..
}
| PositionedValue::String {
prior_insertions, ..
}
| PositionedValue::Instant {
prior_insertions, ..
}
| PositionedValue::Array {
prior_insertions, ..
}
| PositionedValue::Mapping {
prior_insertions, ..
} => prior_insertions,
}
}
pub fn to_plain_value(&self) -> LimaValue {
match self {
PositionedValue::Null { .. } => LimaValue::Null,
PositionedValue::Bool { value, .. } => LimaValue::Bool(*value),
PositionedValue::Int { value, .. } => LimaValue::Int(*value),
PositionedValue::Float { value, .. } => LimaValue::Float(*value),
PositionedValue::String { value, .. } => LimaValue::String(value.clone()),
PositionedValue::Instant { value, .. } => LimaValue::Instant(*value),
PositionedValue::Array { items, .. } => {
LimaValue::Array(items.iter().map(PositionedValue::to_plain_value).collect())
}
PositionedValue::Mapping { entries, .. } => LimaValue::Mapping(
entries
.iter()
.map(|(k, v)| (k.clone(), v.to_plain_value()))
.collect(),
),
}
}
pub fn depth(&self) -> u32 {
match self {
PositionedValue::Array { items, .. } => {
1 + items.iter().map(PositionedValue::depth).max().unwrap_or(0)
}
PositionedValue::Mapping { entries, .. } => {
1 + entries.iter().map(|(_, v)| v.depth()).max().unwrap_or(0)
}
_ => 0,
}
}
}
pub trait Builder {
const POSITIONED: bool = false;
type Value: Clone;
type Mapping;
fn v_null(line: u32) -> Self::Value;
fn v_bool(value: bool, line: u32) -> Self::Value;
fn v_int(value: i64, line: u32) -> Self::Value;
fn v_float(value: f64, line: u32) -> Self::Value;
fn v_string(value: String, line: u32, quoted: bool) -> Self::Value;
fn v_string_src(value: String, line: u32, _source: ReferenceSource) -> Self::Value {
Self::v_string(value, line, false)
}
fn v_instant(value: Instant, line: u32) -> Self::Value;
fn v_array(items: Vec<Self::Value>, line: u32) -> Self::Value;
fn v_mapping(entries: Self::Mapping, line: u32) -> Self::Value;
fn m_create() -> Self::Mapping;
fn m_create_with(key: String, value: Self::Value) -> Self::Mapping;
fn m_has_key(entries: &Self::Mapping, key: &str) -> bool;
fn m_set(entries: &mut Self::Mapping, key: String, value: Self::Value);
fn m_max_depth(entries: &Self::Mapping) -> u32;
}
pub struct PlainBuilder;
impl Builder for PlainBuilder {
type Value = LimaValue;
type Mapping = Vec<(String, LimaValue)>;
fn v_null(_line: u32) -> LimaValue {
LimaValue::Null
}
fn v_bool(value: bool, _line: u32) -> LimaValue {
LimaValue::Bool(value)
}
fn v_int(value: i64, _line: u32) -> LimaValue {
LimaValue::Int(value)
}
fn v_float(value: f64, _line: u32) -> LimaValue {
LimaValue::Float(value)
}
fn v_string(value: String, _line: u32, _quoted: bool) -> LimaValue {
LimaValue::String(value)
}
fn v_instant(value: Instant, _line: u32) -> LimaValue {
LimaValue::Instant(value)
}
fn v_array(items: Vec<LimaValue>, _line: u32) -> LimaValue {
LimaValue::Array(items)
}
fn v_mapping(entries: Self::Mapping, _line: u32) -> LimaValue {
LimaValue::Mapping(entries)
}
fn m_create() -> Self::Mapping {
Vec::new()
}
fn m_create_with(key: String, value: LimaValue) -> Self::Mapping {
vec![(key, value)]
}
fn m_has_key(entries: &Self::Mapping, key: &str) -> bool {
entries.iter().any(|(k, _)| k == key)
}
fn m_set(entries: &mut Self::Mapping, key: String, value: LimaValue) {
set_mapping(entries, key, value)
}
fn m_max_depth(entries: &Self::Mapping) -> u32 {
entries.iter().map(|(_, v)| v.depth()).max().unwrap_or(0)
}
}
pub struct PositionedBuilder;
impl Builder for PositionedBuilder {
const POSITIONED: bool = true;
type Value = PositionedValue;
type Mapping = Vec<(String, PositionedValue)>;
fn v_null(line: u32) -> PositionedValue {
PositionedValue::Null {
line,
inserted_at: None,
prior_insertions: Vec::new(),
}
}
fn v_bool(value: bool, line: u32) -> PositionedValue {
PositionedValue::Bool {
value,
line,
inserted_at: None,
prior_insertions: Vec::new(),
}
}
fn v_int(value: i64, line: u32) -> PositionedValue {
PositionedValue::Int {
value,
line,
inserted_at: None,
prior_insertions: Vec::new(),
}
}
fn v_float(value: f64, line: u32) -> PositionedValue {
PositionedValue::Float {
value,
line,
inserted_at: None,
prior_insertions: Vec::new(),
}
}
fn v_string(value: String, line: u32, quoted: bool) -> PositionedValue {
PositionedValue::String {
value,
line,
quoted,
ref_source: None,
inserted_at: None,
prior_insertions: Vec::new(),
}
}
fn v_string_src(value: String, line: u32, source: ReferenceSource) -> PositionedValue {
PositionedValue::String {
value,
line,
quoted: false,
ref_source: Some(source),
inserted_at: None,
prior_insertions: Vec::new(),
}
}
fn v_instant(value: Instant, line: u32) -> PositionedValue {
PositionedValue::Instant {
value,
line,
inserted_at: None,
prior_insertions: Vec::new(),
}
}
fn v_array(items: Vec<PositionedValue>, line: u32) -> PositionedValue {
PositionedValue::Array {
items,
line,
inserted_at: None,
prior_insertions: Vec::new(),
}
}
fn v_mapping(entries: Self::Mapping, line: u32) -> PositionedValue {
PositionedValue::Mapping {
entries,
line,
inserted_at: None,
prior_insertions: Vec::new(),
}
}
fn m_create() -> Self::Mapping {
Vec::new()
}
fn m_create_with(key: String, value: PositionedValue) -> Self::Mapping {
vec![(key, value)]
}
fn m_has_key(entries: &Self::Mapping, key: &str) -> bool {
entries.iter().any(|(k, _)| k == key)
}
fn m_set(entries: &mut Self::Mapping, key: String, value: PositionedValue) {
set_mapping(entries, key, value)
}
fn m_max_depth(entries: &Self::Mapping) -> u32 {
entries.iter().map(|(_, v)| v.depth()).max().unwrap_or(0)
}
}
pub fn days_from_civil(y: i64, m: u32, d: u32) -> i64 {
let y = if m <= 2 { y - 1 } else { y };
let era = if y >= 0 { y } else { y - 399 } / 400;
let yoe = y - era * 400; let mp = (m as i64 + 9) % 12; let doy = (153 * mp + 2) / 5 + d as i64 - 1; let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; era * 146_097 + doe - 719_468
}
pub fn civil_from_days(z: i64) -> (i64, u32, u32) {
let z = z + 719_468;
let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
let doe = z - era * 146_097; let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; let y = yoe + era * 400;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); let mp = (5 * doy + 2) / 153; let d = (doy - (153 * mp + 2) / 5 + 1) as u32; let m = if mp < 10 { mp + 3 } else { mp - 9 } as u32; (if m <= 2 { y + 1 } else { y }, m, d)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn days_from_civil_roundtrips() {
for &(y, m, d) in &[
(1970, 1, 1),
(2024, 2, 29),
(2000, 2, 29),
(1, 1, 1),
(9999, 12, 31),
(1969, 12, 31),
] {
let days = days_from_civil(y, m, d);
assert_eq!(
civil_from_days(days),
(y, m, d),
"roundtrip failed for {y}-{m}-{d}"
);
}
assert_eq!(days_from_civil(1970, 1, 1), 0);
assert_eq!(
days_from_civil(2024, 3, 1),
days_from_civil(2024, 2, 29) + 1
);
}
}