|
6 | 6 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
7 | 7 | // option. This file may not be copied, modified, or distributed
|
8 | 8 | // except according to those terms.
|
| 9 | +//! Traits for serializing elements. The serializer expects the data to be xml-like (with a name, |
| 10 | +//! and optional children, attrs, text, comments, doctypes, and [processing instructions]). It uses |
| 11 | +//! the visitor pattern, where the serializer and the serializable objects are decoupled and |
| 12 | +//! implement their own traits. |
| 13 | +//! |
| 14 | +//! [processing instructions]: https://en.wikipedia.org/wiki/Processing_Instruction |
9 | 15 |
|
10 | 16 | use QualName;
|
11 | 17 | use std::io;
|
12 | 18 |
|
13 | 19 | //§ serializing-html-fragments
|
| 20 | +/// Used as a parameter to `serialize`, telling it if we want to skip the parent. |
| 21 | +/// TODO check: Currently the parameter to `ChildrenOnly` is unused. |
14 | 22 | #[derive(Clone, PartialEq)]
|
15 | 23 | pub enum TraversalScope {
|
| 24 | + /// Include the parent node when serializing. |
16 | 25 | IncludeNode,
|
| 26 | + /// Only serialize the children of the node. |
17 | 27 | ChildrenOnly(Option<QualName>)
|
18 | 28 | }
|
19 | 29 |
|
| 30 | +/// Types that can be serialized (according to the xml-like scheme in `Serializer`) implement this |
| 31 | +/// trait. |
20 | 32 | pub trait Serialize {
|
| 33 | + /// Take the serializer and call its methods to serialize this type. The type will dictate |
| 34 | + /// which methods are called and with what parameters. |
21 | 35 | fn serialize<S>(&self, serializer: &mut S, traversal_scope: TraversalScope) -> io::Result<()>
|
22 | 36 | where S: Serializer;
|
23 | 37 | }
|
24 | 38 |
|
| 39 | +/// Types that are capable of serializing implement this trait |
25 | 40 | pub trait Serializer {
|
| 41 | + /// Serialize the start of an element, for example `<div class="test">`. |
26 | 42 | fn start_elem<'a, AttrIter>(&mut self, name: QualName, attrs: AttrIter) -> io::Result<()>
|
27 | 43 | where AttrIter: Iterator<Item=AttrRef<'a>>;
|
28 | 44 |
|
| 45 | + /// Serialize the end of an element, for example `</div>`. |
29 | 46 | fn end_elem(&mut self, name: QualName) -> io::Result<()>;
|
30 | 47 |
|
| 48 | + /// Serialize a plain text node. |
31 | 49 | fn write_text(&mut self, text: &str) -> io::Result<()>;
|
32 | 50 |
|
| 51 | + /// Serialize a comment node, for example `<!-- comment -->`. |
33 | 52 | fn write_comment(&mut self, text: &str) -> io::Result<()>;
|
34 | 53 |
|
| 54 | + /// Serialize a doctype node, for example `<!doctype html>`. |
35 | 55 | fn write_doctype(&mut self, name: &str) -> io::Result<()>;
|
36 | 56 |
|
| 57 | + /// Serialize a processing instruction node, for example |
| 58 | + /// `<?xml-stylesheet type="text/xsl" href="style.xsl"?>`. |
37 | 59 | fn write_processing_instruction(&mut self, target: &str, data: &str) -> io::Result<()>;
|
38 | 60 | }
|
39 | 61 |
|
| 62 | +/// A type alias for an attribute name and value (e.g. the `class="test"` in `<div class="test">` |
| 63 | +/// is represented as `(<QualName of type class>, test). |
| 64 | +/// |
| 65 | +/// This is used in [`Serializer::start_elem`] where the value being serialized must supply an |
| 66 | +/// iterator over the attributes for the current element |
40 | 67 | pub type AttrRef<'a> = (&'a QualName, &'a str);
|
0 commit comments