Skip to content

Commit

Permalink
feat(de): from_slice func
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeStanger committed Nov 27, 2022
1 parent e6c8e90 commit 7a2f7b5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
14 changes: 13 additions & 1 deletion libcorn/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::VecDeque;
use serde::de::{DeserializeSeed, EnumAccess, IntoDeserializer, VariantAccess, Visitor};
use serde::{de, Deserialize};

use crate::error::{DeserializationError, Result};
use crate::error::{DeserializationError, Error, Result};
use crate::parse;
use crate::Value;

Expand Down Expand Up @@ -32,6 +32,18 @@ where
Ok(T::deserialize(&mut deserializer)?)
}

pub fn from_slice<'de, T>(bytes: &'de [u8]) -> Result<T>
where
T: de::Deserialize<'de>,
{
match std::str::from_utf8(bytes) {
Ok(s) => from_str(s),
Err(e) => Err(Error::DeserializationError(DeserializationError(
e.to_string(),
))),
}
}

macro_rules! get_value {
($self:ident) => {
match $self.value.take() {
Expand Down
6 changes: 6 additions & 0 deletions libcorn/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ impl From<pest::error::Error<Rule>> for Error {
}
}

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

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Expand Down
2 changes: 1 addition & 1 deletion libcorn/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::Serialize;
use std::collections::{BTreeMap, HashMap};

pub use crate::de::from_str;
pub use crate::de::{from_slice, from_str};
pub use crate::parser::{parse, Rule};

pub mod error;
Expand Down

0 comments on commit 7a2f7b5

Please sign in to comment.