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
//! Plugins allow changing TextBox behaviour.
//!
//! Note: Custom plugins are experimental. If you wish to implement custom plugins,
//! you need to activate the `plugin` feature.

use core::{
    cell::UnsafeCell,
    hash::{Hash, Hasher},
    marker::PhantomData,
    ptr::addr_of,
};
use embedded_graphics::{
    draw_target::DrawTarget,
    prelude::PixelColor,
    primitives::Rectangle,
    text::renderer::{CharacterStyle, TextRenderer},
};

use crate::{
    parser::{Parser, Token},
    rendering::{cursor::Cursor, TextBoxProperties},
};

#[cfg(feature = "plugin")]
pub mod private;
#[cfg(feature = "plugin")]
pub use private::Plugin;

#[cfg(not(feature = "plugin"))]
mod private;
#[cfg(not(feature = "plugin"))]
use private::Plugin;

#[cfg(feature = "ansi")]
pub mod ansi;
pub mod tail;

#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub(crate) enum ProcessingState {
    Measure,
    Render,
}

/// Placeholder type when no plugin is used.
#[derive(Clone, Copy, Default)]
pub struct NoPlugin<C>(PhantomData<C>)
where
    C: PixelColor;

impl<C> NoPlugin<C>
where
    C: PixelColor,
{
    pub(crate) const fn new() -> Self {
        Self(PhantomData)
    }
}

/// Plugin marker trait.
///
/// This trait is an implementation detail. Most likely you don't need to implement this.
#[cfg_attr(
    feature = "plugin",
    doc = "If you wish to implement a plugin, see [Plugin]."
)]
// TODO: remove this trait once Plugin is stabilized, then move Plugin here
pub trait PluginMarker<'a, C: PixelColor>: Plugin<'a, C> {}

impl<'a, C, T> PluginMarker<'a, C> for T
where
    T: Plugin<'a, C>,
    C: PixelColor,
{
}

#[derive(Clone, Debug)]
pub(crate) struct PluginInner<'a, M, C> {
    plugin: M,
    state: ProcessingState,
    peeked_token: Option<Token<'a, C>>,
}

#[derive(Debug)]
pub(crate) struct PluginWrapper<'a, M, C> {
    inner: UnsafeCell<PluginInner<'a, M, C>>,
}

impl<'a, M: Clone, C: Clone> Clone for PluginWrapper<'a, M, C> {
    fn clone(&self) -> Self {
        Self {
            inner: UnsafeCell::new(self.with(|this| PluginInner {
                plugin: this.plugin.clone(),
                state: this.state,
                peeked_token: unsafe { addr_of!(this.peeked_token).read() },
            })),
        }
    }
}

impl<'a, M, C> Hash for PluginWrapper<'a, M, C>
where
    C: PixelColor,
{
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.with(|this| this.state.hash(state))
    }
}

impl<'a, M, C> PluginWrapper<'a, M, C> {
    pub fn new(plugin: M) -> Self {
        Self {
            inner: UnsafeCell::new(PluginInner {
                plugin,
                state: ProcessingState::Measure,
                peeked_token: None,
            }),
        }
    }

    pub fn into_inner(self) -> M {
        self.inner.into_inner().plugin
    }

    fn with<R>(&self, cb: impl FnOnce(&PluginInner<'a, M, C>) -> R) -> R {
        let inner = unsafe {
            // SAFETY: This is safe because we aren't exposing the reference.
            self.inner.get().as_ref().unwrap_unchecked()
        };

        cb(inner)
    }

    fn with_mut<R>(&self, cb: impl FnOnce(&mut PluginInner<'a, M, C>) -> R) -> R {
        let inner = unsafe {
            // SAFETY: This is safe because we aren't exposing the reference.
            self.inner.get().as_mut().unwrap_unchecked()
        };

        cb(inner)
    }
}

impl<'a, M, C> PluginWrapper<'a, M, C>
where
    C: PixelColor,
    M: private::Plugin<'a, C>,
{
    pub fn new_line(&self) {
        self.with_mut(|this| this.plugin.new_line());
    }

    pub fn set_state(&self, state: ProcessingState) {
        self.with_mut(|this| this.state = state);
    }

    #[inline]
    pub fn render_token(&self, token: Token<'a, C>) -> Option<Token<'a, C>> {
        self.with_mut(|this| match this.state {
            ProcessingState::Measure => Some(token),
            ProcessingState::Render => this.plugin.render_token(token),
        })
    }

    pub fn peek_token(&self, source: &mut Parser<'a, C>) -> Option<Token<'a, C>> {
        self.with_mut(|this| {
            if this.peeked_token.is_none() {
                this.peeked_token = this.plugin.next_token(|| source.next());
            }

            this.peeked_token.clone()
        })
    }

    pub fn consume_peeked_token(&self) {
        self.with_mut(|this| this.peeked_token = None);
    }

    pub fn consume_partial(&self, len: usize) {
        self.with_mut(|this| {
            // Only string-like tokens can be partially consumed.
            debug_assert!(matches!(
                this.peeked_token,
                Some(Token::Whitespace(_, _)) | Some(Token::Word(_))
            ));

            let skip_chars = |str: &'a str, n| {
                let mut chars = str.chars();
                for _ in 0..n {
                    chars.next();
                }
                chars.as_str()
            };

            if let Some(token) = this.peeked_token.take() {
                let token = match token {
                    Token::Whitespace(count, seq) => {
                        Token::Whitespace(count - len as u32, skip_chars(seq, len))
                    }
                    Token::Word(w) => Token::Word(skip_chars(w, len)),
                    _ => return,
                };

                this.peeked_token.replace(token);
            }
        })
    }

    pub fn on_start_render<S: CharacterStyle + TextRenderer>(
        &self,
        cursor: &mut Cursor,
        props: TextBoxProperties<'_, S>,
    ) {
        self.with_mut(|this| {
            this.peeked_token = None;

            this.plugin.on_start_render(cursor, &props);
        });
    }

    pub fn on_rendering_finished(&self) {
        self.with_mut(|this| this.plugin.on_rendering_finished());
    }

    pub fn post_render<T, D>(
        &self,
        draw_target: &mut D,
        character_style: &T,
        text: Option<&str>,
        bounds: Rectangle,
    ) -> Result<(), D::Error>
    where
        T: TextRenderer<Color = C>,
        D: DrawTarget<Color = C>,
    {
        self.with_mut(|this| {
            this.plugin
                .post_render(draw_target, character_style, text, bounds)
        })
    }
}