flams_ontology/narration/
sections.rs1use std::fmt::Display;
2
3use crate::{uris::DocumentElementURI, Checked, CheckingState, DocumentRange};
4
5use super::{DocumentElement, NarrationTrait};
6
7#[derive(Debug)]
20pub struct Section<State: CheckingState> {
21 pub range: DocumentRange,
22 pub uri: DocumentElementURI,
23 pub level: SectionLevel,
24 pub title: Option<DocumentRange>,
25 pub children: State::Seq<DocumentElement<State>>,
26}
27
28crate::serde_impl! {
29 struct Section[range,uri,level,title,children]
30}
31
32impl NarrationTrait for Section<Checked> {
33 #[inline]
34 fn children(&self) -> &[DocumentElement<Checked>] {
35 &self.children
36 }
37
38 #[inline]
39 fn from_element(e: &DocumentElement<Checked>) -> Option<&Self>
40 where
41 Self: Sized,
42 {
43 if let DocumentElement::Section(e) = e {
44 Some(e)
45 } else {
46 None
47 }
48 }
49}
50
51#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
52#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
53#[cfg_attr(feature = "wasm", derive(tsify_next::Tsify))]
54#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
55pub enum SectionLevel {
56 Part,
57 Chapter,
58 Section,
59 Subsection,
60 Subsubsection,
61 Paragraph,
62 Subparagraph,
63}
64impl Ord for SectionLevel {
65 #[inline]
66 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
67 let su: u8 = (*self).into();
68 let ou: u8 = (*other).into();
69 su.cmp(&ou)
70 }
71}
72impl PartialOrd for SectionLevel {
73 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
74 Some(self.cmp(other))
75 }
76}
77impl SectionLevel {
78 #[must_use]
79 pub const fn inc(self) -> Self {
80 match self {
81 Self::Part => Self::Chapter,
82 Self::Chapter => Self::Section,
83 Self::Section => Self::Subsection,
84 Self::Subsection => Self::Subsubsection,
85 Self::Subsubsection => Self::Paragraph,
86 _ => Self::Subparagraph,
87 }
88 }
89}
90impl Display for SectionLevel {
91 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92 use SectionLevel::*;
93 write!(
94 f,
95 "{}",
96 match self {
97 Part => "Part",
98 Chapter => "Chapter",
99 Section => "Section",
100 Subsection => "Subsection",
101 Subsubsection => "Subsubsection",
102 Paragraph => "Paragraph",
103 Subparagraph => "Subparagraph",
104 }
105 )
106 }
107}
108impl TryFrom<u8> for SectionLevel {
109 type Error = ();
110 fn try_from(value: u8) -> Result<Self, ()> {
111 use SectionLevel::*;
112 match value {
113 0 => Ok(Part),
114 1 => Ok(Chapter),
115 2 => Ok(Section),
116 3 => Ok(Subsection),
117 4 => Ok(Subsubsection),
118 5 => Ok(Paragraph),
119 6 => Ok(Subparagraph),
120 _ => Err(()),
121 }
122 }
123}
124impl From<SectionLevel> for u8 {
125 fn from(s: SectionLevel) -> Self {
126 use SectionLevel::*;
127 match s {
128 Part => 0,
129 Chapter => 1,
130 Section => 2,
131 Subsection => 3,
132 Subsubsection => 4,
133 Paragraph => 5,
134 Subparagraph => 6,
135 }
136 }
137}