1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use core::fmt;
use std::fmt::{Display, Formatter};
use std::io::{Cursor, Write};
use crate::xdoc::error::{Result, XDocErr};
use crate::xdoc::WriteOpts;
#[derive(Debug, Clone, Eq, PartialOrd, PartialEq, Ord, Hash, Default)]
pub struct Pi {
target: String,
data: String,
}
impl Pi {
pub fn new<S1, S2>(target: S1, data: S2) -> Result<Self>
where
S1: Into<String> + AsRef<str>,
S2: Into<String> + AsRef<str>,
{
if !pi_str_ok(target.as_ref()) {
return Err(XDocErr {
message: format!(
"invalid processing instruction target '{}'",
target.as_ref()
),
file: file!().into(),
line: line!() as u64,
source: None,
});
}
if !pi_str_ok(data.as_ref()) {
return Err(XDocErr {
message: format!("invalid processing instruction data '{}'", target.as_ref()),
file: file!().into(),
line: line!() as u64,
source: None,
});
}
Ok(Self::new_unchecked(target, data))
}
pub fn target(&self) -> &String {
&self.target
}
pub fn data(&self) -> &String {
&self.data
}
pub fn write<W>(&self, writer: &mut W, opts: &WriteOpts, depth: usize) -> Result<()>
where
W: Write,
{
opts.indent(writer, depth)?;
xwrite!(writer, "<?{}", &self.target)?;
if !self.data.is_empty() {
xwrite!(writer, " {}", self.data)?;
}
xwrite!(writer, "?>")?;
Ok(())
}
pub(crate) fn new_unchecked<S1, S2>(target: S1, data: S2) -> Self
where
S1: Into<String>,
S2: Into<String>,
{
Self {
target: target.into(),
data: data.into(),
}
}
}
fn pi_str_ok<S: AsRef<str>>(s: S) -> bool {
!s.as_ref().contains("?>")
}
impl Display for Pi {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut c = Cursor::new(Vec::new());
if self.write(&mut c, &WriteOpts::default(), 0).is_err() {
return write!(f, "<?error?>");
}
let data = c.into_inner();
let data_str = std::str::from_utf8(data.as_slice()).unwrap_or("<?error?>");
write!(f, "{}", data_str)
}
}
#[test]
fn pi_test_simple() {
let pi = Pi::new("thetarget", "dat1 dat2").unwrap();
let got = pi.to_string();
let want = "<?thetarget dat1 dat2?>";
assert_eq!(got, want);
}
#[test]
fn pi_test_empty() {
let pi = Pi::new("x", "").unwrap();
let got = pi.to_string();
let want = "<?x?>";
assert_eq!(got, want);
}
#[test]
fn pi_test_bad() {
let result = Pi::new("x", "da?>t1");
assert!(result.is_err());
}