1use crate::Format;
10
11#[derive(Debug, thiserror::Error)]
13pub enum Error {
14 #[error("invalid ADF JSON: {0}")]
16 Json(#[from] serde_json::Error),
17
18 #[error("expected an ADF node of type \"doc\", found {found:?}")]
20 NotAdfDoc { found: String },
21
22 #[error("malformed {format} input: {detail}")]
25 Malformed { format: Format, detail: String },
26
27 #[error("parsing {0} is not implemented yet")]
30 UnsupportedSource(Format),
31
32 #[error("rendering {0} is not implemented yet")]
34 UnsupportedTarget(Format),
35}
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub enum WarningKind {
40 Lossy,
43 Dropped,
46 Escaped,
50}
51
52impl std::fmt::Display for WarningKind {
53 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54 f.write_str(match self {
55 WarningKind::Lossy => "lossy",
56 WarningKind::Dropped => "dropped",
57 WarningKind::Escaped => "escaped",
58 })
59 }
60}
61
62#[derive(Debug, Clone, PartialEq, Eq)]
68pub struct Warning {
69 pub kind: WarningKind,
70 pub construct: String,
73 pub format: Format,
75 pub detail: Option<String>,
77}
78
79impl Warning {
80 pub fn lossy(construct: impl Into<String>, format: Format) -> Self {
81 Self::new(WarningKind::Lossy, construct, format)
82 }
83
84 pub fn dropped(construct: impl Into<String>, format: Format) -> Self {
85 Self::new(WarningKind::Dropped, construct, format)
86 }
87
88 pub fn escaped(construct: impl Into<String>, format: Format) -> Self {
89 Self::new(WarningKind::Escaped, construct, format)
90 }
91
92 fn new(kind: WarningKind, construct: impl Into<String>, format: Format) -> Self {
93 Self {
94 kind,
95 construct: construct.into(),
96 format,
97 detail: None,
98 }
99 }
100
101 pub fn with_detail(mut self, detail: impl Into<String>) -> Self {
103 self.detail = Some(detail.into());
104 self
105 }
106}
107
108impl std::fmt::Display for Warning {
109 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
110 write!(f, "{}: {} in {}", self.kind, self.construct, self.format)?;
111 if let Some(detail) = &self.detail {
112 write!(f, " ({detail})")?;
113 }
114 Ok(())
115 }
116}
117
118#[cfg(test)]
119mod tests {
120 use super::*;
121
122 #[test]
123 fn warnings_describe_themselves() {
124 let warning = Warning::lossy("table alignment", Format::Adf)
125 .with_detail("ADF has no per-column alignment");
126 assert_eq!(
127 warning.to_string(),
128 "lossy: table alignment in ADF (ADF has no per-column alignment)"
129 );
130 }
131
132 #[test]
133 fn detail_is_optional() {
134 let warning = Warning::escaped("adf:panel", Format::JiraWiki);
135 assert_eq!(
136 warning.to_string(),
137 "escaped: adf:panel in Jira wiki markup"
138 );
139 }
140}