|
10 | 10 | //! scalar-byte budgets proportional to the input length instead: an alias-free document |
11 | 11 | //! cannot have more nodes than bytes, so the cap still bounds alias |
12 | 12 | //! expansion while accepting any report the caller's read limit allows. |
| 13 | +//! |
| 14 | +//! KYAML (the estate's YAML dialect, D280/D307) is a strict subset of |
| 15 | +//! YAML, so `.kyaml` files go through the same parser. |
13 | 16 |
|
14 | 17 | use serde::de::DeserializeOwned; |
15 | 18 | use serde::Serialize; |
16 | 19 | use serde_saphyr::budget::Budget; |
17 | 20 | use serde_saphyr::options::Options; |
| 21 | +use std::path::Path; |
| 22 | + |
| 23 | +/// File extensions read as YAML. `kyaml` is the estate extension for |
| 24 | +/// KYAML-dialect configuration. |
| 25 | +pub const EXTENSIONS: [&str; 3] = ["yaml", "yml", "kyaml"]; |
| 26 | + |
| 27 | +/// Whether `ext` (without the dot) names a YAML or KYAML file. |
| 28 | +pub fn is_extension(ext: &str) -> bool { |
| 29 | + EXTENSIONS.contains(&ext) |
| 30 | +} |
| 31 | + |
| 32 | +/// Whether `path` has a YAML or KYAML extension. |
| 33 | +pub fn has_extension(path: &Path) -> bool { |
| 34 | + path.extension() |
| 35 | + .and_then(|ext| ext.to_str()) |
| 36 | + .is_some_and(is_extension) |
| 37 | +} |
18 | 38 |
|
19 | 39 | /// Parse a hand-written YAML config with the default resource budget. |
20 | 40 | pub fn from_str<T: DeserializeOwned>(input: &str) -> Result<T, serde_saphyr::Error> { |
@@ -42,6 +62,21 @@ pub fn to_string<T: Serialize>(value: &T) -> Result<String, serde_saphyr::ser::E |
42 | 62 | mod tests { |
43 | 63 | use super::*; |
44 | 64 |
|
| 65 | + #[test] |
| 66 | + fn yaml_extensions_include_kyaml() { |
| 67 | + for ext in ["yaml", "yml", "kyaml"] { |
| 68 | + assert!(is_extension(ext), "{ext}"); |
| 69 | + assert!( |
| 70 | + has_extension(Path::new(&format!("dir/file.{ext}"))), |
| 71 | + "{ext}" |
| 72 | + ); |
| 73 | + } |
| 74 | + for ext in ["json", "YAML", "kyml", "ncl"] { |
| 75 | + assert!(!is_extension(ext), "{ext}"); |
| 76 | + } |
| 77 | + assert!(!has_extension(Path::new("kyaml"))); |
| 78 | + } |
| 79 | + |
45 | 80 | #[test] |
46 | 81 | fn report_budget_accepts_documents_past_the_default_node_cap() { |
47 | 82 | let items: Vec<u8> = vec![0; 300_000]; |
|
0 commit comments