Skip to main content

xrpl/models/requests/
random.rs

1use alloc::borrow::Cow;
2use serde::{Deserialize, Serialize};
3use serde_with::skip_serializing_none;
4
5use crate::models::{requests::RequestMethod, Model};
6
7use super::{CommonFields, Request};
8
9/// The random command provides a random number to be used
10/// as a source of entropy for random number generation
11/// by clients.
12///
13/// See Random:
14/// `<https://xrpl.org/random.html#random>`
15#[skip_serializing_none]
16#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
17pub struct Random<'a> {
18    /// The common fields shared by all requests.
19    #[serde(flatten)]
20    pub common_fields: CommonFields<'a>,
21}
22
23impl<'a> Model for Random<'a> {}
24
25impl<'a> Request<'a> for Random<'a> {
26    fn get_common_fields(&self) -> &CommonFields<'a> {
27        &self.common_fields
28    }
29
30    fn get_common_fields_mut(&mut self) -> &mut CommonFields<'a> {
31        &mut self.common_fields
32    }
33}
34
35impl<'a> Random<'a> {
36    pub fn new(id: Option<Cow<'a, str>>) -> Self {
37        Self {
38            common_fields: CommonFields {
39                command: RequestMethod::Random,
40                id,
41            },
42        }
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_serde_round_trip() {
52        let req = Random::new(Some("rand-1".into()));
53        let serialized = serde_json::to_string(&req).unwrap();
54        let deserialized: Random = serde_json::from_str(&serialized).unwrap();
55        assert_eq!(req, deserialized);
56        assert!(serialized.contains("\"command\":\"random\""));
57    }
58}