Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use serde_with DeserializeFromStr #3343

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 55 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ rustls-acme = { version = "0.8.1", features = ["axum"] }
serde = { version = "1.0.137", features = ["derive"] }
serde-hex = "0.1.0"
serde_json = { version = "1.0.81", features = ["preserve_order"] }
serde_with = "3.7.0"
serde_yaml = "0.9.17"
sha3 = "0.10.8"
sysinfo = "0.30.3"
Expand Down
1 change: 1 addition & 0 deletions crates/ordinals/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rust-version = "1.67"
bitcoin = { version = "0.30.1", features = ["rand"] }
derive_more = "0.99.17"
serde = { version = "1.0.137", features = ["derive"] }
serde_with = "3.7.0"
thiserror = "1.0.56"

[dev-dependencies]
Expand Down
7 changes: 2 additions & 5 deletions crates/ordinals/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use {
OutPoint,
},
derive_more::{Display, FromStr},
serde::{Deserialize, Deserializer, Serialize, Serializer},
serde::{Deserialize, Serialize},
serde_with::{DeserializeFromStr, SerializeDisplay},
std::{
cmp,
fmt::{self, Display, Formatter},
Expand All @@ -26,12 +27,8 @@ pub use {
sat_point::SatPoint,
};

#[doc(hidden)]
pub use self::deserialize_from_str::DeserializeFromStr;

mod decimal_sat;
mod degree;
mod deserialize_from_str;
mod epoch;
mod height;
mod rarity;
Expand Down
20 changes: 1 addition & 19 deletions crates/ordinals/src/rarity.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

#[derive(Debug, PartialEq, PartialOrd, Copy, Clone)]
#[derive(Debug, PartialEq, PartialOrd, Copy, Clone, DeserializeFromStr, SerializeDisplay)]
pub enum Rarity {
Common,
Uncommon,
Expand Down Expand Up @@ -90,24 +90,6 @@ impl FromStr for Rarity {
}
}

impl Serialize for Rarity {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(self)
}
}

impl<'de> Deserialize<'de> for Rarity {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
DeserializeFromStr::with(deserializer)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
32 changes: 13 additions & 19 deletions crates/ordinals/src/sat_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,19 @@ use {super::*, bitcoin::transaction::ParseOutPointError};
/// that of the second sat of the genesis block coinbase output is
/// `000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f:0:1`, and
/// so on and so on.
#[derive(Debug, PartialEq, Copy, Clone, Eq, PartialOrd, Ord, Default, Hash)]
#[derive(
Debug,
PartialEq,
Copy,
Clone,
Eq,
PartialOrd,
Ord,
Default,
Hash,
DeserializeFromStr,
SerializeDisplay,
)]
pub struct SatPoint {
pub outpoint: OutPoint,
pub offset: u64,
Expand Down Expand Up @@ -39,24 +51,6 @@ impl Decodable for SatPoint {
}
}

impl Serialize for SatPoint {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(self)
}
}

impl<'de> Deserialize<'de> for SatPoint {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
DeserializeFromStr::with(deserializer)
}
}

impl FromStr for SatPoint {
type Err = Error;

Expand Down
20 changes: 1 addition & 19 deletions src/decimal.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

#[derive(Debug, PartialEq, Copy, Clone, Default)]
#[derive(Debug, PartialEq, Copy, Clone, Default, DeserializeFromStr, SerializeDisplay)]
pub struct Decimal {
value: u128,
scale: u8,
Expand Down Expand Up @@ -85,24 +85,6 @@ impl FromStr for Decimal {
}
}

impl Serialize for Decimal {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(self)
}
}

impl<'de> Deserialize<'de> for Decimal {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
DeserializeFromStr::with(deserializer)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@ use super::*;

pub struct DeserializeFromStr<T: FromStr>(pub T);

impl<'de, T: FromStr> DeserializeFromStr<T>
where
T::Err: Display,
{
pub fn with<D>(deserializer: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
{
Ok(DeserializeFromStr::<T>::deserialize(deserializer)?.0)
}
}

impl<'de, T: FromStr> Deserialize<'de> for DeserializeFromStr<T>
where
T::Err: Display,
Expand Down
22 changes: 3 additions & 19 deletions src/inscriptions/inscription_id.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use super::*;

#[derive(Debug, PartialEq, Copy, Clone, Hash, Eq, PartialOrd, Ord)]
#[derive(
Debug, PartialEq, Copy, Clone, Hash, Eq, PartialOrd, Ord, DeserializeFromStr, SerializeDisplay,
)]
pub struct InscriptionId {
pub txid: Txid,
pub index: u32,
Expand Down Expand Up @@ -34,24 +36,6 @@ impl InscriptionId {
}
}

impl<'de> Deserialize<'de> for InscriptionId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
DeserializeFromStr::with(deserializer)
}
}

impl Serialize for InscriptionId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(self)
}
}

impl Display for InscriptionId {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}i{}", self.txid, self.index)
Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use {
arguments::Arguments,
blocktime::Blocktime,
decimal::Decimal,
deserialize_from_str::DeserializeFromStr,
index::BitcoinCoreRpcResultExt,
inscriptions::{
inscription_id,
Expand Down Expand Up @@ -52,10 +53,11 @@ use {
html_escaper::{Escape, Trusted},
http::HeaderMap,
lazy_static::lazy_static,
ordinals::{DeserializeFromStr, Epoch, Height, Rarity, Sat, SatPoint},
ordinals::{Epoch, Height, Rarity, Sat, SatPoint},
regex::Regex,
reqwest::Url,
serde::{Deserialize, Deserializer, Serialize, Serializer},
serde::{Deserialize, Deserializer, Serialize},
serde_with::{DeserializeFromStr, SerializeDisplay},
std::{
cmp::{self, Reverse},
collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque},
Expand Down Expand Up @@ -111,6 +113,7 @@ pub mod arguments;
mod blocktime;
pub mod chain;
mod decimal;
mod deserialize_from_str;
mod fee_rate;
pub mod index;
mod inscriptions;
Expand Down
20 changes: 1 addition & 19 deletions src/object.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, DeserializeFromStr, SerializeDisplay)]
pub enum Object {
Address(Address<NetworkUnchecked>),
Hash([u8; 32]),
Expand Down Expand Up @@ -53,24 +53,6 @@ impl Display for Object {
}
}

impl Serialize for Object {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(self)
}
}

impl<'de> Deserialize<'de> for Object {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
DeserializeFromStr::with(deserializer)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading
Loading