Skip to main content

serde_saphyr/de/
localizer.rs

1//! Localization / wording customization.
2//!
3//! The [`Localizer`] trait is the central hook for customizing *crate-authored wording*.
4//! It is intentionally designed to be low-boilerplate:
5//!
6//! - Every method has a reasonable English default.
7//! - You can override only the pieces you care about, while inheriting all other defaults.
8//!
9//! This crate may also show *external* message text coming from dependencies (for example
10//! `saphyr-parser` scan errors, or validator messages). Where such texts are used, the
11//! rendering pipeline should provide a best-effort opportunity to override them via
12//! [`Localizer::override_external_message`].
13//!
14//! ## Example: override a single phrase
15//!
16//! ```rust
17//! use serde_saphyr::{Error, Location};
18//! use serde_saphyr::localizer::{Localizer, DEFAULT_ENGLISH_LOCALIZER};
19//! use std::borrow::Cow;
20//!
21//! /// A wrapper that overrides only location suffix wording, delegating everything else.
22//! struct Pirate<'a> {
23//!     base: &'a dyn Localizer,
24//! }
25//!
26//! impl Localizer for Pirate<'_> {
27//!     fn attach_location<'b>(&self, base: Cow<'b, str>, loc: Location) -> Cow<'b, str> {
28//!         if loc == Location::UNKNOWN {
29//!             return base;
30//!         }
31//!         // Note: you can also delegate to `self.base.attach_location(...)` if you want.
32//!         Cow::Owned(format!(
33//!             "{base}. Bug lurks on line {}, then {} runes in",
34//!             loc.line(),
35//!             loc.column()
36//!         ))
37//!     }
38//! }
39//!
40//! // This snippet shows the customization building blocks; the crate's rendering APIs
41//! // obtain a `Localizer` via the `MessageFormatter`.
42//! # let _ = (Error::InvalidUtf8Input, &DEFAULT_ENGLISH_LOCALIZER);
43//! ```
44
45use crate::Location;
46use std::borrow::Cow;
47
48/// Where an “external” message comes from.
49///
50/// External messages are those primarily produced by dependencies (parser / validators).
51#[derive(Debug, Clone, Copy, PartialEq, Eq)]
52pub enum ExternalMessageSource {
53    /// Text produced by `saphyr-parser` (e.g. scanning errors).
54    SaphyrParser,
55    /// Text produced by `garde` validation rules.
56    Garde,
57    /// Text produced by `validator` validation rules.
58    Validator,
59}
60
61/// A best-effort description of an external message.
62///
63/// The crate should pass as much stable metadata as it has (e.g. `code` and `params` for
64/// `validator`) so the localizer can override *specific* messages without string matching.
65#[derive(Debug, Clone)]
66pub struct ExternalMessage<'a> {
67    pub source: ExternalMessageSource,
68    /// The original text as provided by the external library.
69    pub original: &'a str,
70    /// Stable-ish identifier when available (e.g. validator error code).
71    pub code: Option<&'a str>,
72    /// Optional structured parameters when available.
73    pub params: &'a [(String, String)],
74}
75
76/// All crate-authored wording customization points.
77///
78/// Implementors should typically override *only a few* methods.
79/// Everything else should default to English (via the default method bodies).
80pub trait Localizer {
81    // ---------------- Common tiny building blocks ----------------
82
83    /// Attach a location suffix to `base`.
84    ///
85    /// Renderers must use this instead of hard-coding English wording like
86    /// `" at line X, column Y"`.
87    ///
88    /// Default:
89    /// - If `loc == Location::UNKNOWN`: returns `base` unchanged.
90    /// - Otherwise: returns `"{base} at line {line}, column {column}"`.
91    fn attach_location<'a>(&self, base: Cow<'a, str>, loc: Location) -> Cow<'a, str> {
92        if loc == Location::UNKNOWN {
93            base
94        } else {
95            Cow::Owned(format!(
96                "{base} at line {}, column {}",
97                loc.line, loc.column
98            ))
99        }
100    }
101
102    /// Label used when a path has no leaf.
103    ///
104    /// Default <root>
105    fn root_path_label(&self) -> Cow<'static, str> {
106        Cow::Borrowed("<root>")
107    }
108
109    /// Suffix for alias-related errors when a distinct defined-location is available.
110    ///
111    /// Default wording matches the crate's historical English output:
112    /// `" (defined at line X, column Y)"`.
113    ///
114    /// Default: `format!(" (defined at line {line}, column {column})", ...)`.
115    fn alias_defined_at(&self, defined: Location) -> String {
116        format!(
117            " (defined at line {}, column {})",
118            defined.line, defined.column
119        )
120    }
121
122    // ---------------- Validation (plain text) glue ----------------
123
124    /// Render one validation issue line.
125    ///
126    /// The crate provides `resolved_path`, `entry` and the chosen `loc`.
127    ///
128    /// Default:
129    /// - Base text: `"validation error at {resolved_path}: {entry}"`.
130    /// - If `loc` is `Some` and not `Location::UNKNOWN`, appends a location suffix via
131    ///   [`Localizer::attach_location`].
132    fn validation_issue_line(
133        &self,
134        resolved_path: &str,
135        entry: &str,
136        loc: Option<Location>,
137    ) -> String {
138        let base = format!("validation error at {resolved_path}: {entry}");
139        match loc {
140            Some(l) if l != Location::UNKNOWN => {
141                self.attach_location(Cow::Owned(base), l).into_owned()
142            }
143            _ => base,
144        }
145    }
146
147    /// Join multiple validation issues into one message.
148    ///
149    /// Default: joins `lines` with a single newline (`"\n"`).
150    fn join_validation_issues(&self, lines: &[String]) -> String {
151        lines.join("\n")
152    }
153
154    // ---------------- Validation snippets / diagnostic labels ----------------
155
156    /// Label used for a snippet window when the location is known and considered the
157    /// “definition” site.
158    ///
159    /// Default: `"(defined)"`.
160    fn defined(&self) -> Cow<'static, str> {
161        Cow::Borrowed("(defined)")
162    }
163
164    /// Label used for a snippet window when we only have a “defined here” location.
165    ///
166    /// Default: `"(defined here)"`.
167    fn defined_here(&self) -> Cow<'static, str> {
168        Cow::Borrowed("(defined here)")
169    }
170
171    /// Label used for the primary snippet window when an aliased/anchored value is used
172    /// at a different location than where it was defined.
173    ///
174    /// Default: `"the value is used here"`.
175    fn value_used_here(&self) -> Cow<'static, str> {
176        Cow::Borrowed("the value is used here")
177    }
178
179    /// Label used for the secondary snippet window that points at the anchor definition.
180    ///
181    /// Default: `"defined here"`.
182    fn defined_window(&self) -> Cow<'static, str> {
183        Cow::Borrowed("defined here")
184    }
185
186    /// Compose the base validation message used in snippet rendering.
187    ///
188    /// Default: `"validation error: {entry} for `{resolved_path}`"`.
189    fn validation_base_message(&self, entry: &str, resolved_path: &str) -> String {
190        format!("validation error: {entry} for `{resolved_path}`")
191    }
192
193    /// Compose the “invalid here” prefix for the primary snippet message.
194    ///
195    /// Default: `"invalid here, {base}"`.
196    fn invalid_here(&self, base: &str) -> String {
197        format!("invalid here, {base}")
198    }
199
200    /// Intro line printed between the primary and secondary snippet windows for
201    /// anchor/alias (“indirect value”) cases.
202    ///
203    /// Default:
204    /// `"  | This value comes indirectly from the anchor at line {line} column {column}:"`.
205    fn value_comes_from_the_anchor(&self, def: Location) -> String {
206        format!(
207            "  | This value comes indirectly from the anchor at line {} column {}:",
208            def.line, def.column
209        )
210    }
211
212    // ---------------- External overrides ----------------
213
214    /// Optional hook to override the location prefix used for snippet titles
215    ///
216    /// Default:
217    /// - If `loc == Location::UNKNOWN`: returns an empty string.
218    /// - Otherwise: returns `"line {line} column {column}"`.
219    fn snippet_location_prefix(&self, loc: Location) -> String {
220        if loc == Location::UNKNOWN {
221            String::new()
222        } else {
223            format!("line {} column {}", loc.line(), loc.column())
224        }
225    }
226
227    /// Best-effort hook to override/translate dependency-provided message text.
228    ///
229    /// Default: returns `None` (keep the external message as-is).
230    fn override_external_message<'a>(&self, _msg: ExternalMessage<'a>) -> Option<Cow<'a, str>> {
231        None
232    }
233}
234
235/// Default English localizer used by the crate.
236#[derive(Debug, Default, Clone, Copy)]
237pub struct DefaultEnglishLocalizer;
238
239impl Localizer for DefaultEnglishLocalizer {}
240
241/// A single shared instance of the default English localizer.
242///
243/// This avoids repeated instantiation and provides a convenient reference for wrappers.
244pub static DEFAULT_ENGLISH_LOCALIZER: DefaultEnglishLocalizer = DefaultEnglishLocalizer;