1use crate::error::{err, ErrorCode, Result};
7use crate::tables;
8
9pub mod limits {
11 pub const NAME: usize = 64;
13 pub const FUNCTION_NAME: usize = 512;
15 pub const ARGS: usize = 32;
17 pub const NUMBER: usize = 128;
19 pub const STRING: usize = 1024;
21 pub const SUMMARY: usize = 1024;
23 pub const BODY: usize = 1_048_576;
25 pub const INPUTS: usize = 4096;
27 pub const LICENCE_SPDX: usize = 64;
29 pub const LICENCE_URL: usize = 512;
31 pub const KEYS: usize = 64;
33 pub const MEDIA_TYPE: usize = 128;
35 pub const INDEX_UNIT: usize = 64;
37 pub const BLOB_LENGTH: u64 = 9_007_199_254_740_991;
39 pub const EXTENT: usize = 4096;
41 pub const MEMBERS: usize = 1_048_576;
43}
44
45fn is_lower_digit(c: u8) -> bool {
46 c.is_ascii_lowercase() || c.is_ascii_digit()
47}
48
49pub(crate) fn is_name_grammar(s: &str) -> bool {
51 let b = s.as_bytes();
52 match b.first() {
53 Some(c) if c.is_ascii_lowercase() => {}
54 _ => return false,
55 }
56 let mut prev_underscore = false;
57 for &c in &b[1..] {
58 if c == b'_' {
59 if prev_underscore {
60 return false;
61 }
62 prev_underscore = true;
63 } else if is_lower_digit(c) {
64 prev_underscore = false;
65 } else {
66 return false;
67 }
68 }
69 !prev_underscore
70}
71
72pub fn check_name(s: &str, path: &str) -> Result<()> {
74 if !is_name_grammar(s) {
75 return err(ErrorCode::CanonName, path, format!("not a lowercase snake_case name: {s:?}"));
76 }
77 if s.len() > limits::NAME {
78 return err(ErrorCode::CanonLimit, path, "name longer than 64 characters");
79 }
80 Ok(())
81}
82
83pub fn check_arg_name(s: &str, path: &str) -> Result<()> {
85 if !is_name_grammar(s) {
86 return err(ErrorCode::CanonArgName, path, format!("not a lowercase snake_case name: {s:?}"));
87 }
88 if s.len() > limits::NAME {
89 return err(ErrorCode::CanonLimit, path, "name longer than 64 characters");
90 }
91 if s == "asof" || s.starts_with("dm_") {
92 return err(ErrorCode::CanonArgName, path, format!("reserved argument name {s:?}"));
93 }
94 Ok(())
95}
96
97pub fn check_dns_name(s: &str, path: &str) -> Result<()> {
99 if s.len() > 253 {
100 return err(ErrorCode::CanonName, path, "DNS name longer than 253 characters");
101 }
102 let labels: Vec<&str> = s.split('.').collect();
103 if labels.len() < 2 {
104 return err(ErrorCode::CanonName, path, format!("DNS name needs at least two labels: {s:?}"));
105 }
106 for l in &labels {
107 let b = l.as_bytes();
108 let ok = !b.is_empty()
109 && b.len() <= 63
110 && b.iter().all(|&c| is_lower_digit(c) || c == b'-')
111 && b[0] != b'-'
112 && b[b.len() - 1] != b'-';
113 if !ok {
114 return err(ErrorCode::CanonName, path, format!("bad DNS label {l:?}"));
115 }
116 }
117 if labels.last().is_some_and(|l| l.bytes().all(|c| c.is_ascii_digit())) {
118 return err(ErrorCode::CanonName, path, "top-level label is numeric");
119 }
120 Ok(())
121}
122
123pub fn check_namespace(s: &str, path: &str) -> Result<()> {
125 let mut parts = s.split('/');
126 check_dns_name(parts.next().unwrap_or(""), path)?;
127 for seg in parts {
128 check_name(seg, path)?;
129 }
130 Ok(())
131}
132
133pub(crate) fn host_of(namespace: &str) -> &str {
135 namespace.split('/').next().unwrap_or(namespace)
136}
137
138pub fn check_function_name(s: &str, path: &str) -> Result<()> {
140 let Some((ns, f)) = s.rsplit_once('/') else {
141 return err(ErrorCode::CanonName, path, format!("function name has no namespace: {s:?}"));
142 };
143 check_namespace(ns, path)?;
144 check_name(f, path)?;
145 if s.len() > limits::FUNCTION_NAME {
146 return err(ErrorCode::CanonLimit, path, "function name longer than 512 characters");
147 }
148 Ok(())
149}
150
151pub fn check_hex(s: &str, digits: usize, path: &str) -> Result<()> {
153 if s.len() != digits || !s.bytes().all(|c| c.is_ascii_digit() || (b'a'..=b'f').contains(&c)) {
154 return err(ErrorCode::CanonHex, path, format!("expected {digits} lowercase hex digits"));
155 }
156 Ok(())
157}
158
159pub fn check_key_ref(s: &str, path: &str) -> Result<()> {
161 let Some((host, id)) = s.split_once('#') else {
162 return err(ErrorCode::CanonName, path, "a key reference is <host>#<key id>");
163 };
164 check_dns_name(host, path)?;
165 check_hex(id, 16, path)
166}
167
168pub(crate) fn is_number_grammar(s: &str) -> bool {
170 let b = s.as_bytes();
171 let mut i = 0;
172 if b.first() == Some(&b'-') {
173 i = 1;
174 }
175 match b.get(i) {
176 Some(b'0') => i += 1,
177 Some(b'1'..=b'9') => {
178 while b.get(i).is_some_and(u8::is_ascii_digit) {
179 i += 1;
180 }
181 }
182 _ => return false,
183 }
184 if b.get(i) == Some(&b'.') {
185 let frac = &b[i + 1..];
186 if frac.is_empty() || !frac.iter().all(u8::is_ascii_digit) || frac[frac.len() - 1] == b'0' {
187 return false;
188 }
189 i = b.len();
190 }
191 i == b.len() && s != "-0"
192}
193
194pub(crate) fn cmp_numbers(a: &str, b: &str) -> core::cmp::Ordering {
196 use core::cmp::Ordering;
197 let (an, a) = a.strip_prefix('-').map_or((false, a), |x| (true, x));
198 let (bn, b) = b.strip_prefix('-').map_or((false, b), |x| (true, x));
199 let magnitude = || {
201 let (ai, af) = a.split_once('.').unwrap_or((a, ""));
202 let (bi, bf) = b.split_once('.').unwrap_or((b, ""));
203 ai.len().cmp(&bi.len()).then_with(|| ai.cmp(bi)).then_with(|| af.cmp(bf))
204 };
205 match (an, bn) {
206 (false, false) => magnitude(),
207 (true, true) => magnitude().reverse(),
208 (true, false) => Ordering::Less,
209 (false, true) => Ordering::Greater,
210 }
211}
212
213pub(crate) fn is_integer(s: &str) -> bool {
215 !s.contains('.')
216}
217
218pub(crate) fn split_slice_entry(s: &str) -> Option<(&str, &str)> {
220 let rest = s.strip_prefix("slice:")?;
221 let (id, members) = rest.split_once(':')?;
222 let hex = |h: &str| h.len() == 64 && h.bytes().all(|c| c.is_ascii_digit() || (b'a'..=b'f').contains(&c));
223 (hex(id) && hex(members)).then_some((id, members))
224}
225
226pub fn check_input_entry(s: &str, path: &str) -> Result<()> {
229 if s.starts_with("slice:") {
230 if split_slice_entry(s).is_none() {
231 return err(ErrorCode::CanonHex, path, format!("not a slice entry slice:<64 hex>:<64 hex>: {s:?}"));
232 }
233 return Ok(());
234 }
235 check_hex(s, 64, path)
236}
237
238pub fn check_number(s: &str, path: &str) -> Result<()> {
240 if !is_number_grammar(s) {
241 return err(ErrorCode::CanonNumber, path, format!("not a canonical decimal: {s:?}"));
242 }
243 if s.len() > limits::NUMBER {
244 return err(ErrorCode::CanonLimit, path, "number longer than 128 characters");
245 }
246 Ok(())
247}
248
249fn digits(b: &[u8]) -> Option<u32> {
250 if b.is_empty() || !b.iter().all(u8::is_ascii_digit) {
251 return None;
252 }
253 Some(b.iter().fold(0u32, |a, &c| a * 10 + u32::from(c - b'0')))
254}
255
256fn days_in_month(y: u32, m: u32) -> u32 {
257 match m {
258 1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
259 4 | 6 | 9 | 11 => 30,
260 2 if (y.is_multiple_of(4) && !y.is_multiple_of(100)) || y.is_multiple_of(400) => 29,
261 2 => 28,
262 _ => 0,
263 }
264}
265
266pub(crate) fn is_date(s: &str) -> bool {
268 let b = s.as_bytes();
269 if b.len() != 10 || b[4] != b'-' || b[7] != b'-' {
270 return false;
271 }
272 let (Some(y), Some(m), Some(d)) = (digits(&b[0..4]), digits(&b[5..7]), digits(&b[8..10])) else {
273 return false;
274 };
275 (1..=9999).contains(&y) && (1..=12).contains(&m) && d >= 1 && d <= days_in_month(y, m)
276}
277
278pub fn check_date(s: &str, path: &str) -> Result<()> {
280 if !is_date(s) {
281 return err(ErrorCode::CanonDate, path, format!("not a YYYY-MM-DD calendar date: {s:?}"));
282 }
283 Ok(())
284}
285
286pub(crate) fn is_instant(s: &str) -> bool {
288 let b = s.as_bytes();
289 if b.len() != 27 || !s.is_char_boundary(10) || !is_date(&s[..10]) {
290 return false;
291 }
292 if b[10] != b'T' || b[13] != b':' || b[16] != b':' || b[19] != b'.' || b[26] != b'Z' {
293 return false;
294 }
295 let (Some(h), Some(mi), Some(se), Some(_)) =
296 (digits(&b[11..13]), digits(&b[14..16]), digits(&b[17..19]), digits(&b[20..26]))
297 else {
298 return false;
299 };
300 h < 24 && mi < 60 && se < 60
301}
302
303pub fn check_instant(s: &str, path: &str) -> Result<()> {
305 if !is_instant(s) {
306 return err(ErrorCode::CanonInstant, path, format!("not YYYY-MM-DDTHH:MM:SS.ffffffZ: {s:?}"));
307 }
308 Ok(())
309}
310
311fn is_restricted_name(s: &str) -> bool {
312 let b = s.as_bytes();
313 !b.is_empty() && is_lower_digit(b[0]) && b.iter().all(|&c| is_lower_digit(c) || b"!#$&-^_.+".contains(&c))
314}
315
316pub(crate) fn parse_blob(s: &str) -> core::result::Result<([u8; 32], u64, &str), String> {
318 let mut parts = s.split(':');
319 let (Some(h), Some(len), Some(mt), None) = (parts.next(), parts.next(), parts.next(), parts.next()) else {
320 return Err("a blob is <sha256>:<length>:<media type>".into());
321 };
322 let hash = match decode_hex_lower::<32>(h) {
323 Some(x) => x,
324 None => return Err("the hash is 64 lowercase hex digits".into()),
325 };
326 let lb = len.as_bytes();
327 if lb.is_empty() || !lb.iter().all(u8::is_ascii_digit) || (lb.len() > 1 && lb[0] == b'0') || lb.len() > 16 {
328 return Err("the length is a canonical non-negative integer".into());
329 }
330 let n: u64 = len.parse().map_err(|_| String::from("bad length"))?;
331 if n > limits::BLOB_LENGTH {
332 return Err("length above 9007199254740991".into());
333 }
334 let ok_mt = match mt.split_once('/') {
335 Some((t, st)) => is_restricted_name(t) && is_restricted_name(st),
336 None => false,
337 };
338 if !ok_mt {
339 return Err(format!("not a lowercase type/subtype media type: {mt:?}"));
340 }
341 if mt.len() > limits::MEDIA_TYPE {
342 return Err("media type longer than 128 characters".into());
343 }
344 Ok((hash, n, mt))
345}
346
347pub fn check_blob(s: &str, path: &str) -> Result<()> {
349 match parse_blob(s) {
350 Ok(_) => Ok(()),
351 Err(d) => err(ErrorCode::CanonBlob, path, d),
352 }
353}
354
355pub(crate) fn decode_hex_lower<const N: usize>(s: &str) -> Option<[u8; N]> {
357 let b = s.as_bytes();
358 if b.len() != 2 * N {
359 return None;
360 }
361 let mut out = [0u8; N];
362 for (i, o) in out.iter_mut().enumerate() {
363 let hi = hexval(b[2 * i])?;
364 let lo = hexval(b[2 * i + 1])?;
365 *o = (hi << 4) | lo;
366 }
367 Some(out)
368}
369
370fn hexval(c: u8) -> Option<u8> {
371 match c {
372 b'0'..=b'9' => Some(c - b'0'),
373 b'a'..=b'f' => Some(c - b'a' + 10),
374 _ => None,
375 }
376}
377
378pub fn to_hex(bytes: &[u8]) -> String {
380 const HEX: &[u8; 16] = b"0123456789abcdef";
381 let mut s = String::with_capacity(bytes.len() * 2);
382 for &b in bytes {
383 s.push(char::from(HEX[usize::from(b >> 4)]));
384 s.push(char::from(HEX[usize::from(b & 0xF)]));
385 }
386 s
387}
388
389pub fn is_assigned_15_1(c: char) -> bool {
391 let cp = c as u32;
392 tables::ASSIGNED
393 .binary_search_by(|&(lo, hi)| {
394 if hi < cp {
395 core::cmp::Ordering::Less
396 } else if lo > cp {
397 core::cmp::Ordering::Greater
398 } else {
399 core::cmp::Ordering::Equal
400 }
401 })
402 .is_ok()
403}
404
405pub(crate) fn check_text(s: &str, limit: usize, allow_tab_lf: bool, path: &str) -> Result<()> {
408 for c in s.chars() {
409 let cp = c as u32;
410 if (cp < 0x20 && !(allow_tab_lf && (c == '\t' || c == '\n'))) || (0x7F..=0x9F).contains(&cp) {
411 return err(ErrorCode::CanonString, path, format!("control character U+{cp:04X}"));
412 }
413 if !is_assigned_15_1(c) {
414 return err(ErrorCode::CanonString, path, format!("U+{cp:04X} is not assigned in Unicode 15.1"));
415 }
416 }
417 if !unicode_normalization::is_nfc(s) {
418 return err(ErrorCode::CanonString, path, "not in Normalization Form C");
419 }
420 if s.len() > limit {
421 return err(ErrorCode::CanonLimit, path, format!("longer than {limit} UTF-8 bytes"));
422 }
423 Ok(())
424}
425
426pub fn check_string(s: &str, path: &str) -> Result<()> {
428 check_text(s, limits::STRING, false, path)
429}
430
431pub fn check_licence(s: &str, path: &str) -> Result<()> {
433 if let Some(rest) = s.strip_prefix("https://") {
434 if rest.is_empty() || !rest.bytes().all(|c| (0x21..=0x7E).contains(&c)) {
435 return err(ErrorCode::CanonLicence, path, "a licence URL is https:// and printable ASCII");
436 }
437 if s.len() > limits::LICENCE_URL {
438 return err(ErrorCode::CanonLimit, path, "licence URL longer than 512 characters");
439 }
440 return Ok(());
441 }
442 let b = s.as_bytes();
443 let ok = !b.is_empty()
444 && b[0].is_ascii_alphanumeric()
445 && b.iter().all(|&c| c.is_ascii_alphanumeric() || c == b'.' || c == b'+' || c == b'-');
446 if !ok {
447 return err(ErrorCode::CanonLicence, path, format!("not an SPDX identifier or https URL: {s:?}"));
448 }
449 if s.len() > limits::LICENCE_SPDX {
450 return err(ErrorCode::CanonLimit, path, "licence identifier longer than 64 characters");
451 }
452 Ok(())
453}
454
455pub fn is_currency(s: &str) -> bool {
457 tables::CURRENCIES.contains(&s)
458}
459
460pub fn is_index_unit(s: &str) -> bool {
462 if s.len() > limits::INDEX_UNIT {
463 return false;
464 }
465 let Some(rest) = s.strip_prefix("index:") else { return false };
466 let Some((period, value)) = rest.split_once('=') else { return false };
467 let period_ok = match period.len() {
468 4 => is_date(&format!("{period}-01-01")),
469 7 => is_date(&format!("{period}-01")),
470 10 => is_date(period),
471 _ => false,
472 };
473 let shape_ok =
475 period.bytes().enumerate().all(|(i, c)| if i == 4 || i == 7 { c == b'-' } else { c.is_ascii_digit() });
476 period_ok && shape_ok && is_number_grammar(value) && !value.starts_with('-') && value != "0"
477}
478
479pub fn is_literal_unit(s: &str) -> bool {
481 is_currency(s) || tables::PHYSICAL.contains(&s) || tables::RATIOS.contains(&s) || is_index_unit(s)
482}
483
484#[cfg(test)]
485mod tests {
486 use super::*;
487
488 #[test]
489 fn names() {
490 for ok in ["fx_rate", "base", "a01", "v2_final"] {
491 assert!(is_name_grammar(ok), "{ok}");
492 }
493 for bad in ["FxRate", "_x", "x_", "a__b", "1st", ""] {
494 assert!(!is_name_grammar(bad), "{bad}");
495 }
496 }
497
498 #[test]
499 fn numbers() {
500 for ok in ["0", "-1", "1.5", "0.001", "10"] {
501 assert!(is_number_grammar(ok), "{ok}");
502 }
503 for bad in ["-0", "01", "1.", ".5", "1.50", "+1", "1e3", "", "-", "0.0"] {
504 assert!(!is_number_grammar(bad), "{bad}");
505 }
506 }
507
508 #[test]
509 fn index_units() {
510 assert!(is_index_unit("index:2015=100"));
511 assert!(is_index_unit("index:1984-01-03=1000"));
512 assert!(is_index_unit("index:2016-02-29=0.5"));
513 for bad in [
514 "index:0000=1",
515 "index:2015=0",
516 "index:2015=-1",
517 "index:2015-13=1",
518 "index:2015-02-29=1",
519 "index:2015=1.0",
520 "index:15=1",
521 ] {
522 assert!(!is_index_unit(bad), "{bad}");
523 }
524 }
525
526 #[test]
527 fn instants() {
528 assert!(is_instant("2024-03-15T16:00:00.000000Z"));
529 assert!(!is_instant("2024-03-15T24:00:00.000000Z"));
530 assert!(!is_instant("2024-03-15T23:59:60.000000Z"));
531 assert!(!is_instant("2023-02-29T00:00:00.000000Z"));
532 }
533}