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
71
72
73
74
75
76
77
78
79
80
//! Array model.  

//! 配列モデル。  

//!

//! # Examples

//!

//! ```

//! // [ 1, 2, 3 ]

//! ```

use num_traits::Num;

use crate::model::{
    layer210::{BasicString, LiteralString, LiteralValue},
    layer220::{Array, ItemValue},
};
use std::fmt;

impl Default for Array {
    fn default() -> Self {
        Array { items: Vec::new() }
    }
}
impl Array {
    pub fn push_literal_string(&mut self, m: &LiteralValue) {
        self.items.push(ItemValue::LiteralValue(m.clone()));
    }
    pub fn push_single_quote_string(&mut self, m: &LiteralString) {
        self.items.push(ItemValue::LiteralString(m.clone()));
    }
    pub fn push_double_quote_string(&mut self, m: &BasicString) {
        self.items.push(ItemValue::BasicString(m.clone()));
    }
    pub fn push_array(&mut self, m: &Array) {
        self.items.push(ItemValue::Array(m.clone()));
    }
    pub fn to_debug_string(&self) -> String {
        format!("{:?}", self)
    }
    pub fn to_string(&self) -> String {
        format!("{}", self)
    }
    pub fn to_string_vector(&self) -> Vec<String> {
        let mut vec = Vec::<String>::new();
        for item in &self.items {
            vec.push(item.to_string());
        }
        vec
    }
    pub fn to_int_vector<T: Num + std::str::FromStr>(&self) -> Result<Vec<T>, String>
    where
        <T as std::str::FromStr>::Err: std::fmt::Display,
    {
        let mut vec = Vec::<T>::new();
        for item in &self.items {
            let num = match item.to_string().parse() {
                Ok(n) => n,
                Err(why) => return Err(format!("{}", why)),
            };
            vec.push(num);
        }
        Ok(vec)
    }
}
impl fmt::Display for Array {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut buf = String::new();
        for item in &self.items {
            buf.push_str(&format!("{},", item))
        }
        write!(f, "{}", buf)
    }
}
impl fmt::Debug for Array {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut buf = String::new();
        for item in &self.items {
            buf.push_str(&format!("{:?},", item))
        }
        write!(f, "[ {} ]", buf)
    }
}