Skip to content

Commit

Permalink
refactor(pay): make comment size optional on query construction
Browse files Browse the repository at this point in the history
  • Loading branch information
lsunsi committed Dec 6, 2023
1 parent c26736c commit 0720b5c
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 25 deletions.
24 changes: 11 additions & 13 deletions src/core/pay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Query {
pub email: Option<String>,
pub jpeg: Option<Vec<u8>>,
pub png: Option<Vec<u8>>,
pub comment_size: u64,
pub comment_size: Option<u64>,
pub min: u64,
pub max: u64,
}
Expand All @@ -23,8 +23,6 @@ impl std::str::FromStr for Query {

let p: de::Query = miniserde::json::from_str(s).map_err(|_| "deserialize failed")?;

let comment_size = p.comment_allowed.unwrap_or(0);

let metadata = miniserde::json::from_str::<Vec<(String, Value)>>(&p.metadata)
.map_err(|_| "deserialize metadata failed")?;

Expand Down Expand Up @@ -79,11 +77,11 @@ impl std::str::FromStr for Query {

Ok(Query {
callback: p.callback.0.into_owned(),
comment_size: p.comment_allowed,
min: p.min_sendable,
max: p.max_sendable,
short_description,
long_description,
comment_size,
identifier,
email,
jpeg,
Expand Down Expand Up @@ -124,7 +122,7 @@ impl std::fmt::Display for Query {
callback: crate::serde::Url(std::borrow::Cow::Borrowed(&self.callback)),
min_sendable: self.min,
max_sendable: self.max,
comment_allowed: self.comment_size,
comment_allowed: self.comment_size.unwrap_or(0),
}))
}
}
Expand Down Expand Up @@ -290,7 +288,7 @@ mod tests {
assert_eq!(parsed.min, 314);
assert_eq!(parsed.max, 315);

assert_eq!(parsed.comment_size, 0);
assert!(parsed.comment_size.is_none());
assert!(parsed.long_description.is_none());
assert!(parsed.jpeg.is_none());
assert!(parsed.png.is_none());
Expand All @@ -311,7 +309,7 @@ mod tests {
"#;

let parsed = input.parse::<super::Query>().expect("parse");
assert_eq!(parsed.comment_size, 140);
assert_eq!(parsed.comment_size.unwrap(), 140);
}

#[test]
Expand Down Expand Up @@ -386,7 +384,7 @@ mod tests {
long_description: None,
jpeg: None,
png: None,
comment_size: 0,
comment_size: None,
min: 314,
max: 315,
identifier: None,
Expand All @@ -407,7 +405,7 @@ mod tests {
long_description: None,
jpeg: None,
png: None,
comment_size: 140,
comment_size: Some(140),
min: 314,
max: 315,
identifier: None,
Expand All @@ -428,7 +426,7 @@ mod tests {
long_description: Some(String::from("mochila a jato brutal incluida")),
jpeg: None,
png: None,
comment_size: 0,
comment_size: None,
min: 314,
max: 315,
identifier: None,
Expand All @@ -449,7 +447,7 @@ mod tests {
long_description: None,
jpeg: Some(b"imagembrutal".to_vec()),
png: Some(b"fotobrutal".to_vec()),
comment_size: 0,
comment_size: None,
min: 314,
max: 315,
identifier: None,
Expand All @@ -470,7 +468,7 @@ mod tests {
long_description: None,
jpeg: Some(b"imagembrutal".to_vec()),
png: Some(b"fotobrutal".to_vec()),
comment_size: 0,
comment_size: None,
min: 314,
max: 315,
identifier: Some(String::from("steve@magal.brutal")),
Expand All @@ -491,7 +489,7 @@ mod tests {
long_description: None,
jpeg: Some(b"imagembrutal".to_vec()),
png: Some(b"fotobrutal".to_vec()),
comment_size: 0,
comment_size: None,
min: 314,
max: 315,
identifier: None,
Expand Down
2 changes: 1 addition & 1 deletion tests/lud06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn test() {
long_description: Some(String::from("the destroyer of worlds")),
jpeg: None,
png: None,
comment_size: 0,
comment_size: None,
min: 314,
max: 315,
identifier: None,
Expand Down
2 changes: 1 addition & 1 deletion tests/lud09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn test() {
long_description: None,
jpeg: None,
png: None,
comment_size: 0,
comment_size: None,
min: 314,
max: 315,
identifier: None,
Expand Down
2 changes: 1 addition & 1 deletion tests/lud11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn test() {
long_description: None,
jpeg: None,
png: None,
comment_size: 0,
comment_size: None,
min: 314,
max: 315,
identifier: None,
Expand Down
4 changes: 2 additions & 2 deletions tests/lud12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn test() {
long_description: None,
jpeg: None,
png: None,
comment_size: 140,
comment_size: Some(140),
min: 314,
max: 315,
identifier: None,
Expand Down Expand Up @@ -56,7 +56,7 @@ async fn test() {
panic!("not pay request");
};

assert_eq!(pr.core.comment_size, 140);
assert_eq!(pr.core.comment_size.unwrap(), 140);

let invoice = pr.clone().callback("", 314).await.expect("callback");
assert_eq!(invoice.pr, "pierre:None");
Expand Down
2 changes: 1 addition & 1 deletion tests/lud16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async fn test() {
long_description: Some(String::from("the destroyer of worlds")),
jpeg: None,
png: None,
comment_size: 0,
comment_size: None,
min: 314,
max: 315,
identifier: identifier.clone().filter(|i| i.starts_with('n')),
Expand Down
12 changes: 6 additions & 6 deletions tests/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod pay_request {
assert_eq!(pr.short_description, "Sats for lorenzo");
assert!(pr.long_description.is_none());

assert_eq!(pr.comment_size, 255);
assert_eq!(pr.comment_size, Some(255));
assert_eq!(pr.max, 500_000_000);
assert_eq!(pr.min, 1000);

Expand All @@ -37,7 +37,7 @@ mod pay_request {
assert_eq!(pr.short_description, "Payment to lorenzo");
assert!(pr.long_description.is_none());

assert_eq!(pr.comment_size, 2000);
assert_eq!(pr.comment_size, Some(2000));
assert_eq!(pr.max, 100_000_000_000);
assert_eq!(pr.min, 1000);

Expand All @@ -57,7 +57,7 @@ mod pay_request {
assert_eq!(pr.short_description, "$kenu ⚡ bipa.app");
assert!(pr.long_description.is_none());

assert_eq!(pr.comment_size, 140);
assert_eq!(pr.comment_size, Some(140));
assert_eq!(pr.max, 1_000_000_000);
assert_eq!(pr.min, 1000);

Expand All @@ -80,7 +80,7 @@ mod pay_request {
assert_eq!(pr.short_description, "Lightning payment to ethan@pouch.ph");
assert!(pr.long_description.is_none());

assert_eq!(pr.comment_size, 150);
assert_eq!(pr.comment_size, Some(150));
assert_eq!(pr.max, 10_000_000_000);
assert_eq!(pr.min, 1000);

Expand All @@ -103,7 +103,7 @@ mod pay_request {
);
assert!(pr.long_description.is_none());

assert_eq!(pr.comment_size, 255);
assert_eq!(pr.comment_size, Some(255));
assert_eq!(pr.max, 100_000_000_000);
assert_eq!(pr.min, 1000);

Expand All @@ -126,7 +126,7 @@ mod pay_request {
assert_eq!(pr.short_description, "luhack - Welcome to my zbd.gg page!");
assert!(pr.long_description.is_none());

assert_eq!(pr.comment_size, 150);
assert_eq!(pr.comment_size, Some(150));
assert_eq!(pr.max, 500_000_000);
assert_eq!(pr.min, 1000);

Expand Down

0 comments on commit 0720b5c

Please sign in to comment.