Skip to content

Commit 9b7fd96

Browse files
authored
Merge pull request rust-num#238 from serde-rs/pubtuple
No tuple structs with private fields in public API
2 parents 6f8d5db + 9aef53b commit 9b7fd96

File tree

3 files changed

+113
-60
lines changed

3 files changed

+113
-60
lines changed

json/src/error.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use serde::ser;
1010

1111
/// This type represents all possible errors that can occur when serializing or
1212
/// deserializing JSON data.
13-
pub struct Error(Box<ErrorImpl>);
13+
pub struct Error {
14+
err: Box<ErrorImpl>,
15+
}
1416

1517
/// Alias for a `Result` with the error type `serde_json::Error`.
1618
pub type Result<T> = result::Result<T, Error>;
@@ -89,15 +91,17 @@ impl Error {
8991
// Not public API. Should be pub(crate).
9092
#[doc(hidden)]
9193
pub fn syntax(code: ErrorCode, line: usize, col: usize) -> Self {
92-
Error(Box::new(ErrorImpl::Syntax(code, line, col)))
94+
Error {
95+
err: Box::new(ErrorImpl::Syntax(code, line, col)),
96+
}
9397
}
9498

9599
// Not public API. Should be pub(crate).
96100
#[doc(hidden)]
97101
pub fn fix_position<F>(self, f: F) -> Self
98102
where F: FnOnce(ErrorCode) -> Error
99103
{
100-
if let ErrorImpl::Syntax(code, 0, 0) = *self.0 {
104+
if let ErrorImpl::Syntax(code, 0, 0) = *self.err {
101105
f(code)
102106
} else {
103107
self
@@ -169,14 +173,14 @@ impl Display for ErrorCode {
169173

170174
impl error::Error for Error {
171175
fn description(&self) -> &str {
172-
match *self.0 {
176+
match *self.err {
173177
ErrorImpl::Syntax(..) => "syntax error",
174178
ErrorImpl::Io(ref error) => error::Error::description(error),
175179
}
176180
}
177181

178182
fn cause(&self) -> Option<&error::Error> {
179-
match *self.0 {
183+
match *self.err {
180184
ErrorImpl::Io(ref error) => Some(error),
181185
_ => None,
182186
}
@@ -185,7 +189,7 @@ impl error::Error for Error {
185189

186190
impl Display for Error {
187191
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
188-
match *self.0 {
192+
match *self.err {
189193
ErrorImpl::Syntax(ref code, line, col) => {
190194
if line == 0 && col == 0 {
191195
write!(fmt, "{}", code)
@@ -202,7 +206,7 @@ impl Display for Error {
202206
// end up seeing this representation because it is what unwrap() shows.
203207
impl Debug for Error {
204208
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
205-
match *self.0 {
209+
match *self.err {
206210
ErrorImpl::Syntax(ref code, ref line, ref col) => {
207211
formatter.debug_tuple("Syntax")
208212
.field(code)
@@ -221,30 +225,40 @@ impl Debug for Error {
221225

222226
impl From<ErrorImpl> for Error {
223227
fn from(error: ErrorImpl) -> Error {
224-
Error(Box::new(error))
228+
Error {
229+
err: Box::new(error),
230+
}
225231
}
226232
}
227233

228234
impl From<io::Error> for Error {
229235
fn from(error: io::Error) -> Error {
230-
Error(Box::new(ErrorImpl::Io(error)))
236+
Error {
237+
err: Box::new(ErrorImpl::Io(error)),
238+
}
231239
}
232240
}
233241

234242
impl From<de::value::Error> for Error {
235243
fn from(error: de::value::Error) -> Error {
236-
Error(Box::new(ErrorImpl::Syntax(ErrorCode::Message(error.to_string()), 0, 0)))
244+
Error {
245+
err: Box::new(ErrorImpl::Syntax(ErrorCode::Message(error.to_string()), 0, 0)),
246+
}
237247
}
238248
}
239249

240250
impl de::Error for Error {
241251
fn custom<T: Display>(msg: T) -> Error {
242-
Error(Box::new(ErrorImpl::Syntax(ErrorCode::Message(msg.to_string()), 0, 0)))
252+
Error {
253+
err: Box::new(ErrorImpl::Syntax(ErrorCode::Message(msg.to_string()), 0, 0)),
254+
}
243255
}
244256
}
245257

246258
impl ser::Error for Error {
247259
fn custom<T: Display>(msg: T) -> Error {
248-
Error(Box::new(ErrorImpl::Syntax(ErrorCode::Message(msg.to_string()), 0, 0)))
260+
Error {
261+
err: Box::new(ErrorImpl::Syntax(ErrorCode::Message(msg.to_string()), 0, 0)),
262+
}
249263
}
250264
}

0 commit comments

Comments
 (0)