use crate::chars::is_trim_whitespace;
use crate::errors::{LimaDiagnosticCode as Code, LimaError, MULTILINE_VALUE_HINT};
use crate::key_hints::{specific_key_hint, KEY_GRAMMAR_HINT};
use crate::scalars::separator_index;
use crate::unicode_key::is_valid_unquoted;
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum RangeOwner {
None,
Inline,
Mapping,
Scalar,
}
pub(crate) fn check_range_lines(
source: &str,
start: usize,
end: usize,
line: u32,
owner: RangeOwner,
) -> Result<(), LimaError> {
let mut pos = start;
let mut current = line;
let mut base: Option<usize> = None;
let mut scalar_open = owner == RangeOwner::Scalar;
while pos < end {
let line_end = source[pos..end].find('\n').map(|p| pos + p);
let text = &source[pos..line_end.unwrap_or(end)];
let indent = text.bytes().take_while(|&b| b == b' ').count();
let belongs_to_scalar = scalar_open && (indent == text.len() || indent > 0);
if !belongs_to_scalar {
scalar_open = false;
let content = match text.as_bytes().get(indent) {
Some(&b) if b > 0x20 && b < 0x7f => &text[indent..],
_ => text.trim_start_matches(is_trim_whitespace),
};
if !content.is_empty() && !content.starts_with('#') {
let item = indent == 0 && is_item(text);
if owner == RangeOwner::Mapping && base.is_none() && item {
base = Some(0);
}
if indent == 0 && !(base == Some(0) && item) {
return Err(LimaError::new(
Code::UnrecognizedLine,
current,
format!(
"Lima: unrecognised line at line {current}: \"{text}\"{}",
hint(text)
),
));
}
if owner == RangeOwner::Mapping && base.is_none() {
base = Some(indent);
}
if indent > 0
&& !(owner == RangeOwner::Mapping && base.is_some_and(|b| indent >= b))
{
let marker = if owner == RangeOwner::Inline {
MULTILINE_VALUE_HINT
} else {
""
};
return Err(LimaError::new(
Code::InvalidIndentation,
current,
format!(
"Lima: unexpected indentation at line {current}: \"{content}\"{marker}"
),
));
}
}
}
match line_end {
Some(e) => {
pos = e + 1;
current += 1;
}
None => break,
}
}
Ok(())
}
fn hint(text: &str) -> String {
if let Some(hint) = specific_key_hint(text) {
return hint;
}
if text == "---" || text == "..." {
return concat!(
" (a YAML document marker; Lima has none, ",
"and the caller strips the front matter's own \"---\")"
)
.into();
}
if text.starts_with('%') {
return " (a YAML directive; Lima has no directives)".into();
}
if is_item(text) {
return " (a sequence item belongs to a key with no value of its own)".into();
}
if text.starts_with('"') || text.starts_with('\'') {
return " (a quoted key closes on its own line)".into();
}
if let Some(separator) = separator_index(text) {
if !is_valid_unquoted(&text[..separator], false) {
return KEY_GRAMMAR_HINT.into();
}
}
let first_word = text.split(' ').next().unwrap_or("");
let key_chars = is_valid_unquoted(first_word, false);
if !text.contains(':') || (!first_word.is_empty() && key_chars) {
return " (a key needs \": \" after it)".into();
}
KEY_GRAMMAR_HINT.into()
}
fn is_item(text: &str) -> bool {
text.strip_prefix('-')
.is_some_and(|rest| rest.is_empty() || rest.starts_with(is_trim_whitespace))
}