fiscalclock 0.1.0

Fiscal calendar engine: compute any fiscal calendar from a JSON definition
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! The JSON API that every binding wraps.
//!
//! Both functions take and return JSON text exactly as documented in
//! `spec/api-v1.md`: keys in the documented order, no insignificant whitespace,
//! and either the operation's result or `{"errors":[...]}`. Because every
//! language calls these same functions, the same inputs give byte-identical
//! output everywhere.

use alloc::format;
use alloc::string::String;

use serde_json::{Map, Value};

use crate::calendar::{FiscalCalendar, Level, Location, Period};
use crate::date::Date;
use crate::error::{Details, Error, ErrorCode};

/// Validates a definition: `{"ok":true,"calendar_id":"…","canonical":"…"}` or errors.
pub fn validate(definition: &str) -> String {
    match FiscalCalendar::from_json(definition) {
        Ok(cal) => validate_ok(&cal),
        Err(errors) => errors_json(errors.as_slice()),
    }
}

/// Validates `definition`, then runs operation `op` with the JSON object `args`.
///
/// Prefer [`FiscalCalendar::call_json`] when making many calls with one
/// definition, so it is validated once.
pub fn call(definition: &str, op: &str, args: &str) -> String {
    match FiscalCalendar::from_json(definition) {
        Ok(cal) => cal.call_json(op, args),
        Err(errors) => errors_json(errors.as_slice()),
    }
}

impl FiscalCalendar {
    /// Runs operation `op` with the JSON object `args` and returns the result
    /// JSON, or `{"errors":[...]}`, exactly as [`call`] does.
    pub fn call_json(&self, op: &str, args: &str) -> String {
        match self.dispatch(op, args) {
            Ok(out) => out,
            Err(e) => errors_json(core::slice::from_ref(&e)),
        }
    }

    fn dispatch(&self, op: &str, args: &str) -> Result<String, Error> {
        let parsed: Value = crate::strict::parse(args).map_err(|e| {
            Error::new(
                ErrorCode::InvalidArgument,
                "",
                format!("the arguments are not valid JSON: {e}"),
            )
        })?;
        let Some(a) = parsed.as_object() else {
            return Err(Error::new(
                ErrorCode::InvalidArgument,
                "",
                "the arguments must be a JSON object",
            ));
        };
        let mut out = String::new();
        match op {
            "validate" => return Ok(validate_ok(self)),
            "locate" => write_location(&mut out, &self.locate(arg_date(a, "date")?)?),
            "period" => {
                let level = arg_level(a, "level")?;
                let p = match arg_year(a, "year")? {
                    Some(year) => self.period(level, year, arg_index(a, "index", level)?)?,
                    None => return Err(year_out_of_range("year")),
                };
                write_period(&mut out, &p);
            }
            "shift" => {
                let p = self.arg_period(a)?;
                let n = arg_n(a)?;
                write_period(&mut out, &self.shift(&p, n)?);
            }
            "parent" => write_period(&mut out, &self.parent(&self.arg_period(a)?)?),
            "children" => {
                let p = self.arg_period(a)?;
                let level = arg_level(a, "level")?;
                write_periods(&mut out, &self.children(&p, level)?);
            }
            "years" => {
                let from = arg_year(a, "from")?.ok_or_else(|| year_out_of_range("from"))?;
                let to = arg_year(a, "to")?.ok_or_else(|| year_out_of_range("to"))?;
                write_periods(&mut out, &self.years(from, to)?);
            }
            "format" => {
                let p = self.arg_period(a)?;
                let scheme = match a.get("scheme") {
                    None | Some(Value::Null) => None,
                    Some(Value::String(s)) => Some(s.as_str()),
                    Some(_) => return Err(wrong_type("scheme", "a string or null")),
                };
                let label = self.format(&p, scheme)?;
                out.push_str("{\"label\":");
                write_str(&mut out, &label);
                out.push_str(",\"scheme\":");
                write_str(&mut out, self.scheme_name(scheme)?);
                out.push('}');
            }
            "parse" => {
                let label = match required(a, "label")? {
                    Value::String(s) => s,
                    _ => return Err(wrong_type("label", "a string")),
                };
                write_period(&mut out, &self.parse(label)?);
            }
            "dimension" | "dimension_csv" => {
                let start = arg_date(a, "start")?;
                let end = arg_date(a, "end_exclusive")?;
                if op == "dimension" {
                    let rows = self.dimension(start, end)?;
                    self.write_rows_json(&mut out, &rows);
                } else {
                    write_str(&mut out, &self.dimension_csv(start, end)?);
                }
            }
            "explain" => {
                let e = self.explain(arg_date(a, "date")?)?;
                out.push_str("{\"date\":");
                write_str(&mut out, &format!("{}", e.date));
                out.push_str(",\"calendar_id\":");
                write_str(&mut out, self.calendar_id());
                out.push_str(",\"steps\":[");
                for (i, s) in e.steps.iter().enumerate() {
                    if i > 0 {
                        out.push(',');
                    }
                    out.push_str("{\"level\":");
                    write_str(&mut out, s.level.name());
                    out.push_str(",\"rule\":");
                    write_str(&mut out, &s.rule);
                    out.push_str(",\"result\":");
                    write_str(&mut out, &s.result);
                    out.push('}');
                }
                out.push_str("]}");
            }
            _ => {
                return Err(Error::new(
                    ErrorCode::InvalidArgument,
                    "op",
                    format!("\"{op}\" is not an operation"),
                )
                .hint("operations: validate, locate, period, shift, parent, children, years, format, parse, dimension, dimension_csv, explain"));
            }
        }
        Ok(out)
    }

    /// Reads the `period` argument: a Period value or its coordinates. Only
    /// `level`, `year` and `index` are read.
    fn arg_period(&self, a: &Map<String, Value>) -> Result<Period, Error> {
        let Value::Object(p) = required(a, "period")? else {
            return Err(wrong_type("period", "an object"));
        };
        let level = arg_level(p, "level").map_err(at_period)?;
        let year = arg_year(p, "year")
            .map_err(at_period)?
            .ok_or_else(|| year_out_of_range("period"))?;
        let index = arg_index(p, "index", level).map_err(at_period)?;
        // Only the coordinates matter: every operation recomputes the bounds.
        Ok(Period {
            level,
            year,
            index,
            start: Date::MIN,
            end_exclusive: Date::MIN,
            days: 0,
            weeks: None,
            has_extra_week: false,
        })
    }
}

fn at_period(mut e: Error) -> Error {
    e.path = String::from("period");
    e
}

fn required<'a>(a: &'a Map<String, Value>, name: &str) -> Result<&'a Value, Error> {
    a.get(name).ok_or_else(|| {
        Error::new(
            ErrorCode::InvalidArgument,
            name,
            format!("{name} is required"),
        )
    })
}

fn wrong_type(name: &str, expected: &str) -> Error {
    Error::new(
        ErrorCode::InvalidArgument,
        name,
        format!("{name} must be {expected}"),
    )
}

fn year_out_of_range(path: &str) -> Error {
    crate::calendar::out_of_range(path, String::from("that fiscal year"))
}

fn arg_date(a: &Map<String, Value>, name: &str) -> Result<Date, Error> {
    match required(a, name)? {
        Value::String(s) => Date::parse_iso(s).ok_or_else(|| {
            Error::new(
                ErrorCode::InvalidDate,
                name,
                format!("\"{s}\" is not a YYYY-MM-DD civil date in the years 0001-9999"),
            )
            .hint("pass a plain date such as \"2024-02-03\", without a time or zone")
        }),
        _ => Err(wrong_type(name, "a YYYY-MM-DD string")),
    }
}

fn arg_level(a: &Map<String, Value>, name: &str) -> Result<Level, Error> {
    match required(a, name)? {
        Value::String(s) => Level::from_name(s).ok_or_else(|| {
            Error::new(
                ErrorCode::InvalidArgument,
                name,
                format!("\"{s}\" is not a level"),
            )
            .hint("levels: year, quarter, period, week")
        }),
        _ => Err(wrong_type(name, "a level name")),
    }
}

/// A year number; `Ok(None)` for an integer that cannot be a supported year.
fn arg_year(a: &Map<String, Value>, name: &str) -> Result<Option<i32>, Error> {
    let v = required(a, name)?;
    if v.is_i64() || v.is_u64() {
        Ok(v.as_i64().and_then(|n| i32::try_from(n).ok()))
    } else {
        Err(wrong_type(name, "an integer"))
    }
}

fn arg_index(a: &Map<String, Value>, name: &str, level: Level) -> Result<Option<u32>, Error> {
    match a.get(name) {
        None | Some(Value::Null) => Ok(None),
        Some(v) if v.is_i64() || v.is_u64() => match v.as_i64().and_then(|n| u32::try_from(n).ok())
        {
            Some(i) => Ok(Some(i)),
            None if level == Level::Year => Ok(Some(0)),
            None => Err(Error::new(
                ErrorCode::PeriodNotFound,
                name,
                format!("there is no {} {v}", level.name()),
            )),
        },
        Some(_) => Err(wrong_type(name, "an integer or null")),
    }
}

fn arg_n(a: &Map<String, Value>) -> Result<i64, Error> {
    let v = required(a, "n")?;
    if let Some(n) = v.as_i64() {
        Ok(n)
    } else if v.is_u64() {
        Err(crate::calendar::out_of_range(
            "n",
            format!("shifting by {v}"),
        ))
    } else {
        Err(wrong_type("n", "an integer"))
    }
}

fn validate_ok(cal: &FiscalCalendar) -> String {
    let mut out = String::from("{\"ok\":true,\"calendar_id\":");
    write_str(&mut out, cal.calendar_id());
    out.push_str(",\"canonical\":");
    write_str(&mut out, cal.canonical_json());
    out.push('}');
    out
}

/// Writes `s` as a JSON string with only the escapes the spec requires.
pub(crate) fn write_str(out: &mut String, s: &str) {
    out.push('"');
    for c in s.chars() {
        match c {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\u{08}' => out.push_str("\\b"),
            '\u{0c}' => out.push_str("\\f"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
            c => out.push(c),
        }
    }
    out.push('"');
}

fn write_date(out: &mut String, d: Date) {
    out.push_str(&format!("\"{d}\""));
}

fn write_period(out: &mut String, p: &Period) {
    out.push_str("{\"level\":");
    write_str(out, p.level.name());
    out.push_str(&format!(",\"year\":{},\"index\":", p.year));
    match p.index {
        Some(i) => out.push_str(&format!("{i}")),
        None => out.push_str("null"),
    }
    out.push_str(",\"start\":");
    write_date(out, p.start);
    out.push_str(",\"end_exclusive\":");
    write_date(out, p.end_exclusive);
    out.push_str(",\"last_day\":");
    write_date(out, p.last_day());
    out.push_str(&format!(",\"days\":{},\"weeks\":", p.days));
    match p.weeks {
        Some(w) => out.push_str(&format!("{w}")),
        None => out.push_str("null"),
    }
    out.push_str(if p.has_extra_week {
        ",\"has_extra_week\":true}"
    } else {
        ",\"has_extra_week\":false}"
    });
}

fn write_periods(out: &mut String, ps: &[Period]) {
    out.push('[');
    for (i, p) in ps.iter().enumerate() {
        if i > 0 {
            out.push(',');
        }
        write_period(out, p);
    }
    out.push(']');
}

fn write_location(out: &mut String, l: &Location) {
    out.push_str("{\"date\":");
    write_date(out, l.date);
    for (name, p) in [
        ("year", &l.year),
        ("quarter", &l.quarter),
        ("period", &l.period),
        ("week", &l.week),
    ] {
        out.push_str(&format!(",\"{name}\":"));
        write_period(out, p);
    }
    out.push_str(&format!(
        ",\"day_of_year\":{},\"day_of_quarter\":{},\"day_of_period\":{},\"day_of_week\":{}}}",
        l.day_of_year, l.day_of_quarter, l.day_of_period, l.day_of_week
    ));
}

fn write_error(out: &mut String, e: &Error) {
    out.push_str("{\"code\":");
    write_str(out, e.code.as_str());
    out.push_str(",\"path\":");
    write_str(out, &e.path);
    out.push_str(",\"message\":");
    write_str(out, &e.message);
    out.push_str(",\"hint\":");
    match &e.hint {
        Some(h) => write_str(out, h),
        None => out.push_str("null"),
    }
    out.push_str(",\"details\":");
    match &e.details {
        None => out.push_str("null"),
        Some(Details::Years(years)) => {
            out.push_str("{\"years\":[");
            for (i, y) in years.iter().enumerate() {
                if i > 0 {
                    out.push(',');
                }
                out.push_str(&format!("{y}"));
            }
            out.push_str("]}");
        }
        Some(Details::Candidates(candidates)) => {
            out.push_str("{\"candidates\":[");
            for (i, c) in candidates.iter().enumerate() {
                if i > 0 {
                    out.push(',');
                }
                out.push_str("{\"period\":");
                write_period(out, &c.period);
                out.push_str(",\"scheme\":");
                write_str(out, &c.scheme);
                out.push_str(",\"level\":");
                write_str(out, c.level.name());
                out.push('}');
            }
            out.push_str("]}");
        }
    }
    out.push('}');
}

/// `{"errors":[...]}` for a list of errors.
pub fn errors_json(errors: &[Error]) -> String {
    let mut out = String::from("{\"errors\":[");
    for (i, e) in errors.iter().enumerate() {
        if i > 0 {
            out.push(',');
        }
        write_error(&mut out, e);
    }
    out.push_str("]}");
    out
}

/// The `TIMESTAMP_REJECTED` error for argument `path`, for bindings to return
/// when a caller passes a timestamp (such as a Python `datetime` or a
/// JavaScript `Date`) where a civil date is expected. Built here so that every
/// binding returns the same bytes.
pub fn timestamp_rejected(path: &str) -> String {
    errors_json(&[Error::new(
        ErrorCode::TimestampRejected,
        path,
        "a timestamp was passed where a civil date (year, month, day) is expected",
    )
    .hint("convert the timestamp to a date in the time zone you mean, then pass that date")])
}