fn render_literal(literal: &str) -> String { match literal { "\n" => "newline".to_owned(), "`" => "'`'".to_owned(),
s if s.chars().all(|c| c.is_ascii_control()) => {
format!("`{}`", s.escape_debug())
}
s => format!("`{s}`"),
}
}
/// Displays a TOML parse error /// /// # Example /// /// TOML parse error at line 1, column 10 /// | /// 1 | 00:32:00.a999999 /// | ^ /// Unexpected `a` /// Expected `digit` /// While parsing a Time /// While parsing a Date-Time impl core::fmt::Display for Error { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { letmut context = false; iflet (Some(input), Some(span)) = (&self.input, self.span()) {
context = true;
let (line, column) = translate_position(input.as_bytes(), span.start); let line_num = line + 1; let col_num = column + 1; let gutter = line_num.to_string().len(); let content = input.split('\n').nth(line).expect("valid line number"); let highlight_len = span.end - span.start; // Allow highlight to go one past the line let highlight_len = highlight_len.min(content.len().saturating_sub(column));
writeln!(f, "TOML parse error at line {line_num}, column {col_num}")?; // | for _ in0..=gutter {
write!(f, " ")?;
}
writeln!(f, "|")?;
// | ^ for _ in0..=gutter {
write!(f, " ")?;
}
write!(f, "|")?; for _ in0..=column {
write!(f, " ")?;
} // The span will be empty at eof, so we need to make sure we always print at least // one `^`
write!(f, "^")?; for _ in1..highlight_len {
write!(f, "^")?;
}
writeln!(f)?;
}
writeln!(f, "{}", self.message)?; if !context && !self.keys.is_empty() {
writeln!(f, "in `{}`", self.keys.join("."))?;
}
Ok(())
}
}
#[cfg(feature = "std")] impl std::error::Error for Error {} #[cfg(not(feature = "std"))] #[cfg(feature = "serde")] impl serde_core::de::StdError for Error {}
#[cfg(feature = "parse")] impl<'i> toml_parser::ErrorSink for TomlSink<'i, Vec<Error>> { fn report_error(&mutself, error: toml_parser::ParseError) { let input = self
.input
.get_or_insert_with(|| alloc::sync::Arc::from(self.source.input())); let error = Error::new(input.clone(), error); self.sink.push(error);
}
}
#[cfg(test)] mod test_translate_position { usesuper::*;
#[test] fn empty() { let input = b""; let index = 0; let position = translate_position(&input[..], index);
assert_eq!(position, (0, 0));
}
#[test] fn start() { let input = b"Hello"; let index = 0; let position = translate_position(&input[..], index);
assert_eq!(position, (0, 0));
}
#[test] fn end() { let input = b"Hello"; let index = input.len() - 1; let position = translate_position(&input[..], index);
assert_eq!(position, (0, input.len() - 1));
}
#[test] fn after() { let input = b"Hello"; let index = input.len(); let position = translate_position(&input[..], index);
assert_eq!(position, (0, input.len()));
}
#[test] fn first_line() { let input = b"Hello\nWorld\n"; let index = 2; let position = translate_position(&input[..], index);
assert_eq!(position, (0, 2));
}
#[test] fn end_of_line() { let input = b"Hello\nWorld\n"; let index = 5; let position = translate_position(&input[..], index);
assert_eq!(position, (0, 5));
}
#[test] fn start_of_second_line() { let input = b"Hello\nWorld\n"; let index = 6; let position = translate_position(&input[..], index);
assert_eq!(position, (1, 0));
}
#[test] fn second_line() { let input = b"Hello\nWorld\n"; let index = 8; let position = translate_position(&input[..], index);
assert_eq!(position, (1, 2));
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.