Skip to content

Commit 9241d44

Browse files
bors[bot]gbip
andcommitted
7: Bump heapless & serde version r=japaric a=gbip Hi, I bumped `serde`, `serde_derive` and `heapless` versions. I also implemented `Display` for both error types. Feel free to tell me if there is anything that need to be changed. Co-authored-by: Paul Florence <perso@florencepaul.com>
2 parents a2c4cf0 + 46faaf2 commit 9241d44

File tree

5 files changed

+74
-46
lines changed

5 files changed

+74
-46
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ name = "serde-json-core"
44
version = "0.1.0"
55

66
[dependencies]
7-
heapless = "0.2.4"
7+
heapless = "0.4.0"
88

99
[dependencies.serde]
1010
default-features = false
11-
version = "1.0.33"
11+
version = "1.0.80"
1212

1313
[dev-dependencies]
14-
serde_derive = "1.0.33"
14+
serde_derive = "1.0.80"
1515

1616
[features]
1717
std = ["serde/std"]

src/de/mod.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,32 @@ impl de::Error for Error {
548548
}
549549

550550
impl fmt::Display for Error {
551-
fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result {
552-
unreachable!()
551+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
552+
write!(f, "{}", match self {
553+
Error::EofWhileParsingList => "EOF while parsing a list.",
554+
Error::EofWhileParsingObject => "EOF while parsing an object.",
555+
Error::EofWhileParsingString => "EOF while parsing a string.",
556+
Error::EofWhileParsingValue => "EOF while parsing a JSON value.",
557+
Error::ExpectedColon => "Expected this character to be a `':'`.",
558+
Error::ExpectedListCommaOrEnd => "Expected this character to be either a `','` or\
559+
a \
560+
`']'`.",
561+
Error::ExpectedObjectCommaOrEnd => "Expected this character to be either a `','` \
562+
or a \
563+
`'}'`.",
564+
Error::ExpectedSomeIdent => "Expected to parse either a `true`, `false`, or a \
565+
`null`.",
566+
Error::ExpectedSomeValue => "Expected this character to start a JSON value.",
567+
Error::InvalidNumber => "Invalid number.",
568+
Error::InvalidType => "Invalid type",
569+
Error::InvalidUnicodeCodePoint => "Invalid unicode code point.",
570+
Error::KeyMustBeAString => "Object key is not a string.",
571+
Error::TrailingCharacters => "JSON has non-whitespace trailing characters after \
572+
the \
573+
value.",
574+
Error::TrailingComma => "JSON has a comma after the last value in an array or map.",
575+
_ => "Invalid JSON"
576+
})
553577
}
554578
}
555579

src/ser/mod.rs

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
//! Serialize a Rust data structure into JSON data
22
3-
use core::marker::Unsize;
43
use core::{fmt, mem};
54

65
use serde::ser;
76

8-
use heapless::{BufferFullError, String, Vec};
7+
use heapless::{String, Vec};
98

109
use self::seq::SerializeSeq;
1110
use self::struct_::SerializeStruct;
@@ -25,35 +24,43 @@ pub enum Error {
2524
__Extensible,
2625
}
2726

27+
impl From<()> for Error {
28+
fn from(_:()) -> Error {
29+
Error::BufferFull
30+
}
31+
}
32+
33+
34+
impl From<u8> for Error {
35+
fn from(_:u8) -> Error {
36+
Error::BufferFull
37+
}
38+
}
39+
2840
#[cfg(feature = "std")]
2941
impl ::std::error::Error for Error {
3042
fn description(&self) -> &str {
3143
""
3244
}
3345
}
3446

35-
impl From<BufferFullError> for Error {
36-
fn from(_: BufferFullError) -> Self {
37-
Error::BufferFull
38-
}
39-
}
4047

4148
impl fmt::Display for Error {
42-
fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result {
43-
unreachable!()
49+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50+
write!(f, "Buffer is full")
4451
}
4552
}
4653

4754
pub(crate) struct Serializer<B>
4855
where
49-
B: Unsize<[u8]>,
56+
B: heapless::ArrayLength<u8>,
5057
{
5158
buf: Vec<u8, B>,
5259
}
5360

5461
impl<B> Serializer<B>
5562
where
56-
B: Unsize<[u8]>,
63+
B: heapless::ArrayLength<u8>,
5764
{
5865
fn new() -> Self {
5966
Serializer { buf: Vec::new() }
@@ -120,7 +127,7 @@ macro_rules! serialize_signed {
120127

121128
impl<'a, B> ser::Serializer for &'a mut Serializer<B>
122129
where
123-
B: Unsize<[u8]>,
130+
B: heapless::ArrayLength<u8>,
124131
{
125132
type Ok = ();
126133
type Error = Error;
@@ -317,7 +324,7 @@ where
317324
/// Serializes the given data structure as a string of JSON text
318325
pub fn to_string<B, T>(value: &T) -> Result<String<B>>
319326
where
320-
B: Unsize<[u8]>,
327+
B: heapless::ArrayLength<u8>,
321328
T: ser::Serialize + ?Sized,
322329
{
323330
let mut ser = Serializer::new();
@@ -328,7 +335,7 @@ where
328335
/// Serializes the given data structure as a JSON byte vector
329336
pub fn to_vec<B, T>(value: &T) -> Result<Vec<u8, B>>
330337
where
331-
B: Unsize<[u8]>,
338+
B: heapless::ArrayLength<u8>,
332339
T: ser::Serialize + ?Sized,
333340
{
334341
let mut ser = Serializer::new();
@@ -414,19 +421,20 @@ impl ser::SerializeStructVariant for Unreachable {
414421

415422
#[cfg(test)]
416423
mod tests {
417-
const N: usize = 128;
424+
use heapless::consts::U128;
425+
type N = U128;
418426

419427
#[test]
420428
fn array() {
421429
assert_eq!(
422-
&*super::to_string::<[u8; N], _>(&[0, 1, 2]).unwrap(),
430+
&*super::to_string::<N,_>(&[0, 1, 2]).unwrap(),
423431
"[0,1,2]"
424432
);
425433
}
426434

427435
#[test]
428436
fn bool() {
429-
assert_eq!(&*super::to_string::<[u8; N], _>(&true).unwrap(), "true");
437+
assert_eq!(&*super::to_string::<N, _>(&true).unwrap(), "true");
430438
}
431439

432440
#[test]
@@ -440,20 +448,20 @@ mod tests {
440448
}
441449

442450
assert_eq!(
443-
&*super::to_string::<[u8; N], _>(&Type::Boolean).unwrap(),
451+
&*super::to_string::<N, _>(&Type::Boolean).unwrap(),
444452
r#""boolean""#
445453
);
446454

447455
assert_eq!(
448-
&*super::to_string::<[u8; N], _>(&Type::Number).unwrap(),
456+
&*super::to_string::<N, _>(&Type::Number).unwrap(),
449457
r#""number""#
450458
);
451459
}
452460

453461
#[test]
454462
fn str() {
455463
assert_eq!(
456-
&*super::to_string::<[u8; N], _>("hello").unwrap(),
464+
&*super::to_string::<N, _>("hello").unwrap(),
457465
r#""hello""#
458466
);
459467
}
@@ -466,7 +474,7 @@ mod tests {
466474
}
467475

468476
assert_eq!(
469-
&*super::to_string::<[u8; N], _>(&Led { led: true }).unwrap(),
477+
&*super::to_string::<N, _>(&Led { led: true }).unwrap(),
470478
r#"{"led":true}"#
471479
);
472480
}
@@ -479,22 +487,22 @@ mod tests {
479487
}
480488

481489
assert_eq!(
482-
&*super::to_string::<[u8; N], _>(&Temperature { temperature: 127 }).unwrap(),
490+
&*super::to_string::<N, _>(&Temperature { temperature: 127 }).unwrap(),
483491
r#"{"temperature":127}"#
484492
);
485493

486494
assert_eq!(
487-
&*super::to_string::<[u8; N], _>(&Temperature { temperature: 20 }).unwrap(),
495+
&*super::to_string::<N, _>(&Temperature { temperature: 20 }).unwrap(),
488496
r#"{"temperature":20}"#
489497
);
490498

491499
assert_eq!(
492-
&*super::to_string::<[u8; N], _>(&Temperature { temperature: -17 }).unwrap(),
500+
&*super::to_string::<N, _>(&Temperature { temperature: -17 }).unwrap(),
493501
r#"{"temperature":-17}"#
494502
);
495503

496504
assert_eq!(
497-
&*super::to_string::<[u8; N], _>(&Temperature { temperature: -128 }).unwrap(),
505+
&*super::to_string::<N, _>(&Temperature { temperature: -128 }).unwrap(),
498506
r#"{"temperature":-128}"#
499507
);
500508
}
@@ -507,15 +515,15 @@ mod tests {
507515
}
508516

509517
assert_eq!(
510-
super::to_string::<[u8; N], _>(&Property {
518+
super::to_string::<N, _>(&Property {
511519
description: Some("An ambient temperature sensor"),
512520
}).unwrap(),
513521
r#"{"description":"An ambient temperature sensor"}"#
514522
);
515523

516524
// XXX Ideally this should produce "{}"
517525
assert_eq!(
518-
super::to_string::<[u8; N], _>(&Property { description: None }).unwrap(),
526+
super::to_string::<N, _>(&Property { description: None }).unwrap(),
519527
r#"{"description":null}"#
520528
);
521529
}
@@ -528,7 +536,7 @@ mod tests {
528536
}
529537

530538
assert_eq!(
531-
&*super::to_string::<[u8; N], _>(&Temperature { temperature: 20 }).unwrap(),
539+
&*super::to_string::<N, _>(&Temperature { temperature: 20 }).unwrap(),
532540
r#"{"temperature":20}"#
533541
);
534542
}
@@ -539,7 +547,7 @@ mod tests {
539547
struct Empty {}
540548

541549
assert_eq!(
542-
&*super::to_string::<[u8; N], _>(&Empty {}).unwrap(),
550+
&*super::to_string::<N, _>(&Empty {}).unwrap(),
543551
r#"{}"#
544552
);
545553

@@ -550,7 +558,7 @@ mod tests {
550558
}
551559

552560
assert_eq!(
553-
&*super::to_string::<[u8; N], _>(&Tuple { a: true, b: false }).unwrap(),
561+
&*super::to_string::<N, _>(&Tuple { a: true, b: false }).unwrap(),
554562
r#"{"a":true,"b":false}"#
555563
);
556564
}

src/ser/seq.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
use core::marker::Unsize;
2-
31
use serde::ser;
42

53
use ser::{Error, Result, Serializer};
64

75
pub struct SerializeSeq<'a, B>
86
where
9-
B: Unsize<[u8]> + 'a,
7+
B: heapless::ArrayLength<u8> + 'a,
108
{
119
de: &'a mut Serializer<B>,
1210
first: bool,
1311
}
1412

1513
impl<'a, B> SerializeSeq<'a, B>
1614
where
17-
B: Unsize<[u8]>,
15+
B: heapless::ArrayLength<u8>,
1816
{
1917
pub(crate) fn new(de: &'a mut Serializer<B>) -> Self {
2018
SerializeSeq { de, first: true }
@@ -23,7 +21,7 @@ where
2321

2422
impl<'a, B> ser::SerializeSeq for SerializeSeq<'a, B>
2523
where
26-
B: Unsize<[u8]>,
24+
B: heapless::ArrayLength<u8>,
2725
{
2826
type Ok = ();
2927
type Error = Error;
@@ -49,7 +47,7 @@ where
4947

5048
impl<'a, B> ser::SerializeTuple for SerializeSeq<'a, B>
5149
where
52-
B: Unsize<[u8]>,
50+
B: heapless::ArrayLength<u8>,
5351
{
5452
type Ok = ();
5553
type Error = Error;

src/ser/struct_.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
use core::marker::Unsize;
2-
31
use serde::ser;
42

53
use ser::{Error, Result, Serializer};
64

75
pub struct SerializeStruct<'a, B>
86
where
9-
B: Unsize<[u8]> + 'a,
7+
B: heapless::ArrayLength<u8> + 'a,
108
{
119
de: &'a mut Serializer<B>,
1210
first: bool,
1311
}
1412

1513
impl<'a, B> SerializeStruct<'a, B>
1614
where
17-
B: Unsize<[u8]>,
15+
B: heapless::ArrayLength<u8>,
1816
{
1917
pub(crate) fn new(de: &'a mut Serializer<B>) -> Self {
2018
SerializeStruct { de, first: true }
@@ -23,7 +21,7 @@ where
2321

2422
impl<'a, B> ser::SerializeStruct for SerializeStruct<'a, B>
2523
where
26-
B: Unsize<[u8]>,
24+
B: heapless::ArrayLength<u8>,
2725
{
2826
type Ok = ();
2927
type Error = Error;

0 commit comments

Comments
 (0)