tldr_traits/
summary.rs

1// This is free and unencumbered software released into the public domain.
2
3use crate::{Tldr, TldrContext, TldrResult, ToTldr};
4use alloc::{boxed::Box, string::String};
5use core::fmt::Debug;
6
7/// An owned TL;DR summary.
8///
9/// # Examples
10///
11/// ```rust
12/// # use tldr_traits::TldrSummary;
13/// let summary: TldrSummary<String> = TldrSummary::builder()
14///     .who("John")
15///     .what("a book")
16///     .when("yesterday")
17///     .r#where("the library")
18///     .why("to learn")
19///     .whence("from the internet")
20///     .how("by reading")
21///     .build();
22/// ```
23///
24/// See: [en.wikipedia.org/wiki/Five_Ws](https://en.wikipedia.org/wiki/Five_Ws)
25///
26/// See: [en.wikipedia.org/wiki/Interrogative_word](https://en.wikipedia.org/wiki/Interrogative_word)
27#[derive(Clone, Debug, Default)]
28#[cfg_attr(feature = "builder", derive(bon::Builder))]
29#[cfg_attr(feature = "builder", builder(on(T, into)))]
30#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
31#[cfg_attr(feature = "serde", serde(default))]
32pub struct TldrSummary<T: Clone + Debug + Default = String> {
33    /// Answers the question "Who?"
34    ///
35    /// See: [en.wiktionary.org/wiki/who](https://en.wiktionary.org/wiki/who)
36    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
37    pub who: Option<T>,
38
39    /// Answers the question "What?"
40    ///
41    /// See: [en.wiktionary.org/wiki/what](https://en.wiktionary.org/wiki/what)
42    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
43    pub what: Option<T>,
44
45    /// Answers the question "When?"
46    ///
47    /// See: [en.wiktionary.org/wiki/when](https://en.wiktionary.org/wiki/when)
48    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
49    pub when: Option<T>,
50
51    /// Answers the question "Where?"
52    ///
53    /// See: [en.wiktionary.org/wiki/where](https://en.wiktionary.org/wiki/where)
54    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
55    pub r#where: Option<T>,
56
57    /// Answers the question "Why?"
58    ///
59    /// See: [en.wiktionary.org/wiki/why](https://en.wiktionary.org/wiki/why)
60    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
61    pub why: Option<T>,
62
63    /// Answers the question "Whence?" (i.e., "From where?")
64    ///
65    /// See: [en.wiktionary.org/wiki/whence](https://en.wiktionary.org/wiki/whence)
66    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
67    pub whence: Option<T>,
68
69    /// Answers the question "How?"
70    ///
71    /// See: [en.wiktionary.org/wiki/how](https://en.wiktionary.org/wiki/how)
72    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
73    pub how: Option<T>,
74}
75
76impl<T: Clone + Debug + Default + 'static> ToTldr<T> for TldrSummary<T> {
77    type Error = ();
78
79    fn to_tldr(&self) -> Box<dyn Tldr<T, Error = Self::Error>> {
80        Box::new(self.clone())
81    }
82}
83
84impl<T: Clone + Debug + Default> Tldr<T> for TldrSummary<T> {
85    type Error = ();
86
87    fn who(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error> {
88        Ok(self.who.clone())
89    }
90
91    fn what(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error> {
92        Ok(self.what.clone())
93    }
94
95    fn when(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error> {
96        Ok(self.when.clone())
97    }
98
99    fn r#where(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error> {
100        Ok(self.r#where.clone())
101    }
102
103    fn why(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error> {
104        Ok(self.why.clone())
105    }
106
107    fn whence(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error> {
108        Ok(self.whence.clone())
109    }
110
111    fn how(&self, _ctx: &TldrContext) -> TldrResult<T, Self::Error> {
112        Ok(self.how.clone())
113    }
114}