Trait Tldr

Source
pub trait Tldr<T = String> {
    type Error;

    // Provided methods
    fn who(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error> { ... }
    fn what(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error> { ... }
    fn when(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error> { ... }
    fn where(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error> { ... }
    fn why(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error> { ... }
    fn whence(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error> { ... }
    fn how(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error> { ... }
}
Expand description

A trait for generating a TL;DR summary.

§Examples

Implementing the Tldr trait for a custom type is simple:

struct Rectangle {
    width: u32,
    height: u32,
}

impl Tldr<String> for Rectangle {
    type Error = Box<dyn Error>;

    fn what(&self, _ctx: &TldrContext) -> TldrResult<String> {
        Ok(Some(format!("A rectangle with a width of {} and a height of {}.", self.width, self.height)))
    }
}

Generally, you’d want to match on the context language to provide a localized response:

struct Rectangle {
    width: u32,
    height: u32,
}

impl Tldr<String> for Rectangle {
    type Error = Box<dyn Error>;

    fn what(&self, ctx: &TldrContext) -> TldrResult<String> {
        use TldrLanguage::*;
        Ok(match ctx.language {
            English => Some(format!("A rectangle with a width of {} and a height of {}.", self.width, self.height)),
            _ => None,
        })
    }
}

See: en.wikipedia.org/wiki/Five_Ws

See: en.wikipedia.org/wiki/Interrogative_word

Required Associated Types§

Source

type Error

The associated error type.

If in doubt, specify this as Box<dyn Error>.

Provided Methods§

Source

fn who(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>

Answers the question “Who?”

See: en.wiktionary.org/wiki/who

Source

fn what(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>

Answers the question “What?”

See: en.wiktionary.org/wiki/what

Source

fn when(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>

Answers the question “When?”

See: en.wiktionary.org/wiki/when

Source

fn where(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>

Answers the question “Where?”

See: en.wiktionary.org/wiki/where

Source

fn why(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>

Answers the question “Why?”

See: en.wiktionary.org/wiki/why

Source

fn whence(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>

Answers the question “Whence?” (i.e., “From where?”)

See: en.wiktionary.org/wiki/whence

Source

fn how(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>

Answers the question “How?”

See: en.wiktionary.org/wiki/how

Implementors§