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
143
144
145
146
147
148
149
150
151
use core::fmt;
use core::mem;
use {Descriptor, Encoding};
use super::{is_valid, parse, ParseResult};
use super::multi::StrEncodings;
#[derive(Clone, Copy, Debug)]
pub struct ParseEncodingError<S>(S) where S: AsRef<str>;
impl<S> fmt::Display for ParseEncodingError<S> where S: AsRef<str> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "Invalid encoding: {:?}", self.0.as_ref())
}
}
#[derive(Clone, Copy, Debug)]
pub struct StrEncoding<S: ?Sized = str>(S) where S: AsRef<str>;
impl StrEncoding {
pub fn from_str(s: &str) -> Result<&StrEncoding, ParseEncodingError<&str>> {
if is_valid(s) {
Ok(StrEncoding::from_str_unchecked(s))
} else {
Err(ParseEncodingError(s))
}
}
pub fn from_str_unchecked(s: &str) -> &StrEncoding {
unsafe { mem::transmute(s) }
}
}
impl<S> StrEncoding<S> where S: AsRef<str> {
pub fn new(s: S) -> Result<StrEncoding<S>, ParseEncodingError<S>> {
if is_valid(s.as_ref()) {
Ok(StrEncoding::new_unchecked(s))
} else {
Err(ParseEncodingError(s))
}
}
pub fn new_unchecked(s: S) -> StrEncoding<S> {
StrEncoding(s)
}
}
impl<S: ?Sized> StrEncoding<S> where S: AsRef<str> {
pub fn as_str(&self) -> &str {
self.0.as_ref()
}
}
impl<S: ?Sized> Encoding for StrEncoding<S> where S: AsRef<str> {
type PointerTarget = StrEncoding;
type ArrayItem = StrEncoding;
type StructFields = StrEncodings;
type UnionMembers = StrEncodings;
fn descriptor(&self) -> Descriptor<StrEncoding, StrEncoding, StrEncodings, StrEncodings> {
let s = self.as_str();
match parse(s) {
ParseResult::Primitive(p) => Descriptor::Primitive(p),
ParseResult::Pointer(t) =>
Descriptor::Pointer(StrEncoding::from_str_unchecked(t)),
ParseResult::Array(len, item) =>
Descriptor::Array(len, StrEncoding::from_str_unchecked(item)),
ParseResult::Struct(name, fields) =>
Descriptor::Struct(name, StrEncodings::from_str_unchecked(fields)),
ParseResult::Union(name, members) =>
Descriptor::Union(name, StrEncodings::from_str_unchecked(members)),
ParseResult::Error => panic!("Failed to parse an encoding from {:?}", s),
}
}
fn write<W: fmt::Write>(&self, writer: &mut W) -> fmt::Result {
writer.write_str(self.as_str())
}
}
impl<S: ?Sized> fmt::Display for StrEncoding<S> where S: AsRef<str> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
self.write(formatter)
}
}
impl<S: ?Sized, E: ?Sized> PartialEq<E> for StrEncoding<S>
where S: AsRef<str>, E: Encoding {
fn eq(&self, other: &E) -> bool {
self.eq_encoding(other)
}
}
#[cfg(test)]
mod tests {
use encoding::{Array, Primitive, Struct};
use super::*;
#[test]
fn test_parsed_array() {
let a = StrEncoding::from_str_unchecked("[12i]");
assert!(a == &Array::new(12, Primitive::Int));
}
#[test]
fn test_parsed_struct() {
let parsed = StrEncoding::from_str_unchecked("{CGPoint=ci}");
let expected = Struct::new("CGPoint", (Primitive::Char, Primitive::Int));
assert!(parsed == &expected);
}
}