rs-rich-ext 0.0.21

Extensions for the `rich` Rust port: diagnostics, structured data, clap and tracing integration, diffs, workflow renderables and a plugin registry
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! Composable transforms: rewrite what is about to be rendered, one stage at a
//! time.
//!
//! A [`Transform<T>`] takes a value and returns a changed one: a [`Text`] with
//! some lines dropped, a data `Document` (`data::transform`, with the
//! `jsonpath` feature) narrowed to a JSONPath, a [`TableData`](crate::table::TableData) sorted, a
//! git [`Patch`](crate::diff::git::Patch) limited to some files. A
//! [`Pipeline`] runs named stages in the order they were added, and says which
//! stage failed.
//!
//! Text transforms are the plugin-facing kind: they implement
//! [`TextTransform`] from the plugin contract, so a plugin can register one
//! (see [`ExtensionRegistry::text_pipeline`](crate::ExtensionRegistry::text_pipeline)).
//! Every [`TextTransform`] is also a `Transform<Text>`.
//!
//! ```
//! use rich::{Style, Text};
//! use rich_ext::transform::{HighlightMatches, KeepLines, Pipeline};
//!
//! let pipeline = Pipeline::new()
//!     .then("filter", KeepLines::new("ERROR|WARN").unwrap())
//!     .then("highlight", HighlightMatches::new("ERROR", Style::parse("bold red").unwrap()).unwrap());
//! assert_eq!(pipeline.names(), ["filter", "highlight"]);
//!
//! let log = Text::new("INFO start\nWARN slow\nERROR failed\n");
//! let text = pipeline.apply(log).unwrap();
//! assert_eq!(text.plain(), "WARN slow\nERROR failed\n");
//! ```

use std::fmt;

use rich::{Style, Text};
use rich_plugin_api::PluginError;
pub use rich_plugin_api::TextTransform;

/// Why a transform failed.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TransformError {
    message: String,
}

impl TransformError {
    pub fn new(message: impl Into<String>) -> Self {
        TransformError {
            message: message.into(),
        }
    }

    pub fn message(&self) -> &str {
        &self.message
    }
}

impl fmt::Display for TransformError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.message)
    }
}

impl std::error::Error for TransformError {}

impl From<PluginError> for TransformError {
    fn from(error: PluginError) -> Self {
        TransformError::new(error.to_string())
    }
}

/// Rewrites a `T`.
pub trait Transform<T>: Send + Sync {
    fn apply(&self, input: T) -> Result<T, TransformError>;
}

impl<X: TextTransform + ?Sized> Transform<Text> for X {
    fn apply(&self, input: Text) -> Result<Text, TransformError> {
        Ok(self.transform(input)?)
    }
}

/// A transform from a function.
pub fn from_fn<T, F>(f: F) -> impl Transform<T>
where
    F: Fn(T) -> Result<T, TransformError> + Send + Sync,
{
    FromFn(f)
}

struct FromFn<F>(F);

impl<T, F> Transform<T> for FromFn<F>
where
    F: Fn(T) -> Result<T, TransformError> + Send + Sync,
{
    fn apply(&self, input: T) -> Result<T, TransformError> {
        (self.0)(input)
    }
}

/// Which stage of a [`Pipeline`] failed, and why.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PipelineError {
    /// The stage's name, as given to [`Pipeline::then`].
    pub stage: String,
    pub error: TransformError,
}

impl fmt::Display for PipelineError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: {}", self.stage, self.error)
    }
}

impl std::error::Error for PipelineError {}

/// Named transforms run in order: each stage gets the previous one's output.
pub struct Pipeline<T> {
    stages: Vec<(String, Box<dyn Transform<T>>)>,
}

impl<T> Default for Pipeline<T> {
    fn default() -> Self {
        Pipeline { stages: Vec::new() }
    }
}

impl<T> fmt::Debug for Pipeline<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Pipeline")
            .field("stages", &self.names())
            .finish()
    }
}

impl<T> Pipeline<T> {
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a stage at the end.
    #[must_use]
    pub fn then(mut self, name: impl Into<String>, transform: impl Transform<T> + 'static) -> Self {
        self.push(name, Box::new(transform));
        self
    }

    /// Add a boxed stage at the end.
    pub fn push(&mut self, name: impl Into<String>, transform: Box<dyn Transform<T>>) {
        self.stages.push((name.into(), transform));
    }

    /// The stages' names, in order.
    pub fn names(&self) -> Vec<&str> {
        self.stages.iter().map(|(name, _)| name.as_str()).collect()
    }

    pub fn len(&self) -> usize {
        self.stages.len()
    }

    pub fn is_empty(&self) -> bool {
        self.stages.is_empty()
    }

    /// Run every stage in order.
    pub fn apply(&self, input: T) -> Result<T, PipelineError> {
        self.stages
            .iter()
            .try_fold(input, |value, (stage, transform)| {
                transform.apply(value).map_err(|error| PipelineError {
                    stage: stage.clone(),
                    error,
                })
            })
    }
}

/// A pipeline is itself a transform, so pipelines nest.
impl<T> Transform<T> for Pipeline<T> {
    fn apply(&self, input: T) -> Result<T, TransformError> {
        Pipeline::apply(self, input).map_err(|error| TransformError::new(error.to_string()))
    }
}

/// A regular expression compiled once and run on plain `&str`: what a
/// filter tests each row or cell with, where building a [`Text`] for
/// [`KeepLines`] would cost more than the match. The same engine and
/// syntax as [`KeepLines`] and [`HighlightMatches`] (fancy-regex:
/// look-around and back-references).
///
/// ```
/// use rich_ext::transform::Pattern;
///
/// let pattern = Pattern::new("(?i)name9+$").unwrap();
/// assert!(pattern.is_match("Name99"));
/// assert!(!pattern.is_match("name98"));
/// assert_eq!(pattern.as_str(), "(?i)name9+$");
/// assert!(Pattern::new("(").is_err());
/// ```
#[derive(Clone, Debug)]
pub struct Pattern {
    regex: fancy_regex::Regex,
}

impl Pattern {
    /// Compile `pattern`, or say why it is not one.
    pub fn new(pattern: &str) -> Result<Self, TransformError> {
        let regex = fancy_regex::Regex::new(pattern)
            .map_err(|e| TransformError::new(format!("invalid pattern {pattern:?}: {e}")))?;
        Ok(Pattern { regex })
    }

    /// Whether it matches anywhere in `text`; `false` too where the engine
    /// gives up (its backtrack limit), which [`try_is_match`](Self::try_is_match)
    /// reports instead.
    pub fn is_match(&self, text: &str) -> bool {
        self.regex.is_match(text).unwrap_or(false)
    }

    /// Whether it matches anywhere in `text`, or the engine's error.
    pub fn try_is_match(&self, text: &str) -> Result<bool, TransformError> {
        self.regex
            .is_match(text)
            .map_err(|e| TransformError::new(format!("match failed: {e}")))
    }

    /// The pattern as it was written.
    pub fn as_str(&self) -> &str {
        self.regex.as_str()
    }
}

/// Keeps the lines of a text that match a regular expression (or, inverted,
/// those that do not). Styles are kept, and the result ends with a newline
/// only if the input did.
#[derive(Clone, Debug)]
pub struct KeepLines {
    regex: fancy_regex::Regex,
    invert: bool,
}

impl KeepLines {
    /// Keep lines matching `pattern` anywhere.
    pub fn new(pattern: &str) -> Result<Self, TransformError> {
        let regex = fancy_regex::Regex::new(pattern)
            .map_err(|e| TransformError::new(format!("invalid pattern {pattern:?}: {e}")))?;
        Ok(KeepLines {
            regex,
            invert: false,
        })
    }

    /// Keep the lines that do *not* match instead.
    #[must_use]
    pub fn invert(mut self, invert: bool) -> Self {
        self.invert = invert;
        self
    }
}

impl TextTransform for KeepLines {
    fn transform(&self, text: Text) -> Result<Text, PluginError> {
        if text.plain().is_empty() {
            return Ok(text);
        }
        let mut kept = text.blank_copy();
        for line in text.split("\n", true, false) {
            let content = line.plain().strip_suffix('\n').unwrap_or(line.plain());
            let matched = self
                .regex
                .is_match(content)
                .map_err(|e| PluginError::Other(format!("filter failed: {e}")))?;
            if matched != self.invert {
                kept = kept.append_text(&line);
            }
        }
        // End the way the input did: a kept middle line brings its `\n`.
        if !text.plain().ends_with('\n') && kept.plain().ends_with('\n') {
            kept.right_crop(1);
        }
        Ok(kept)
    }
}

/// Styles every match of a regular expression.
///
/// Only whole matches are styled: unlike [`Text::highlight_regex`], a named
/// group is just a group here, never a style name. A match the regex engine
/// cannot finish (its backtrack limit) is an error, as it is for [`KeepLines`].
#[derive(Clone, Debug)]
pub struct HighlightMatches {
    regex: fancy_regex::Regex,
    style: Style,
}

impl HighlightMatches {
    pub fn new(pattern: &str, style: Style) -> Result<Self, TransformError> {
        let regex = fancy_regex::Regex::new(pattern)
            .map_err(|e| TransformError::new(format!("invalid pattern {pattern:?}: {e}")))?;
        Ok(HighlightMatches { regex, style })
    }
}

impl TextTransform for HighlightMatches {
    fn transform(&self, mut text: Text) -> Result<Text, PluginError> {
        let mut ranges = Vec::new();
        for found in self.regex.find_iter(text.plain()) {
            let found = found.map_err(|e| PluginError::Other(format!("highlight failed: {e}")))?;
            ranges.push(found.range());
        }
        // `Text` spans are byte offsets, the same units the regex reports.
        for range in ranges {
            text.stylize(self.style.clone(), range.start, range.end);
        }
        Ok(text)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn keep_lines_keeps_endings_and_styles() {
        let mut text = Text::new("one\ntwo\nthree");
        text.stylize(Style::parse("bold").unwrap(), 4, 7);
        let kept = KeepLines::new("t").unwrap().transform(text).unwrap();
        assert_eq!(kept.plain(), "two\nthree");
        assert_eq!(kept.spans().len(), 1);
        assert_eq!((kept.spans()[0].start, kept.spans()[0].end), (0, 3));

        let dropped = KeepLines::new("t")
            .unwrap()
            .invert(true)
            .transform(Text::new("one\ntwo\n"))
            .unwrap();
        assert_eq!(dropped.plain(), "one\n");
        let first = KeepLines::new("a")
            .unwrap()
            .transform(Text::new("a\nb"))
            .unwrap();
        assert_eq!(first.plain(), "a");
        assert_eq!(
            KeepLines::new("x")
                .unwrap()
                .transform(Text::new("a\nb"))
                .unwrap()
                .plain(),
            ""
        );
        assert!(KeepLines::new("(").is_err());
    }

    #[test]
    fn a_pattern_matches_plain_text_as_keep_lines_does() {
        let pattern = Pattern::new(r"(?<=a)b|^c").unwrap();
        assert!(pattern.is_match("ab"));
        assert!(pattern.is_match("cat"));
        assert!(!pattern.is_match("b c"));
        assert_eq!(pattern.try_is_match("xab"), Ok(true));
        let err = Pattern::new("[").unwrap_err();
        assert!(err.message().starts_with("invalid pattern"), "{err}");
    }

    #[test]
    fn highlight_matches_styles_each_match() {
        let text = HighlightMatches::new("o+", Style::parse("red").unwrap())
            .unwrap()
            .transform(Text::new("foo boo"))
            .unwrap();
        let ranges: Vec<_> = text.spans().iter().map(|s| (s.start, s.end)).collect();
        assert_eq!(ranges, [(1, 3), (5, 7)]);
    }

    #[test]
    fn highlight_matches_styles_whole_matches_only() {
        // A named group is not a style name here, unlike `highlight_regex`.
        let text = HighlightMatches::new("(?P<blink>ERR)OR", Style::parse("reverse").unwrap())
            .unwrap()
            .transform(Text::new("an ERROR"))
            .unwrap();
        assert_eq!(text.spans().len(), 1);
        assert_eq!((text.spans()[0].start, text.spans()[0].end), (3, 8));
    }

    #[test]
    fn highlight_matches_reports_the_backtrack_limit_like_keep_lines() {
        let input = || Text::new("a".repeat(30));
        let pattern = r"^(a|a)*\1b";
        assert!(KeepLines::new(pattern).unwrap().transform(input()).is_err());
        assert!(
            HighlightMatches::new(pattern, Style::parse("reverse").unwrap())
                .unwrap()
                .transform(input())
                .is_err()
        );
    }

    #[test]
    fn a_pipeline_runs_in_order_and_names_the_failing_stage() {
        let pipeline: Pipeline<Vec<u8>> = Pipeline::new()
            .then(
                "push",
                from_fn(|mut v: Vec<u8>| {
                    v.push(1);
                    Ok(v)
                }),
            )
            .then(
                "double",
                from_fn(|v: Vec<u8>| Ok(v.into_iter().map(|x| x * 2).collect())),
            );
        assert_eq!(pipeline.apply(vec![3]).unwrap(), [6, 2]);

        let failing = pipeline.then("fail", from_fn(|_: Vec<u8>| Err(TransformError::new("no"))));
        let error = failing.apply(Vec::new()).unwrap_err();
        assert_eq!(error.stage, "fail");
        assert_eq!(error.to_string(), "fail: no");
    }
}