|
| 1 | +use std::fmt; |
| 2 | + |
| 3 | +/// Represents all document formats that can participate in transformation edges. |
| 4 | +/// |
| 5 | +/// Formats are modelled as graph nodes: each variant is a unique node in the |
| 6 | +/// [`TransformGraph`](super::TransformGraph). An edge between two nodes |
| 7 | +/// indicates that a [`TransformEdge`](super::TransformEdge) exists that can |
| 8 | +/// convert from the source format to the target format. |
| 9 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 10 | +pub enum Format { |
| 11 | + Markdown, |
| 12 | + Html, |
| 13 | + Pdf, |
| 14 | + Docx, |
| 15 | + Epub, |
| 16 | + Rst, |
| 17 | + Latex, |
| 18 | +} |
| 19 | + |
| 20 | +impl fmt::Display for Format { |
| 21 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 22 | + let s = match self { |
| 23 | + Format::Markdown => "markdown", |
| 24 | + Format::Html => "html", |
| 25 | + Format::Pdf => "pdf", |
| 26 | + Format::Docx => "docx", |
| 27 | + Format::Epub => "epub", |
| 28 | + Format::Rst => "rst", |
| 29 | + Format::Latex => "latex", |
| 30 | + }; |
| 31 | + write!(f, "{}", s) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +#[cfg(test)] |
| 36 | +mod tests { |
| 37 | + use super::*; |
| 38 | + |
| 39 | + #[test] |
| 40 | + fn test_display_markdown() { |
| 41 | + assert_eq!(Format::Markdown.to_string(), "markdown"); |
| 42 | + } |
| 43 | + |
| 44 | + #[test] |
| 45 | + fn test_display_all_variants() { |
| 46 | + assert_eq!(Format::Html.to_string(), "html"); |
| 47 | + assert_eq!(Format::Pdf.to_string(), "pdf"); |
| 48 | + assert_eq!(Format::Docx.to_string(), "docx"); |
| 49 | + assert_eq!(Format::Epub.to_string(), "epub"); |
| 50 | + assert_eq!(Format::Rst.to_string(), "rst"); |
| 51 | + assert_eq!(Format::Latex.to_string(), "latex"); |
| 52 | + } |
| 53 | + |
| 54 | + #[test] |
| 55 | + fn test_format_equality() { |
| 56 | + assert_eq!(Format::Markdown, Format::Markdown); |
| 57 | + assert_ne!(Format::Markdown, Format::Html); |
| 58 | + } |
| 59 | + |
| 60 | + #[test] |
| 61 | + fn test_format_clone_copy() { |
| 62 | + let f = Format::Pdf; |
| 63 | + let g = f; |
| 64 | + assert_eq!(f, g); |
| 65 | + } |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn test_format_hash() { |
| 69 | + use std::collections::HashSet; |
| 70 | + let mut set = HashSet::new(); |
| 71 | + set.insert(Format::Markdown); |
| 72 | + set.insert(Format::Markdown); |
| 73 | + set.insert(Format::Html); |
| 74 | + assert_eq!(set.len(), 2); |
| 75 | + } |
| 76 | +} |
0 commit comments