Rust abstractions for TL;DR summarization using the five Ws: who? what? when? where? why?
- Provides the
Tldr
trait for generating TL;DR summaries. - Provides the
ToTldr
trait for converting objects into TL;DR summaries. - Supports multilingual TL;DR generation while defaulting to English.
- Zero required dependencies, only optional integrations with Serde & Bon.
- Adheres to the Rust API Guidelines in its naming conventions.
- 100% free and unencumbered public domain software.
- Rust 1.85+ (2024 edition)
cargo add tldr-traits --rename tldr
[dependencies]
tldr = { version = "0", package = "tldr-traits" }
[dependencies]
tldr = { version = "0", package = "tldr-traits", default-features = false, features = ["serde"] }
use tldr::{Tldr, TldrContext, TldrLanguage, TldrResult, TldrSummary, ToTldr};
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)))
}
}
pub trait Tldr<T = String> {
fn who(&self, ctx: &TldrContext) -> TldrResult<T>;
fn what(&self, ctx: &TldrContext) -> TldrResult<T>;
fn when(&self, ctx: &TldrContext) -> TldrResult<T>;
fn where(&self, ctx: &TldrContext) -> TldrResult<T>;
fn why(&self, ctx: &TldrContext) -> TldrResult<T>;
fn whence(&self, ctx: &TldrContext) -> TldrResult<T>;
fn how(&self, ctx: &TldrContext) -> TldrResult<T>;
}
pub struct TldrContext {
pub language: TldrLanguage,
}
pub enum TldrLanguage {
#[default]
English,
// ...
Other(String),
}
pub type TldrResult<T = String, E = Box<dyn Error>> =
Result<Option<T>, E>;
pub struct TldrSummary<T = String> {
pub who: Option<T>,
pub what: Option<T>,
pub when: Option<T>,
pub where: Option<T>,
pub why: Option<T>,
pub whence: Option<T>,
pub how: Option<T>,
}
pub trait ToTldr<T = String> {
fn to_tldr(&self) -> Box<dyn Tldr<T>>;
}
git clone https://github.com/dryrust/tldr.rs.git