1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// ref https://developer.apple.com/documentation/appstorereceipts/responsebody

use serde::Serialize;

#[derive(Serialize, Debug)]
pub struct RequestBody<'a> {
    #[serde(rename(serialize = "receipt-data"))]
    pub receipt_data: &'a str,
    pub password: &'a str,
    #[serde(
        rename(serialize = "exclude-old-transactions",),
        skip_serializing_if = "Option::is_none"
    )]
    pub exclude_old_transactions: Option<bool>,
}

impl<'a> RequestBody<'a> {
    pub fn new(
        receipt_data: &'a str,
        password: &'a str,
        exclude_old_transactions: Option<bool>,
    ) -> Self {
        Self {
            receipt_data,
            password,
            exclude_old_transactions,
        }
    }
}

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

    use std::io;

    #[test]
    fn simple() -> io::Result<()> {
        assert_eq!(
            serde_json::to_string(&RequestBody::new("foo", "pw", None))?,
            r#"{"receipt-data":"foo","password":"pw"}"#
        );

        assert_eq!(
            serde_json::to_string(&RequestBody::new("foo", "pw", Some(true)))?,
            r#"{"receipt-data":"foo","password":"pw","exclude-old-transactions":true}"#
        );

        Ok(())
    }
}