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
use xmlparser::StrSpan;

use super::MjPreview;
#[cfg(feature = "async")]
use crate::prelude::parser::{AsyncMrmlParser, AsyncParseElement};
use crate::prelude::parser::{Error, MrmlCursor, MrmlParser, ParseElement};

#[inline]
fn parse(cursor: &mut MrmlCursor<'_>) -> Result<MjPreview, Error> {
    let ending = cursor.assert_element_end()?;
    if ending.empty {
        return Ok(MjPreview::default());
    }

    let text = cursor.next_text()?.map(|inner| inner.text.to_string());

    cursor.assert_element_close()?;

    Ok(MjPreview {
        children: text.unwrap_or_default(),
    })
}

impl<'opts> ParseElement<MjPreview> for MrmlParser<'opts> {
    fn parse<'a>(
        &self,
        cursor: &mut MrmlCursor<'a>,
        _tag: StrSpan<'a>,
    ) -> Result<MjPreview, Error> {
        parse(cursor)
    }
}

#[cfg(feature = "async")]
#[async_trait::async_trait(?Send)]
impl AsyncParseElement<MjPreview> for AsyncMrmlParser {
    async fn async_parse<'a>(
        &self,
        cursor: &mut MrmlCursor<'a>,
        _tag: StrSpan<'a>,
    ) -> Result<MjPreview, Error> {
        parse(cursor)
    }
}

#[cfg(test)]
mod tests {
    use crate::mj_preview::MjPreview;

    crate::should_sync_parse!(
        should_parse,
        MjPreview,
        "<mj-preview>Hello World!</mj-preview>"
    );
    crate::should_sync_parse!(should_parse_without_children, MjPreview, "<mj-preview />");
}