-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
156 additions
and
57 deletions.
There are no files selected for viewing
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,86 @@ | ||
use ethers::types::Bytes; | ||
use serde::{Deserialize, Serialize}; | ||
use poem_openapi::registry::{MetaSchema, MetaSchemaRef}; | ||
use poem_openapi::types::{ParseFromJSON, ToJSON}; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
#[serde(transparent)] | ||
pub struct HexBytes(pub Bytes); | ||
|
||
impl From<Bytes> for HexBytes { | ||
fn from(value: Bytes) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
impl poem_openapi::types::Type for HexBytes { | ||
const IS_REQUIRED: bool = true; | ||
|
||
type RawValueType = Self; | ||
|
||
type RawElementValueType = Self; | ||
|
||
fn name() -> std::borrow::Cow<'static, str> { | ||
"string(bytes)".into() | ||
} | ||
|
||
fn schema_ref() -> MetaSchemaRef { | ||
let mut schema_ref = MetaSchema::new_with_format("string", "bytes"); | ||
|
||
schema_ref.example = Some(serde_json::Value::String( | ||
"0xffffff".to_string(), | ||
)); | ||
schema_ref.title = Some("Bytes".to_string()); | ||
schema_ref.description = Some("Hex encoded binary blob"); | ||
|
||
MetaSchemaRef::Inline(Box::new(MetaSchema::new_with_format( | ||
"string", "bytes", | ||
))) | ||
} | ||
|
||
fn as_raw_value(&self) -> Option<&Self::RawValueType> { | ||
Some(self) | ||
} | ||
|
||
fn raw_element_iter<'a>( | ||
&'a self, | ||
) -> Box<dyn Iterator<Item = &'a Self::RawElementValueType> + 'a> { | ||
Box::new(self.as_raw_value().into_iter()) | ||
} | ||
} | ||
|
||
impl ParseFromJSON for HexBytes { | ||
fn parse_from_json( | ||
value: Option<serde_json::Value>, | ||
) -> poem_openapi::types::ParseResult<Self> { | ||
// TODO: Better error handling | ||
let value = value | ||
.ok_or_else(|| poem_openapi::types::ParseError::expected_input())?; | ||
|
||
let inner = serde_json::from_value(value) | ||
.map_err(|_| poem_openapi::types::ParseError::expected_input())?; | ||
|
||
Ok(Self(inner)) | ||
} | ||
} | ||
|
||
impl ToJSON for HexBytes { | ||
fn to_json(&self) -> Option<serde_json::Value> { | ||
serde_json::to_value(&self.0).ok() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use test_case::test_case; | ||
|
||
#[test_case("0xff", vec![255])] | ||
#[test_case("0xffff", vec![255, 255])] | ||
#[test_case("0x0101", vec![1, 1])] | ||
fn deserialize_string(s: &str, v: Vec<u8>) { | ||
let value = serde_json::Value::String(s.to_string()); | ||
let result = HexBytes::parse_from_json(Some(value)).unwrap(); | ||
assert_eq!(result.0, Bytes::from(v)); | ||
} | ||
} |
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