celery_rs_core/argparse/
args.rs

1/*
2Parser given arguments into a json string
3
4Author Andrew Evans
5*/
6
7use std::vec::Vec;
8
9use amiquip::AmqpValue;
10use amq_protocol::types::AMQPType;
11use amq_protocol::uri::AMQPScheme::AMQP;
12use serde_json::Value;
13
14use crate::serde_utils::val_handler::value_to_amqp_value;
15
16
17/// Structure storing the arguments
18#[derive(Clone, Debug)]
19pub struct Args{
20    pub args: Vec<Arg>,
21}
22
23
24/// Struct storing a single argument
25#[derive(Clone, Debug)]
26pub struct Arg{
27    pub arg: Value,
28    pub arg_type: AMQPType,
29}
30
31
32/// Argument implementation
33impl Arg{
34
35    /// Create a new argument
36    pub fn new(arg: Value, arg_type: AMQPType) -> Arg{
37        Arg{
38            arg: arg,
39            arg_type: arg_type
40        }
41    }
42}
43
44
45/// Implementation of arguments list
46impl Args{
47
48    /// convert to amqp vec
49    pub fn args_to_amqp_vec(&self) -> Vec<AmqpValue>{
50        let mut val_vec = Vec::<AmqpValue>::new();
51        for i in 0..self.args.len(){
52            let val = self.args.get(i).unwrap().clone();
53            val_vec.push(value_to_amqp_value(&val.arg));
54        }
55        val_vec
56    }
57
58    /// convert args to a vec
59    pub fn args_to_vec(&self) -> Vec<Value>{
60        let mut val_vec = Vec::<Value>::new();
61        for i in 0..self.args.len(){
62            let val = self.args.get(i).unwrap().clone();
63            val_vec.push(val.arg);
64        }
65        val_vec
66    }
67
68    /// size of the list
69    pub fn size(&self) -> usize{
70        self.args.len()
71    }
72
73    /// add an argument
74    pub fn add_arg(&mut self, arg: Arg){
75        self.args.push(arg);
76    }
77
78    /// create a new arguments list
79    pub fn new() -> Args{
80        Args{
81           args: Vec::<Arg>::new(),
82        }
83    }
84
85}
86
87
88#[cfg(test)]
89mod tests{
90    use amq_protocol::types::AMQPType;
91    use serde_json::Value;
92
93    use super::*;
94
95    #[test]
96    fn should_create_an_argument(){
97        let test_string = String::from("test");
98        let test_val = Value::from(test_string);
99        let arg = Arg::new(test_val.clone(), AMQPType::LongString);
100        assert!(arg.arg.as_str().unwrap().eq("test"));
101    }
102
103    #[test]
104    fn should_create_an_argument_list(){
105        let test_string = String::from("test");
106        let test_val = Value::from(test_string);
107        let arg = Arg::new(test_val.clone(), AMQPType::LongString);
108        assert!(arg.arg.as_str().unwrap().eq("test"));
109        let mut args = Args::new();
110        args.args.push(arg);
111        assert!(args.size() == 1);
112        assert!(args.args.len() == 1);
113        assert!(args.args.get(0).unwrap().arg.as_str().unwrap().eq("test"));
114    }
115}