use crate::core::parse_core_with_positions;
use crate::errors::{LimaDiagnosticCode as Code, LimaError};
use crate::normalize::NESTING_DEPTH_LIMIT;
use crate::scalars::SCALAR_LENGTH_LIMIT;
use crate::value::{set_mapping, InsertedAt, LimaValue, PositionedValue};
pub const PARTIAL_KEY_LENGTH_LIMIT: usize = 128;
pub const PARTIAL_VALUE_DEPTH_LIMIT: u32 = 16;
pub const PARTIAL_COUNT_LIMIT: usize = 128;
pub const PARTIAL_NAME_LENGTH_LIMIT: usize = 128;
pub const PARTIAL_NODE_LIMIT: usize = 4096;
pub const RESULT_NODE_LIMIT: usize = 65536;
fn canonical_string(v: &LimaValue) -> String {
match v {
LimaValue::Null => String::new(),
LimaValue::Bool(b) => if *b { "true" } else { "false" }.to_string(),
LimaValue::Instant(i) => i.to_iso_string(),
LimaValue::String(s) => s.clone(),
LimaValue::Int(n) => n.to_string(),
LimaValue::Float(n) => format_canonical_float(*n),
_ => String::new(),
}
}
fn format_canonical_float(n: f64) -> String {
let abs = n.abs();
if abs != 0.0 && !(1e-6..1e21).contains(&abs) {
format!("{n:e}")
} else {
format!("{n}")
}
}
fn is_doc_segment_start(c: u8) -> bool {
c.is_ascii_alphanumeric() || c == b'_'
}
fn is_doc_segment_cont(c: u8) -> bool {
c.is_ascii_alphanumeric() || c == b'_' || c == b':' || c == b'-'
}
fn is_partial_key_cont(c: u8) -> bool {
c.is_ascii_alphanumeric() || c == b'_' || c == b':' || c == b'/' || c == b'-'
}
fn match_doc_path(s: &[u8]) -> Option<usize> {
if s.is_empty() || !is_doc_segment_start(s[0]) {
return None;
}
let mut i = 1;
while i < s.len() && is_doc_segment_cont(s[i]) {
i += 1;
}
while i < s.len() && s[i] == b'.' && s.get(i + 1).is_some_and(|&c| is_doc_segment_start(c)) {
i += 2;
while i < s.len() && is_doc_segment_cont(s[i]) {
i += 1;
}
}
Some(i)
}
fn match_partial_key(s: &[u8]) -> Option<usize> {
if s.is_empty() || !is_doc_segment_start(s[0]) {
return None;
}
let mut i = 1;
while i < s.len() && is_partial_key_cont(s[i]) {
i += 1;
}
Some(i)
}
struct RefMatch {
start: usize,
end: usize,
is_partial: bool,
key: String,
}
fn try_match_ref_at(bytes: &[u8], start: usize) -> Option<RefMatch> {
if bytes.get(start) != Some(&b'(') {
return None;
}
let sigil = *bytes.get(start + 1)?;
let is_partial = match sigil {
b'$' => false,
b'%' => true,
_ => return None,
};
let body_start = start + 2;
let body = &bytes[body_start..];
let len = if is_partial {
match_partial_key(body)
} else {
match_doc_path(body)
}?;
let close = body_start + len;
if bytes.get(close) != Some(&b')') {
return None;
}
Some(RefMatch {
start,
end: close + 1,
is_partial,
key: std::str::from_utf8(&bytes[body_start..close])
.unwrap()
.to_string(),
})
}
fn as_pure_ref(s: &str) -> Option<(bool, String)> {
let m = try_match_ref_at(s.as_bytes(), 0)?;
if m.end == s.len() {
Some((m.is_partial, m.key))
} else {
None
}
}
fn interpolate(s: &str, mut replace_one: impl FnMut(bool, &str, &str) -> String) -> String {
let bytes = s.as_bytes();
let mut out = String::with_capacity(s.len());
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'(' {
if let Some(m) = try_match_ref_at(bytes, i) {
let matched = &s[m.start..m.end];
out.push_str(&replace_one(m.is_partial, &m.key, matched));
i = m.end;
continue;
}
}
let ch = s[i..].chars().next().unwrap();
out.push(ch);
i += ch.len_utf8();
}
out
}
fn get_nested_value_p<'a>(
root: &'a [(String, PositionedValue)],
path: &str,
) -> Option<&'a PositionedValue> {
let mut parts = path.split('.');
let first = parts.next()?;
let mut cur = &root.iter().find(|(k, _)| k == first)?.1;
for part in parts {
let PositionedValue::Mapping { entries, .. } = cur else {
return None;
};
cur = &entries.iter().find(|(k, _)| k == part)?.1;
}
Some(cur)
}
fn is_reference_free_p(v: &PositionedValue) -> bool {
match v {
PositionedValue::String { value, quoted, .. } => {
*quoted || (!value.contains("($") && !value.contains("(%"))
}
PositionedValue::Array { items, .. } => items.iter().all(is_reference_free_p),
PositionedValue::Mapping { entries, .. } => {
entries.iter().all(|(_, c)| is_reference_free_p(c))
}
_ => true,
}
}
fn partial_to_positioned(v: &LimaValue, line: u32) -> PositionedValue {
match v {
LimaValue::Null => PositionedValue::Null {
line,
inserted_at: None,
},
LimaValue::Bool(b) => PositionedValue::Bool {
value: *b,
line,
inserted_at: None,
},
LimaValue::Int(n) => PositionedValue::Int {
value: *n,
line,
inserted_at: None,
},
LimaValue::Float(n) => PositionedValue::Float {
value: *n,
line,
inserted_at: None,
},
LimaValue::String(s) => PositionedValue::String {
value: s.clone(),
line,
quoted: true,
source_spans: None,
inserted_at: None,
},
LimaValue::Instant(i) => PositionedValue::Instant {
value: *i,
line,
inserted_at: None,
},
LimaValue::Array(items) => PositionedValue::Array {
items: items
.iter()
.map(|i| partial_to_positioned(i, line))
.collect(),
line,
inserted_at: None,
},
LimaValue::Mapping(entries) => PositionedValue::Mapping {
entries: entries
.iter()
.map(|(k, c)| (k.clone(), partial_to_positioned(c, line)))
.collect(),
line,
inserted_at: None,
},
}
}
fn validate_partial_value(
v: &LimaValue,
partial_name: &str,
path: &str,
depth: u32,
) -> Result<(), LimaError> {
let invalid = |reason: String| -> LimaError {
LimaError {
code: Code::InvalidPartial,
line: None,
key: None,
token: None,
partial: None,
path: None,
column: None,
message: format!(
"Lima: invalid partial \"{partial_name}\" at path \"{path}\": {reason}"
),
}
};
match v {
LimaValue::Float(n) if !n.is_finite() => Err(invalid("non-finite number".to_string())),
LimaValue::String(s) if s.chars().count() > SCALAR_LENGTH_LIMIT => Err(invalid(format!(
"string exceeds maximum length of {SCALAR_LENGTH_LIMIT} code points"
))),
LimaValue::Instant(i) => {
let (year, _, _) = crate::value::civil_from_days(i.epoch_seconds.div_euclid(86_400));
if !(1..=9999).contains(&year) {
Err(invalid(format!(
"date year {year} outside the range 0001-9999"
)))
} else {
Ok(())
}
}
LimaValue::Array(items) => {
if depth >= PARTIAL_VALUE_DEPTH_LIMIT {
return Err(invalid(format!(
"nesting depth exceeds maximum of {PARTIAL_VALUE_DEPTH_LIMIT}"
)));
}
for (i, item) in items.iter().enumerate() {
if matches!(item, LimaValue::Array(_)) {
return Err(LimaError {
code: Code::InvalidPartial,
line: None,
key: None,
token: None, partial: None, path: None, column: None,
message: format!(
"Lima: invalid partial \"{partial_name}\" at path \"{path}[{i}]\": nested arrays are not supported"
),
});
}
validate_partial_value(item, partial_name, &format!("{path}[{i}]"), depth + 1)?;
}
Ok(())
}
LimaValue::Mapping(entries) => {
if depth >= PARTIAL_VALUE_DEPTH_LIMIT {
return Err(invalid(format!(
"nesting depth exceeds maximum of {PARTIAL_VALUE_DEPTH_LIMIT}"
)));
}
for (k, c) in entries {
if k.chars().count() > PARTIAL_KEY_LENGTH_LIMIT {
return Err(LimaError {
code: Code::InvalidPartial,
line: None,
key: None,
token: None, partial: None, path: None, column: None,
message: format!(
"Lima: invalid partial \"{partial_name}\" at path \"{path}.{k}\": key exceeds maximum length of {PARTIAL_KEY_LENGTH_LIMIT} code points"
),
});
}
validate_partial_value(c, partial_name, &format!("{path}.{k}"), depth + 1)?;
}
Ok(())
}
_ => Ok(()),
}
}
struct ResolutionContext {
best: Option<LimaError>,
hard_error: Option<LimaError>,
}
impl ResolutionContext {
fn report(&mut self, err: LimaError) {
let line = err.line.unwrap_or(0);
if self
.best
.as_ref()
.is_none_or(|b| line < b.line.unwrap_or(0))
{
self.best = Some(err);
}
}
}
fn resolve_tree(
node: &PositionedValue,
lookup: &[(String, PositionedValue)],
partials: &[(String, PositionedValue)],
ctx: &mut ResolutionContext,
) -> PositionedValue {
if !matches!(node, PositionedValue::String { .. }) && is_reference_free_p(node) {
return node.clone();
}
if let PositionedValue::String {
value: val,
line,
quoted,
..
} = node
{
if *quoted {
return node.clone();
}
let line = *line;
if let Some((is_partial, key)) = as_pure_ref(val) {
let inserted_at = InsertedAt {
line,
offset: 0,
token: val.clone(),
};
if is_partial {
if let Some(target) = partials.iter().find(|(k, _)| k == &key) {
return with_inserted_at(target.1.clone(), inserted_at);
}
} else if let Some(target) = get_nested_value_p(lookup, &key) {
if is_reference_free_p(target) {
return with_inserted_at(target.clone(), inserted_at);
}
}
}
if val.contains("($") || val.contains("(%") {
let replaced = interpolate(val, |is_partial, key, matched| {
let target = if is_partial {
partials.iter().find(|(k, _)| k == key).map(|(_, v)| v)
} else {
get_nested_value_p(lookup, key).filter(|t| is_reference_free_p(t))
};
let Some(target) = target else {
return matched.to_string();
};
match target {
PositionedValue::Mapping { .. } => {
ctx.report(LimaError::new(
Code::InvalidInterpolation, line,
format!("Lima: invalid interpolation of \"{matched}\" at line {line}: mapping cannot be interpolated into a string"),
));
matched.to_string()
}
PositionedValue::Array { items, .. } => {
if items.iter().any(|i| {
matches!(
i,
PositionedValue::Array { .. } | PositionedValue::Mapping { .. }
)
}) {
ctx.report(LimaError::new(
Code::InvalidInterpolation, line,
format!("Lima: invalid interpolation of \"{matched}\" at line {line}: array contains a nested array or mapping"),
));
return matched.to_string();
}
items
.iter()
.map(|i| canonical_string(&i.to_plain_value()))
.collect::<Vec<_>>()
.join(", ")
}
other => canonical_string(&other.to_plain_value()),
}
});
if replaced.chars().count() > SCALAR_LENGTH_LIMIT && ctx.hard_error.is_none() {
ctx.hard_error = Some(LimaError::new(
Code::ResourceLimit, line,
format!("Lima: scalar exceeds maximum length of {SCALAR_LENGTH_LIMIT} code points at line {line}"),
));
}
return PositionedValue::String {
value: replaced,
line,
quoted: false,
source_spans: None,
inserted_at: None,
};
}
return node.clone();
}
match node {
PositionedValue::Array { items, line, inserted_at } => PositionedValue::Array {
line: *line,
inserted_at: inserted_at.clone(),
items: items
.iter()
.map(|item| {
let resolved = resolve_tree(item, lookup, partials, ctx);
if matches!(item, PositionedValue::String { .. }) && matches!(resolved, PositionedValue::Array { .. }) {
if let PositionedValue::String { value, line, .. } = item {
ctx.report(LimaError::new(
Code::InvalidReferenceShape, *line,
format!("Lima: reference \"{value}\" resolves to an array, which cannot be inserted as a sequence item at line {line}"),
));
}
item.clone()
} else {
resolved
}
})
.collect(),
},
PositionedValue::Mapping { entries, line, inserted_at } => PositionedValue::Mapping {
line: *line,
inserted_at: inserted_at.clone(),
entries: entries.iter().map(|(k, c)| (k.clone(), resolve_tree(c, lookup, partials, ctx))).collect(),
},
other => other.clone(), }
}
fn with_inserted_at(mut v: PositionedValue, inserted_at: InsertedAt) -> PositionedValue {
match &mut v {
PositionedValue::Null {
inserted_at: ia, ..
}
| PositionedValue::Bool {
inserted_at: ia, ..
}
| PositionedValue::Int {
inserted_at: ia, ..
}
| PositionedValue::Float {
inserted_at: ia, ..
}
| PositionedValue::String {
inserted_at: ia, ..
}
| PositionedValue::Instant {
inserted_at: ia, ..
}
| PositionedValue::Array {
inserted_at: ia, ..
}
| PositionedValue::Mapping {
inserted_at: ia, ..
} => *ia = Some(inserted_at),
}
v
}
struct FinalizedValue {
native: LimaValue,
node_count: usize,
depth: u32,
deepest_participants: Vec<InsertedAt>,
}
fn finalize_positioned(v: &PositionedValue) -> FinalizedValue {
let own: Vec<InsertedAt> = v.inserted_at().cloned().into_iter().collect();
match v {
PositionedValue::Array { items, .. } => {
if items.is_empty() {
return FinalizedValue {
native: LimaValue::Array(vec![]),
node_count: 1,
depth: 1,
deepest_participants: own,
};
}
let mut native = Vec::with_capacity(items.len());
let mut node_count = 1;
let mut max_depth: i64 = -1;
let mut child_participants: Vec<InsertedAt> = Vec::new();
for item in items {
let r = finalize_positioned(item);
native.push(r.native);
node_count += r.node_count;
match (r.depth as i64).cmp(&max_depth) {
std::cmp::Ordering::Greater => {
max_depth = r.depth as i64;
child_participants = r.deepest_participants;
}
std::cmp::Ordering::Equal => child_participants.extend(r.deepest_participants),
std::cmp::Ordering::Less => {}
}
}
let mut participants = own;
participants.extend(child_participants);
FinalizedValue {
native: LimaValue::Array(native),
node_count,
depth: (max_depth + 1) as u32,
deepest_participants: participants,
}
}
PositionedValue::Mapping { entries, .. } => {
if entries.is_empty() {
return FinalizedValue {
native: LimaValue::Mapping(vec![]),
node_count: 1,
depth: 1,
deepest_participants: own,
};
}
let mut native = Vec::with_capacity(entries.len());
let mut node_count = 1;
let mut max_depth: i64 = -1;
let mut child_participants: Vec<InsertedAt> = Vec::new();
for (k, c) in entries {
let r = finalize_positioned(c);
native.push((k.clone(), r.native));
node_count += r.node_count;
match (r.depth as i64).cmp(&max_depth) {
std::cmp::Ordering::Greater => {
max_depth = r.depth as i64;
child_participants = r.deepest_participants;
}
std::cmp::Ordering::Equal => child_participants.extend(r.deepest_participants),
std::cmp::Ordering::Less => {}
}
}
let mut participants = own;
participants.extend(child_participants);
FinalizedValue {
native: LimaValue::Mapping(native),
node_count,
depth: (max_depth + 1) as u32,
deepest_participants: participants,
}
}
_ => FinalizedValue {
native: v.to_plain_value(),
node_count: 1,
depth: 0,
deepest_participants: own,
},
}
}
fn earliest_participant(participants: &[InsertedAt]) -> Option<&InsertedAt> {
participants.iter().min_by_key(|p| p.line)
}
fn collect_all_participants(v: &PositionedValue, acc: &mut Vec<InsertedAt>) {
if let Some(ia) = v.inserted_at() {
acc.push(ia.clone());
}
match v {
PositionedValue::Array { items, .. } => {
items.iter().for_each(|i| collect_all_participants(i, acc))
}
PositionedValue::Mapping { entries, .. } => entries
.iter()
.for_each(|(_, c)| collect_all_participants(c, acc)),
_ => {}
}
}
#[derive(Default)]
pub struct ReferencesOptions {
pub partials: Vec<(String, LimaValue)>,
pub strict: bool,
}
pub fn parse_references(
front_matter: &str,
options: ReferencesOptions,
) -> Result<LimaValue, LimaError> {
let strict = options.strict;
if options.partials.len() > PARTIAL_COUNT_LIMIT {
return Err(LimaError {
code: Code::InvalidPartial,
line: None,
key: None,
token: None,
partial: None,
path: None,
column: None,
message: format!("Lima: too many partials (max {PARTIAL_COUNT_LIMIT})"),
});
}
for (name, _) in &options.partials {
if name.chars().count() > PARTIAL_NAME_LENGTH_LIMIT {
return Err(LimaError {
code: Code::InvalidPartial,
line: None,
key: None,
token: None, partial: None, path: None, column: None,
message: format!(
"Lima: invalid partial \"{name}\" at path \"{name}\": name exceeds maximum length of {PARTIAL_NAME_LENGTH_LIMIT} code points"
),
});
}
}
for (name, value) in &options.partials {
validate_partial_value(value, name, name, 0)?;
}
let total_partial_nodes: usize = options
.partials
.iter()
.map(|(_, v)| count_lima_nodes(v))
.sum();
if total_partial_nodes > PARTIAL_NODE_LIMIT {
return Err(LimaError {
code: Code::InvalidPartial,
line: None,
key: None,
token: None,
partial: None,
path: None,
column: None,
message: format!(
"Lima: partials exceed the combined maximum of {PARTIAL_NODE_LIMIT} value nodes"
),
});
}
let partials_positioned: Vec<(String, PositionedValue)> = options
.partials
.iter()
.map(|(name, v)| (name.clone(), partial_to_positioned(v, 0)))
.collect();
let root = parse_core_with_positions(front_matter, strict)?;
let mut ctx = ResolutionContext {
best: None,
hard_error: None,
};
let has_refs = front_matter.contains("($") || front_matter.contains("(%");
let final_map: Vec<(String, PositionedValue)> = if !has_refs {
root.clone()
} else {
let mut phase1_live: Vec<(String, PositionedValue)> = Vec::new();
let mut original_pure_ref_text: Vec<(String, String)> = Vec::new();
for (key, node) in &root {
if let PositionedValue::String {
value,
quoted: false,
..
} = node
{
if as_pure_ref(value).is_some() {
original_pure_ref_text.push((key.clone(), value.clone()));
}
}
let resolved = resolve_tree(node, &phase1_live, &partials_positioned, &mut ctx);
set_mapping(&mut phase1_live, key.clone(), resolved);
}
let phase1_snapshot: Vec<(String, PositionedValue)> = phase1_live
.iter()
.map(|(key, value)| {
if let Some((_, text)) = original_pure_ref_text.iter().find(|(k, _)| k == key) {
let line = root.iter().find(|(k, _)| k == key).unwrap().1.line();
(
key.clone(),
PositionedValue::String {
value: text.clone(),
line,
quoted: false,
source_spans: None,
inserted_at: None,
},
)
} else {
(key.clone(), value.clone())
}
})
.collect();
root.iter()
.map(|(key, _)| {
let live_value = &phase1_live.iter().find(|(k, _)| k == key).unwrap().1;
(
key.clone(),
resolve_tree(live_value, &phase1_snapshot, &partials_positioned, &mut ctx),
)
})
.collect()
};
if let Some(e) = ctx.hard_error {
return Err(e);
}
if strict {
fn scan_unresolved(v: &PositionedValue, ctx: &mut ResolutionContext) {
match v {
PositionedValue::String {
value,
quoted: false,
line,
..
} => {
if value.contains("($") || value.contains("(%") {
if let Some(m) =
try_match_ref_at(value.as_bytes(), value.find('(').unwrap_or(0))
{
let token = &value[m.start..m.end];
ctx.report(LimaError::new(
Code::UnresolvedReference,
*line,
format!("Lima: unresolved reference \"{token}\" at line {line}"),
));
} else if let Some(paren) = value.find('(') {
if let Some(m) = try_match_ref_at(value.as_bytes(), paren) {
let token = &value[m.start..m.end];
ctx.report(LimaError::new(
Code::UnresolvedReference,
*line,
format!(
"Lima: unresolved reference \"{token}\" at line {line}"
),
));
}
}
}
}
PositionedValue::Array { items, .. } => {
items.iter().for_each(|i| scan_unresolved(i, ctx))
}
PositionedValue::Mapping { entries, .. } => {
entries.iter().for_each(|(_, c)| scan_unresolved(c, ctx))
}
_ => {}
}
}
for (_, v) in &final_map {
scan_unresolved(v, &mut ctx);
}
}
if let Some(e) = ctx.best {
return Err(e);
}
let final_results: Vec<(String, FinalizedValue)> = final_map
.iter()
.map(|(k, v)| (k.clone(), finalize_positioned(v)))
.collect();
let depth = final_results
.iter()
.map(|(_, r)| r.depth)
.max()
.unwrap_or(0);
if depth > NESTING_DEPTH_LIMIT as u32 {
let mut participants = Vec::new();
for (_, r) in final_results.iter().filter(|(_, r)| r.depth == depth) {
participants.extend(r.deepest_participants.iter().cloned());
}
let winner = earliest_participant(&participants);
return Err(LimaError {
code: Code::ResourceLimit,
line: Some(winner.map(|w| w.line).unwrap_or(1)),
key: None,
token: None, partial: None, path: None, column: None,
message: match winner {
Some(w) => format!("Lima: nesting depth exceeds maximum of {NESTING_DEPTH_LIMIT} at line {}: \"{}\"", w.line, w.token),
None => format!("Lima: nesting depth exceeds maximum of {NESTING_DEPTH_LIMIT} at line 1"),
},
});
}
let mut total_result_nodes = 1; for (_, r) in &final_results {
total_result_nodes += r.node_count;
}
if total_result_nodes > RESULT_NODE_LIMIT {
let mut participants = Vec::new();
for (_, v) in &final_map {
collect_all_participants(v, &mut participants);
}
let winner = earliest_participant(&participants);
return Err(LimaError {
code: Code::ResourceLimit,
line: Some(winner.map(|w| w.line).unwrap_or(1)),
key: None,
token: None, partial: None, path: None, column: None,
message: match winner {
Some(w) => format!("Lima: result exceeds maximum size of {RESULT_NODE_LIMIT} total nodes at line {}: \"{}\"", w.line, w.token),
None => format!("Lima: result exceeds maximum size of {RESULT_NODE_LIMIT} total nodes at line 1"),
},
});
}
Ok(LimaValue::Mapping(
final_results
.into_iter()
.map(|(k, r)| (k, r.native))
.collect(),
))
}
fn count_lima_nodes(v: &LimaValue) -> usize {
match v {
LimaValue::Array(items) => 1 + items.iter().map(count_lima_nodes).sum::<usize>(),
LimaValue::Mapping(entries) => {
1 + entries
.iter()
.map(|(_, c)| count_lima_nodes(c))
.sum::<usize>()
}
_ => 1,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn get<'a>(v: &'a LimaValue, key: &str) -> &'a LimaValue {
let LimaValue::Mapping(entries) = v else {
panic!("expected mapping")
};
&entries
.iter()
.find(|(k, _)| k == key)
.unwrap_or_else(|| panic!("missing key {key:?}"))
.1
}
fn parse(input: &str) -> LimaValue {
parse_references(input, ReferencesOptions::default()).unwrap()
}
#[test]
fn document_without_references_passes_through() {
let v = parse("title: Hello\ncount: 3\n");
assert_eq!(get(&v, "title"), &LimaValue::String("Hello".into()));
assert_eq!(get(&v, "count"), &LimaValue::Int(3));
}
#[test]
fn backward_document_reference() {
let v = parse("a: 1\nb: ($a)\n");
assert_eq!(get(&v, "a"), &LimaValue::Int(1));
assert_eq!(get(&v, "b"), &LimaValue::Int(1));
}
#[test]
fn forward_document_reference() {
let v = parse("a: ($b)\nb: 2\n");
assert_eq!(get(&v, "a"), &LimaValue::Int(2));
assert_eq!(get(&v, "b"), &LimaValue::Int(2));
}
#[test]
fn nested_path_reference() {
let v = parse("author:\n name: Alice\ncredit: ($author.name)\n");
assert_eq!(get(&v, "credit"), &LimaValue::String("Alice".into()));
}
#[test]
fn string_interpolation() {
let v = parse("name: Alice\ngreeting: Hello, ($name)!\n");
assert_eq!(
get(&v, "greeting"),
&LimaValue::String("Hello, Alice!".into())
);
}
#[test]
fn quoted_strings_are_never_reference_sites() {
let v = parse("a: 1\nb: \"($a)\"\n");
assert_eq!(get(&v, "b"), &LimaValue::String("($a)".into()));
}
#[test]
fn unresolved_reference_strict_throws() {
let err = parse_references(
"a: ($missing)",
ReferencesOptions {
strict: true,
..Default::default()
},
);
assert!(err.is_err());
}
#[test]
fn unresolved_reference_non_strict_stays_literal() {
let v = parse("a: ($missing)\n");
assert_eq!(get(&v, "a"), &LimaValue::String("($missing)".into()));
}
#[test]
fn partial_reference() {
let v = parse_references(
"a: (%greeting)",
ReferencesOptions {
partials: vec![("greeting".to_string(), LimaValue::String("hi".into()))],
strict: false,
},
)
.unwrap();
assert_eq!(get(&v, "a"), &LimaValue::String("hi".into()));
}
#[test]
fn array_reference_into_sequence_item_throws() {
let err = parse_references(
"list:\n values: [1, 2]\nitems:\n - ($list.values)\n",
ReferencesOptions {
strict: true,
..Default::default()
},
);
assert!(err.is_err());
}
}
#[cfg(test)]
mod corpus_tests {
use super::*;
use crate::errors::LimaDiagnosticCode;
use crate::scalars::parse_date_utc;
use crate::value::{days_from_civil, Instant};
use serde_json::Value as Json;
use std::{fs, path::PathBuf};
fn corpus_dir() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../corpus/references")
}
fn code_name(code: LimaDiagnosticCode) -> &'static str {
use LimaDiagnosticCode::*;
match code {
InvalidEscape => "INVALID_ESCAPE",
InvalidQuote => "INVALID_QUOTE",
InvalidDate => "INVALID_DATE",
InvalidNumber => "INVALID_NUMBER",
InvalidReferenceShape => "INVALID_REFERENCE_SHAPE",
InvalidIndentation => "INVALID_INDENTATION",
InvalidFlowSyntax => "INVALID_FLOW_SYNTAX",
DuplicateKey => "DUPLICATE_KEY",
ResourceLimit => "RESOURCE_LIMIT",
UnresolvedReference => "UNRESOLVED_REFERENCE",
InvalidInterpolation => "INVALID_INTERPOLATION",
InvalidPartial => "INVALID_PARTIAL",
}
}
fn json_to_lima(v: &Json) -> Option<LimaValue> {
match v {
Json::Null => Some(LimaValue::Null),
Json::Bool(v) => Some(LimaValue::Bool(*v)),
Json::Number(v) => {
let number = v.as_f64()?;
if number.fract() == 0.0 && number.abs() <= 9_007_199_254_740_991.0 {
Some(LimaValue::Int(number as i64))
} else {
Some(LimaValue::Float(number))
}
}
Json::String(v) => Some(LimaValue::String(v.clone())),
Json::Array(values) => Some(LimaValue::Array(
values.iter().map(json_to_lima).collect::<Option<_>>()?,
)),
Json::Object(values) if !values.contains_key("$type") => Some(LimaValue::Mapping(
values
.iter()
.map(|(key, value)| Some((key.clone(), json_to_lima(value)?)))
.collect::<Option<_>>()?,
)),
Json::Object(value) => match (
value.get("$type").and_then(Json::as_str)?,
value.get("value").and_then(Json::as_str)?,
) {
("instant", text) => parse_date_utc(text, false, 0)
.ok()
.flatten()
.map(LimaValue::Instant),
("host-number", "nan") => Some(LimaValue::Float(f64::NAN)),
("host-number", "infinity") => Some(LimaValue::Float(f64::INFINITY)),
("host-number", "-infinity") => Some(LimaValue::Float(f64::NEG_INFINITY)),
("host-number", "-0") => Some(LimaValue::Float(-0.0)),
("host-date", "invalid" | "year-overflow") => Some(LimaValue::Instant(Instant {
epoch_seconds: days_from_civil(10_000, 1, 1) * 86_400,
})),
("host-date", "year-underflow") => Some(LimaValue::Instant(Instant {
epoch_seconds: days_from_civil(-1, 1, 1) * 86_400,
})),
_ => None,
},
}
}
fn value_matches(actual: &LimaValue, expected: &Json) -> bool {
match (actual, expected) {
(LimaValue::Null, Json::Null) => true,
(LimaValue::Bool(a), Json::Bool(e)) => a == e,
(LimaValue::Int(a), Json::Number(e)) => e.as_f64() == Some(*a as f64),
(LimaValue::Float(a), Json::Number(e)) => e.as_f64() == Some(*a),
(LimaValue::String(a), Json::String(e)) => a == e,
(LimaValue::Instant(a), Json::Object(e)) => {
e.get("$type").and_then(Json::as_str) == Some("instant")
&& e.get("value").and_then(Json::as_str) == Some(a.to_iso_string().as_str())
}
(LimaValue::Array(a), Json::Array(e)) => {
a.len() == e.len() && a.iter().zip(e).all(|(a, e)| value_matches(a, e))
}
(LimaValue::Mapping(a), Json::Object(e)) if !e.contains_key("$type") => {
a.len() == e.len()
&& e.iter().all(|(key, expected)| {
a.iter()
.find(|(actual, _)| actual == key)
.is_some_and(|(_, actual)| value_matches(actual, expected))
})
}
_ => false,
}
}
fn nested_mappings(params: &Json) -> Option<String> {
let depth = params["depth"].as_u64()?;
let key = params["key"].as_str().unwrap_or("k");
let leaf = params["leafValue"].as_str().unwrap_or("v");
let mut lines: Vec<String> = (0..depth)
.map(|level| format!("{}{key}:", " ".repeat(level as usize)))
.collect();
lines.push(format!("{}{key}: {leaf}", " ".repeat(depth as usize)));
Some(lines.join("\n"))
}
fn resolve_fixture(fixture: &Json) -> Option<(String, Vec<(String, LimaValue)>)> {
if let Some(input) = fixture["input"].as_str() {
let partials = if let Some(values) = fixture["options"]["partials"].as_object() {
values
.iter()
.map(|(key, value)| Some((key.clone(), json_to_lima(value)?)))
.collect::<Option<Vec<_>>>()?
} else {
Vec::new()
};
return Some((input.to_owned(), partials));
}
let generator = &fixture["generator"];
let params = &generator["parameters"];
match generator["name"].as_str()? {
"nested-mappings" => Some((nested_mappings(params)?, Vec::new())),
"partial-count" => {
let count = params["count"].as_u64()?;
let prefix = params["namePrefix"].as_str().unwrap_or("p");
Some((
String::new(),
(0..count)
.map(|i| (format!("{prefix}{i}"), LimaValue::String("v".into())))
.collect(),
))
}
"partial-node-tree" => {
let nodes = params["totalNodes"].as_u64()?;
let name = params["partialName"].as_str().unwrap_or("big");
Some((
String::new(),
vec![(
name.into(),
LimaValue::Array(vec![LimaValue::Int(1); (nodes - 1) as usize]),
)],
))
}
_ => None,
}
}
#[test]
fn matches_all_references_1_corpus_cases() {
let mut paths: Vec<_> = fs::read_dir(corpus_dir())
.unwrap()
.filter_map(Result::ok)
.map(|entry| entry.path())
.filter(|path| path.extension().and_then(|ext| ext.to_str()) == Some("json"))
.collect();
paths.sort();
assert_eq!(paths.len(), 101, "References 1.0 corpus count changed");
let mut passed = 0;
let mut skipped = 0;
let mut failures = Vec::new();
for path in paths {
let fixture: Json = serde_json::from_str(&fs::read_to_string(path).unwrap()).unwrap();
let id = fixture["id"].as_str().unwrap_or("<unknown>");
let Some((input, partials)) = resolve_fixture(&fixture) else {
skipped += 1;
continue;
};
let result = parse_references(
&input,
ReferencesOptions {
partials,
strict: fixture["options"]["strict"].as_bool().unwrap_or(false),
},
);
if let Some(expected) = fixture["expect"].get("result") {
match result {
Ok(actual) if value_matches(&actual, expected) => passed += 1,
Ok(actual) => failures.push(format!(
"{id}: mismatch: got {actual:?}, expected {expected:?}"
)),
Err(error) => failures.push(format!("{id}: unexpected error: {error}")),
}
} else if let Some(expected) = fixture["expect"]["error"].as_object() {
match result {
Err(error)
if expected.get("code").and_then(Json::as_str)
== Some(code_name(error.code)) =>
{
passed += 1;
}
Err(error) => failures.push(format!(
"{id}: got {}, expected {:?}",
code_name(error.code),
expected.get("code")
)),
Ok(_) => failures.push(format!("{id}: expected error, parsing succeeded")),
}
} else {
skipped += 1;
}
}
assert_eq!(skipped, 0, "References 1.0 corpus cases were skipped");
assert!(
failures.is_empty(),
"{} corpus failures:\n{}",
failures.len(),
failures.join("\n")
);
assert_eq!(passed, 101);
}
}