ephemeris_documents/yaml/source/
opaque.rs1use std::fmt;
4
5use super::mapping::Mapping;
6use super::text::{Origin, Reindent, Text};
7
8#[derive(Debug, Clone, PartialEq, Eq)]
13pub(crate) struct Opaque {
14 text: Text,
15 rewrite: Option<Rewrite>,
16}
17
18#[derive(Debug, Clone, PartialEq, Eq)]
19struct Rewrite {
20 original: Mapping,
21 edited: Mapping,
22}
23
24impl Opaque {
25 pub(super) const fn raw(text: Text) -> Self {
27 Self {
28 text,
29 rewrite: None,
30 }
31 }
32
33 pub(super) fn flow(text: Text, block: Mapping) -> Self {
35 Self {
36 text,
37 rewrite: Some(Rewrite {
38 original: block.clone(),
39 edited: block,
40 }),
41 }
42 }
43
44 pub(super) fn mapping(&self) -> Option<&Mapping> {
46 self.rewrite.as_ref().map(|rewrite| &rewrite.edited)
47 }
48
49 pub(super) fn mapping_mut(&mut self) -> Option<&mut Mapping> {
50 self.rewrite.as_mut().map(|rewrite| &mut rewrite.edited)
51 }
52
53 pub(super) const fn is_unsupported(&self) -> bool {
55 self.rewrite.is_none()
56 }
57
58 pub(super) fn comment(&self) -> Option<&str> {
60 self.mapping()?.comment()
61 }
62
63 pub(super) fn terminate(&mut self) {
65 self.text.terminate();
66 if let Some(rewrite) = &mut self.rewrite {
67 rewrite.original.terminate();
68 rewrite.edited.terminate();
69 }
70 }
71
72 pub(super) fn reindent(&mut self, shift: Reindent) {
74 self.text.reindent(shift, Origin::MidLine);
75 }
76}
77
78impl fmt::Display for Opaque {
79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80 match &self.rewrite {
81 Some(rewrite) if rewrite.edited != rewrite.original => rewrite.edited.fmt(f),
82 Some(_) | None => self.text.fmt(f),
83 }
84 }
85}