Skip to main content

Function

Trait Function 

Source
pub trait Function: Sync {
    // Required methods
    fn resolve<'a>(
        &self,
        cx: &FnContext<'_>,
        operand: Option<&Value<'a>>,
        options: &Options<'_, 'a>,
        errs: &mut dyn ErrorSink,
    ) -> Option<Value<'a>>;
    fn format(&self, cx: &FnContext<'_>, value: &Value<'_>, out: &mut dyn Sink);

    // Provided methods
    fn formattable(
        &self,
        cx: &FnContext<'_>,
        value: &Value<'_>,
    ) -> Result<(), FormatError> { ... }
    fn format_parts(
        &self,
        cx: &FnContext<'_>,
        value: &Value<'_>,
        out: &mut dyn SubPartSink,
    ) { ... }
    fn part_kind(&self) -> &'static str { ... }
    fn dir(&self, cx: &FnContext<'_>, value: &Value<'_>) -> Dir { ... }
    fn selectable(&self, value: &Value<'_>) -> bool { ... }
    fn matches(
        &self,
        cx: &FnContext<'_>,
        value: &Value<'_>,
        key: &str,
        errs: &mut dyn ErrorSink,
    ) -> bool { ... }
    fn better_than(
        &self,
        cx: &FnContext<'_>,
        value: &Value<'_>,
        key1: &str,
        key2: &str,
    ) -> bool { ... }
}
Expand description

A function handler. :string, :number, :integer, :offset are in crate::functions; custom functions implement the same trait (the suite’s :test:* functions are written against it).

A handler first resolves an expression to a Value; the runtime then asks the same handler, for that value, whether and how it formats, its direction, and whether and how it selects.

Required Methods§

Source

fn resolve<'a>( &self, cx: &FnContext<'_>, operand: Option<&Value<'a>>, options: &Options<'_, 'a>, errs: &mut dyn ErrorSink, ) -> Option<Value<'a>>

Function resolution (formatting.md): operand resolved — a Value::Fallback when it failed to resolve, so the handler decides (plans/03-runtime.md §2.6) — and options resolved with u:id/u:dir removed. None makes the expression a fallback value; the handler has reported why.

Source

fn format(&self, cx: &FnContext<'_>, value: &Value<'_>, out: &mut dyn Sink)

Writes value’s formatted text.

Provided Methods§

Source

fn formattable( &self, cx: &FnContext<'_>, value: &Value<'_>, ) -> Result<(), FormatError>

Whether value can be formatted. Err(e): the placeholder is a fallback value and e is reported.

Source

fn format_parts( &self, cx: &FnContext<'_>, value: &Value<'_>, out: &mut dyn SubPartSink, )

Writes value’s formatted sub-parts (none by default).

Source

fn part_kind(&self) -> &'static str

The type of this handler’s expression parts.

Source

fn dir(&self, cx: &FnContext<'_>, value: &Value<'_>) -> Dir

The direction of value’s formatted text (Default Bidi Strategy); Auto = unknown.

Source

fn selectable(&self, value: &Value<'_>) -> bool

Whether value supports selection (formatting.md, “Resolve Selectors”). A value that does not matches only *, with Bad Selector.

Source

fn matches( &self, cx: &FnContext<'_>, value: &Value<'_>, key: &str, errs: &mut dyn ErrorSink, ) -> bool

Match(value, key); key is NFC. Report e.g. Bad Variant Key through errs.

Source

fn better_than( &self, cx: &FnContext<'_>, value: &Value<'_>, key1: &str, key2: &str, ) -> bool

BetterThan(value, key1, key2), for two keys that both match.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§