-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4388 from systeminit/fnichol/naxum-updates
feat(naxum): implement Json extractor
- Loading branch information
Showing
12 changed files
with
1,229 additions
and
778 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use async_trait::async_trait; | ||
use bytes::Bytes; | ||
use serde::de::DeserializeOwned; | ||
|
||
use crate::{ | ||
extract::{ | ||
rejection::{JsonDataError, JsonRejection, JsonSyntaxError}, | ||
FromMessage, | ||
}, | ||
MessageHead, | ||
}; | ||
|
||
#[derive(Clone, Copy, Default, Debug)] | ||
#[must_use] | ||
pub struct Json<T>(pub T); | ||
|
||
#[async_trait] | ||
impl<T, S, R> FromMessage<S, R> for Json<T> | ||
where | ||
T: DeserializeOwned, | ||
R: MessageHead + Send + 'static, | ||
S: Send + Sync, | ||
{ | ||
type Rejection = JsonRejection; | ||
|
||
async fn from_message(req: R, state: &S) -> Result<Self, Self::Rejection> { | ||
let bytes = Bytes::from_message(req, state) | ||
.await | ||
.expect("from_message is infallible"); | ||
Self::from_bytes(&bytes) | ||
} | ||
} | ||
|
||
impl<T> Json<T> | ||
where | ||
T: DeserializeOwned, | ||
{ | ||
pub fn from_bytes(bytes: &[u8]) -> Result<Self, JsonRejection> { | ||
let deserializer = &mut serde_json::Deserializer::from_slice(bytes); | ||
|
||
let value = match serde_path_to_error::deserialize(deserializer) { | ||
Ok(value) => value, | ||
Err(err) => { | ||
let rejection = match err.inner().classify() { | ||
serde_json::error::Category::Data => JsonDataError::from_err(err).into(), | ||
serde_json::error::Category::Syntax | serde_json::error::Category::Eof => { | ||
JsonSyntaxError::from_err(err).into() | ||
} | ||
serde_json::error::Category::Io => { | ||
if cfg!(debug_assertions) { | ||
// We don't use `serde_json::from_reader` and instead always buffer | ||
// bodies first, so we shouldn't encounter any IO errors | ||
unreachable!() | ||
} else { | ||
JsonSyntaxError::from_err(err).into() | ||
} | ||
} | ||
}; | ||
return Err(rejection); | ||
} | ||
}; | ||
|
||
Ok(Json(value)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.