parsoid/
immutable.rs

1/*
2Copyright (C) 2021 Kunal Mehta <legoktm@debian.org>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 3 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18use crate::private::Sealed;
19use crate::Wikicode;
20
21/// An immutable version of `Wikicode` that implements [`Send`](https://doc.rust-lang.org/std/marker/trait.Send.html)
22/// and [`Sync`](https://doc.rust-lang.org/std/marker/trait.Sync.html). It
23/// can be used interchangably with a normal `Wikicode` for API methods.
24///
25/// You can also use `From`/`Into` in both directions between `Wikicode`
26/// and `ImmutableWikicode`.
27#[derive(Clone, Debug)]
28pub struct ImmutableWikicode {
29    pub(crate) html: String,
30    pub(crate) title: Option<String>,
31    pub(crate) etag: Option<String>,
32    pub(crate) revid: Option<u64>,
33}
34
35impl ImmutableWikicode {
36    pub fn new(html: &str) -> Self {
37        Self {
38            html: html.to_string(),
39            title: None,
40            etag: None,
41            revid: None,
42        }
43    }
44
45    pub fn html(&self) -> &str {
46        &self.html
47    }
48
49    pub fn title(&self) -> Option<String> {
50        self.title.clone()
51    }
52
53    pub fn etag(&self) -> Option<&str> {
54        self.etag.as_deref()
55    }
56
57    pub fn revision_id(&self) -> Option<u64> {
58        self.revid
59    }
60
61    pub fn into_mutable(self) -> Wikicode {
62        self.into()
63    }
64}
65
66impl From<Wikicode> for ImmutableWikicode {
67    fn from(code: Wikicode) -> Self {
68        Self {
69            html: code.to_string(),
70            revid: code.revision_id(),
71            title: code.title,
72            etag: code.etag,
73        }
74    }
75}
76
77impl From<ImmutableWikicode> for Wikicode {
78    fn from(immutable: ImmutableWikicode) -> Self {
79        let mut code = Wikicode::new(&immutable.html);
80        code.etag = immutable.etag;
81        code.title = immutable.title;
82        code
83    }
84}
85
86impl Sealed for ImmutableWikicode {}
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91
92    fn assert_sync_send<T: Sync + Send>() {}
93
94    #[test]
95    fn test_immutable() {
96        assert_sync_send::<ImmutableWikicode>();
97    }
98}