Skip to content

Commit

Permalink
Refactor streaming JSON to separate modules
Browse files Browse the repository at this point in the history
  • Loading branch information
zargony committed Oct 6, 2024
1 parent bfef4b6 commit 6a9c986
Show file tree
Hide file tree
Showing 5 changed files with 681 additions and 649 deletions.
42 changes: 42 additions & 0 deletions firmware/src/json/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use super::value::TryFromValueError;
use core::fmt;

/// JSON reader/writer error
#[derive(Debug, PartialEq)]
pub enum Error<E> {
Io(E),
Eof,
Unexpected(char),
NumberTooLarge,
InvalidType,
}

impl<E: embedded_io_async::Error> From<E> for Error<E> {
fn from(err: E) -> Self {
Self::Io(err)
}
}

impl<E> From<TryFromValueError> for Error<E> {
fn from(_err: TryFromValueError) -> Self {
Self::InvalidType
}
}

impl<E: fmt::Display> fmt::Display for Error<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(err) => write!(f, "I/O error: {err}"),
Self::Eof => write!(f, "Premature EOF"),
Self::Unexpected(ch) => write!(f, "Unexpected `{ch}`"),
Self::NumberTooLarge => write!(f, "Number too large"),
Self::InvalidType => write!(f, "Invalid type"),
}
}
}

impl<E> Error<E> {
pub fn unexpected(ch: u8) -> Self {
Self::Unexpected(char::from(ch))
}
}
13 changes: 13 additions & 0 deletions firmware/src/json/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![allow(unused_imports)]

mod error;
pub use self::error::Error;

mod reader;
pub use self::reader::{FromJson, Reader};

mod value;
pub use self::value::{TryFromValueError, Value};

mod writer;
pub use self::writer::{ObjectWriter, ToJson, Writer};
Loading

0 comments on commit 6a9c986

Please sign in to comment.