use crate::Location;
use std::borrow::Cow;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExternalMessageSource {
SaphyrParser,
Garde,
Validator,
}
#[derive(Debug, Clone)]
pub struct ExternalMessage<'a> {
pub source: ExternalMessageSource,
pub original: &'a str,
pub code: Option<&'a str>,
pub params: &'a [(String, String)],
}
pub trait Localizer {
fn attach_location<'a>(&self, base: Cow<'a, str>, loc: Location) -> Cow<'a, str> {
if loc == Location::UNKNOWN {
base
} else {
Cow::Owned(format!(
"{base} at line {}, column {}",
loc.line, loc.column
))
}
}
fn root_path_label(&self) -> Cow<'static, str> {
Cow::Borrowed("<root>")
}
fn alias_defined_at(&self, defined: Location) -> String {
format!(
" (defined at line {}, column {})",
defined.line, defined.column
)
}
fn validation_issue_line(
&self,
resolved_path: &str,
entry: &str,
loc: Option<Location>,
) -> String {
let base = format!("validation error at {resolved_path}: {entry}");
match loc {
Some(l) if l != Location::UNKNOWN => {
self.attach_location(Cow::Owned(base), l).into_owned()
}
_ => base,
}
}
fn join_validation_issues(&self, lines: &[String]) -> String {
lines.join("\n")
}
fn defined(&self) -> Cow<'static, str> {
Cow::Borrowed("(defined)")
}
fn defined_here(&self) -> Cow<'static, str> {
Cow::Borrowed("(defined here)")
}
fn value_used_here(&self) -> Cow<'static, str> {
Cow::Borrowed("the value is used here")
}
fn defined_window(&self) -> Cow<'static, str> {
Cow::Borrowed("defined here")
}
fn validation_base_message(&self, entry: &str, resolved_path: &str) -> String {
format!("validation error: {entry} for `{resolved_path}`")
}
fn invalid_here(&self, base: &str) -> String {
format!("invalid here, {base}")
}
fn value_comes_from_the_anchor(&self, def: Location) -> String {
format!(
" | This value comes indirectly from the anchor at line {} column {}:",
def.line, def.column
)
}
fn snippet_location_prefix(&self, loc: Location) -> String {
if loc == Location::UNKNOWN {
String::new()
} else {
format!("line {} column {}", loc.line(), loc.column())
}
}
fn override_external_message<'a>(&self, _msg: ExternalMessage<'a>) -> Option<Cow<'a, str>> {
None
}
}
#[derive(Debug, Default, Clone, Copy)]
pub struct DefaultEnglishLocalizer;
impl Localizer for DefaultEnglishLocalizer {}
pub static DEFAULT_ENGLISH_LOCALIZER: DefaultEnglishLocalizer = DefaultEnglishLocalizer;