Trait ecma402_traits::listformat::Format
source · pub trait Format {
type Error: Error;
// Required methods
fn try_new<L>(l: L, opts: Options) -> Result<Self, Self::Error>
where L: Locale,
Self: Sized;
fn format<I, L, W>(&self, list: L, writer: &mut W) -> Result
where I: Display,
L: IntoIterator<Item = I>,
W: Write;
}
Expand description
The package workhorse: formats supplied pieces of text into an ergonomically formatted list.
While ECMA 402 originally has functions under Intl
, we probably want to
obtain a separate factory from each implementor.
Purposely omitted:
supported_locales_of
.
Required Associated Types§
Required Methods§
sourcefn try_new<L>(l: L, opts: Options) -> Result<Self, Self::Error>
fn try_new<L>(l: L, opts: Options) -> Result<Self, Self::Error>
Creates a new Format.
Creation may fail, for example, if the locale-specific data is not loaded, or if the supplied options are inconsistent.
sourcefn format<I, L, W>(&self, list: L, writer: &mut W) -> Result
fn format<I, L, W>(&self, list: L, writer: &mut W) -> Result
Formats list
into the supplied standard writer
fmt::Write.
The original ECMA 402 function returns a string. This is likely the only
reasonably generic option in JavaScript so it is adequate. In Rust, however, it is
possible to pass in a standard formatting strategy (through writer
).
This makes it unnecessary for Format to implement its own, and can completely avoid constructing any intermediary representation. This, in turn, allows the user to provide a purpose built formatter, or a custom one if needed.
A purpose built formatter could be one that formats into a fixed-size buffer; or another that knows how to format strings into a DOM. If ECMA 402 compatibility is needed, the user can force formatting into a string by passing the appropriate formatter.
Note:
- Should there be a convenience method that prints to string specifically?
- Do we need
format_into_parts
?