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,
})
}
}
Required Associated Types§
Provided Methods§
Sourcefn who(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>
fn who(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>
Answers the question “Who?”
Sourcefn what(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>
fn what(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>
Answers the question “What?”
Sourcefn when(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>
fn when(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>
Answers the question “When?”
Sourcefn where(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>
fn where(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>
Answers the question “Where?”
Sourcefn why(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>
fn why(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>
Answers the question “Why?”
Sourcefn whence(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>
fn whence(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>
Answers the question “Whence?” (i.e., “From where?”)
Sourcefn how(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>
fn how(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error>
Answers the question “How?”