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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! Interpreting memory calls.
use std::borrow::Cow;

use crate::{
    data,
    error::{ApiError, Result},
    EndpointResult,
};

/// Call raw result.
#[derive(serde_derive::Deserialize, Clone, Hash, Debug)]
#[doc(hidden)]
pub(crate) struct Response {
    ok: i32,
}

/// SetMemorySegment details
#[derive(Serialize, Clone, Hash, Debug)]
pub struct SetMemorySegmentArgs<'a> {
    /// The segment to set.
    pub segment: u32,
    /// The shard to set it in (optional for private servers).
    pub shard: Option<Cow<'a, str>>,
    /// The data
    pub data: Cow<'a, str>,
}

/// Memory segment set result
#[derive(Clone, Hash, Debug)]
pub(crate) struct SetMemorySegment {
    /// Phantom data in order to allow adding any additional fields in the future.
    _non_exhaustive: (),
}

impl EndpointResult for SetMemorySegment {
    type RequestResult = Response;
    type ErrorResult = data::ApiError;

    fn from_raw(raw: Response) -> Result<Self> {
        let Response { ok } = raw;

        if ok != 1 {
            return Err(ApiError::NotOk(ok).into());
        }

        Ok(SetMemorySegment {
            _non_exhaustive: (),
        })
    }
}

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

    fn test_parse(json: serde_json::Value) {
        let response = serde_json::from_value(json).unwrap();

        let _ = SetMemorySegment::from_raw(response).unwrap();
    }

    #[test]
    fn parse_sample() {
        test_parse(json! ({
            "ok": 1,
        }));
    }
}