Skip to main content

iced_core/text/
parser.rs

1//! Parse text.
2use std::ops::Range;
3
4/// A type capable of parsing text, producing some `Output`.
5///
6/// A [`Parser`] parses lines in sequence. When a line changes,
7/// it must be notified and the lines after the changed one must be fed
8/// again to the [`Parser`].
9pub trait Parser: 'static {
10    /// The settings to configure the [`Parser`].
11    type Settings: PartialEq + Clone;
12
13    /// The output of this [`Parser`].
14    type Output;
15
16    /// The parse iterator type.
17    type Iterator<'a>: Iterator<Item = (Range<usize>, Self::Output)>
18    where
19        Self: 'a;
20
21    /// Creates a new [`Parser`] from its [`Self::Settings`].
22    fn new(settings: &Self::Settings) -> Self;
23
24    /// Updates the [`Parser`] with some new [`Self::Settings`].
25    fn update(&mut self, new_settings: &Self::Settings);
26
27    /// Notifies the [`Parser`] that the line at the given index has changed.
28    fn change_line(&mut self, line: usize);
29
30    /// Parses the given line.
31    ///
32    /// If a line changed prior to this, the first line provided here will be the
33    /// line that changed.
34    fn parse_line(&mut self, line: &str) -> Self::Iterator<'_>;
35
36    /// Returns the current line of the [`Parser`].
37    ///
38    /// If `change_line` has been called, this will normally be the least index
39    /// that changed.
40    fn current_line(&self) -> usize;
41}
42
43/// A parser that produces no output.
44#[derive(Debug, Clone, Copy)]
45pub struct PlainText;
46
47impl Parser for PlainText {
48    type Settings = ();
49    type Output = ();
50
51    type Iterator<'a> = std::iter::Empty<(Range<usize>, ())>;
52
53    fn new(_settings: &Self::Settings) -> Self {
54        Self
55    }
56
57    fn update(&mut self, _new_settings: &Self::Settings) {}
58
59    fn change_line(&mut self, _line: usize) {}
60
61    fn parse_line(&mut self, _line: &str) -> Self::Iterator<'_> {
62        std::iter::empty()
63    }
64
65    fn current_line(&self) -> usize {
66        usize::MAX
67    }
68}