Skip to content

Commit

Permalink
Move vec_from_str into util module
Browse files Browse the repository at this point in the history
This change moves the vec_from_str function, which so far was private to
api::v2::order, into the util module. This new location will allow this
function to be also used from the crate's data module and its children.
  • Loading branch information
d-e-s-o committed Jan 10, 2022
1 parent da38f71 commit c507c5e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
13 changes: 1 addition & 12 deletions src/api/v2/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use num_decimal::Num;

use serde::ser::SerializeStruct;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use serde::Serializer;
use serde_json::from_slice as from_json;
Expand All @@ -24,6 +23,7 @@ use serde_urlencoded::to_string as to_query;
use uuid::Uuid;

use crate::api::v2::asset;
use crate::util::vec_from_str;
use crate::Str;


Expand Down Expand Up @@ -497,17 +497,6 @@ pub struct ChangeReq {
}


/// Deserialize a `Vec` from a string that could contain a `null`.
fn vec_from_str<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error>
where
D: Deserializer<'de>,
T: Deserialize<'de>,
{
let vec = Option::<Vec<T>>::deserialize(deserializer)?;
Ok(vec.unwrap_or_else(Vec::new))
}


/// A single order as returned by the /v2/orders endpoint on a GET
/// request.
#[derive(Clone, Debug, Deserialize, PartialEq)]
Expand Down
14 changes: 13 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,23 @@ use num_decimal::Num;
use serde::Deserialize;
use serde::Deserializer;


/// Deserialize a `Num` from a string, parsing the value as signed first
/// and then dropping the sign.
pub fn abs_num_from_str<'de, D>(deserializer: D) -> Result<Num, D::Error>
pub(crate) fn abs_num_from_str<'de, D>(deserializer: D) -> Result<Num, D::Error>
where
D: Deserializer<'de>,
{
Num::deserialize(deserializer).map(|num| if num.is_negative() { num * -1 } else { num })
}


/// Deserialize a `Vec` from a string that could contain a `null`.
pub(crate) fn vec_from_str<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error>
where
D: Deserializer<'de>,
T: Deserialize<'de>,
{
let vec = Option::<Vec<T>>::deserialize(deserializer)?;
Ok(vec.unwrap_or_else(Vec::new))
}

0 comments on commit c507c5e

Please sign in to comment.