Skip to content

Commit feeedd5

Browse files
authored
feat(options): make stylesheet methods optional (#168)
## Summary - give every `StyleSheet` method its existing `DefaultStyleSheet` value as a trait default - let custom style sheets override only the choices they need - keep the canonical renderer output unchanged and simplify focused examples and tests ## Rationale `StyleSheet` is a customization policy rather than a contract where every choice must be supplied. Each method already has an unambiguous canonical value, so requiring downstream implementations to repeat unrelated defaults adds boilerplate without providing additional correctness. The tradeoff is that accidentally omitting an intended override now compiles, but that matches the trait's opt-in customization model and makes future style hooks source-compatible by construction. ## Validation - `cargo test --workspace --all-features` - `cargo test -p tui-markdown --no-default-features` - `cargo clippy --workspace --all-targets --all-features -- -D warnings` - `cargo fmt --all -- --check`
1 parent 135c2db commit feeedd5

8 files changed

Lines changed: 31 additions & 257 deletions

File tree

‎tui-markdown/src/options.rs‎

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,6 @@ pub enum ImageFallback {
6565
/// fn heading(&self, _level: u8) -> Style {
6666
/// Style::new().bold()
6767
/// }
68-
///
69-
/// fn code(&self) -> Style {
70-
/// Style::new().white().on_dark_gray()
71-
/// }
72-
///
73-
/// fn link(&self) -> Style {
74-
/// Style::new().blue().underlined()
75-
/// }
76-
///
77-
/// fn blockquote(&self) -> Style {
78-
/// Style::new().yellow()
79-
/// }
80-
///
81-
/// fn heading_meta(&self) -> Style {
82-
/// Style::new().dim()
83-
/// }
84-
///
85-
/// fn metadata_block(&self) -> Style {
86-
/// Style::new().light_yellow()
87-
/// }
8868
/// }
8969
///
9070
/// let options = Options::new(MyStyleSheet);
@@ -194,26 +174,6 @@ mod tests {
194174
_ => Style::new().green(),
195175
}
196176
}
197-
198-
fn code(&self) -> Style {
199-
Style::new().white().on_black()
200-
}
201-
202-
fn link(&self) -> Style {
203-
Style::new().blue().underlined()
204-
}
205-
206-
fn blockquote(&self) -> Style {
207-
Style::new().yellow()
208-
}
209-
210-
fn heading_meta(&self) -> Style {
211-
Style::new().dim()
212-
}
213-
214-
fn metadata_block(&self) -> Style {
215-
Style::new().light_yellow()
216-
}
217177
}
218178

219179
let options = Options {
@@ -227,7 +187,7 @@ mod tests {
227187
assert_eq!(options.styles.heading(2), Style::new().green());
228188
assert_eq!(options.styles.code(), Style::new().white().on_black());
229189
assert_eq!(options.styles.link(), Style::new().blue().underlined());
230-
assert_eq!(options.styles.blockquote(), Style::new().yellow());
190+
assert_eq!(options.styles.blockquote(), Style::new().green());
231191
assert_eq!(options.styles.heading_meta(), Style::new().dim());
232192
assert_eq!(options.styles.metadata_block(), Style::new().light_yellow());
233193
assert_eq!(options.styles.image_alt(), Style::new().dim().italic());

‎tui-markdown/src/renderer/blockquote.rs‎

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -92,30 +92,6 @@ mod tests {
9292
struct CustomAlertStyleSheet;
9393

9494
impl StyleSheet for CustomAlertStyleSheet {
95-
fn heading(&self, level: u8) -> Style {
96-
DefaultStyleSheet.heading(level)
97-
}
98-
99-
fn code(&self) -> Style {
100-
DefaultStyleSheet.code()
101-
}
102-
103-
fn link(&self) -> Style {
104-
DefaultStyleSheet.link()
105-
}
106-
107-
fn blockquote(&self) -> Style {
108-
DefaultStyleSheet.blockquote()
109-
}
110-
111-
fn heading_meta(&self) -> Style {
112-
DefaultStyleSheet.heading_meta()
113-
}
114-
115-
fn metadata_block(&self) -> Style {
116-
DefaultStyleSheet.metadata_block()
117-
}
118-
11995
fn alert(&self, kind: AlertKind) -> Style {
12096
match kind {
12197
AlertKind::Note => Style::new().on_red(),
@@ -128,30 +104,6 @@ mod tests {
128104
struct CustomAlertHeadingStyleSheet;
129105

130106
impl StyleSheet for CustomAlertHeadingStyleSheet {
131-
fn heading(&self, level: u8) -> Style {
132-
DefaultStyleSheet.heading(level)
133-
}
134-
135-
fn code(&self) -> Style {
136-
DefaultStyleSheet.code()
137-
}
138-
139-
fn link(&self) -> Style {
140-
DefaultStyleSheet.link()
141-
}
142-
143-
fn blockquote(&self) -> Style {
144-
DefaultStyleSheet.blockquote()
145-
}
146-
147-
fn heading_meta(&self) -> Style {
148-
DefaultStyleSheet.heading_meta()
149-
}
150-
151-
fn metadata_block(&self) -> Style {
152-
DefaultStyleSheet.metadata_block()
153-
}
154-
155107
fn alert_icon(&self, kind: AlertKind) -> &str {
156108
match kind {
157109
AlertKind::Note => "!!",

‎tui-markdown/src/renderer/code.rs‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -145,36 +145,11 @@ mod tests {
145145
use super::*;
146146
use crate::renderer::test_support::{with_tracing, DefaultGuard};
147147
use crate::renderer::*;
148-
use crate::DefaultStyleSheet;
149148

150149
#[derive(Clone, Copy)]
151150
struct CustomCodeBlockFence(&'static str);
152151

153152
impl StyleSheet for CustomCodeBlockFence {
154-
fn heading(&self, level: u8) -> Style {
155-
DefaultStyleSheet.heading(level)
156-
}
157-
158-
fn code(&self) -> Style {
159-
DefaultStyleSheet.code()
160-
}
161-
162-
fn link(&self) -> Style {
163-
DefaultStyleSheet.link()
164-
}
165-
166-
fn blockquote(&self) -> Style {
167-
DefaultStyleSheet.blockquote()
168-
}
169-
170-
fn heading_meta(&self) -> Style {
171-
DefaultStyleSheet.heading_meta()
172-
}
173-
174-
fn metadata_block(&self) -> Style {
175-
DefaultStyleSheet.metadata_block()
176-
}
177-
178153
fn code_block_fence(&self) -> &str {
179154
self.0
180155
}

‎tui-markdown/src/renderer/definition_list.rs‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ mod tests {
6464
use super::*;
6565
use crate::renderer::test_support::{with_tracing, DefaultGuard};
6666
use crate::renderer::*;
67-
use crate::*;
6867

6968
mod definition_list {
7069
use pretty_assertions::assert_eq;
@@ -75,30 +74,6 @@ mod tests {
7574
struct CustomDefinitionStyleSheet;
7675

7776
impl StyleSheet for CustomDefinitionStyleSheet {
78-
fn heading(&self, level: u8) -> Style {
79-
DefaultStyleSheet.heading(level)
80-
}
81-
82-
fn code(&self) -> Style {
83-
DefaultStyleSheet.code()
84-
}
85-
86-
fn link(&self) -> Style {
87-
DefaultStyleSheet.link()
88-
}
89-
90-
fn blockquote(&self) -> Style {
91-
DefaultStyleSheet.blockquote()
92-
}
93-
94-
fn heading_meta(&self) -> Style {
95-
DefaultStyleSheet.heading_meta()
96-
}
97-
98-
fn metadata_block(&self) -> Style {
99-
DefaultStyleSheet.metadata_block()
100-
}
101-
10277
fn definition_term(&self) -> Style {
10378
Style::new().red().underlined()
10479
}

‎tui-markdown/src/renderer/footnote.rs‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ mod tests {
5151
use super::*;
5252
use crate::renderer::test_support::{with_tracing, DefaultGuard};
5353
use crate::renderer::*;
54-
use crate::*;
5554

5655
mod footnotes {
5756
use pretty_assertions::assert_eq;
@@ -215,30 +214,6 @@ mod tests {
215214
struct CustomFootnoteStyle;
216215

217216
impl StyleSheet for CustomFootnoteStyle {
218-
fn heading(&self, level: u8) -> Style {
219-
DefaultStyleSheet.heading(level)
220-
}
221-
222-
fn code(&self) -> Style {
223-
DefaultStyleSheet.code()
224-
}
225-
226-
fn link(&self) -> Style {
227-
DefaultStyleSheet.link()
228-
}
229-
230-
fn blockquote(&self) -> Style {
231-
DefaultStyleSheet.blockquote()
232-
}
233-
234-
fn heading_meta(&self) -> Style {
235-
DefaultStyleSheet.heading_meta()
236-
}
237-
238-
fn metadata_block(&self) -> Style {
239-
DefaultStyleSheet.metadata_block()
240-
}
241-
242217
fn footnote_ref(&self) -> Style {
243218
Style::new().red().underlined()
244219
}

‎tui-markdown/src/renderer/heading.rs‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -102,36 +102,11 @@ mod tests {
102102
use super::*;
103103
use crate::renderer::test_support::{with_tracing, DefaultGuard};
104104
use crate::renderer::*;
105-
use crate::DefaultStyleSheet;
106105

107106
#[derive(Clone, Copy)]
108107
struct CustomHeadingMarker;
109108

110109
impl StyleSheet for CustomHeadingMarker {
111-
fn heading(&self, level: u8) -> Style {
112-
DefaultStyleSheet.heading(level)
113-
}
114-
115-
fn code(&self) -> Style {
116-
DefaultStyleSheet.code()
117-
}
118-
119-
fn link(&self) -> Style {
120-
DefaultStyleSheet.link()
121-
}
122-
123-
fn blockquote(&self) -> Style {
124-
DefaultStyleSheet.blockquote()
125-
}
126-
127-
fn heading_meta(&self) -> Style {
128-
DefaultStyleSheet.heading_meta()
129-
}
130-
131-
fn metadata_block(&self) -> Style {
132-
DefaultStyleSheet.metadata_block()
133-
}
134-
135110
fn heading_marker(&self, level: u8) -> &str {
136111
match level {
137112
1 => "",

‎tui-markdown/src/renderer/math.rs‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ mod tests {
4444
use super::*;
4545
use crate::renderer::test_support::{with_tracing, DefaultGuard};
4646
use crate::renderer::*;
47-
use crate::*;
4847

4948
mod math {
5049
use pretty_assertions::assert_eq;
@@ -108,30 +107,6 @@ mod tests {
108107
struct CustomMathStyle;
109108

110109
impl StyleSheet for CustomMathStyle {
111-
fn heading(&self, level: u8) -> Style {
112-
DefaultStyleSheet.heading(level)
113-
}
114-
115-
fn code(&self) -> Style {
116-
DefaultStyleSheet.code()
117-
}
118-
119-
fn link(&self) -> Style {
120-
DefaultStyleSheet.link()
121-
}
122-
123-
fn blockquote(&self) -> Style {
124-
DefaultStyleSheet.blockquote()
125-
}
126-
127-
fn heading_meta(&self) -> Style {
128-
DefaultStyleSheet.heading_meta()
129-
}
130-
131-
fn metadata_block(&self) -> Style {
132-
DefaultStyleSheet.metadata_block()
133-
}
134-
135110
fn math_display(&self) -> Style {
136111
Style::new().red().bold()
137112
}

0 commit comments

Comments
 (0)