sing_parse/
callobj.rs

1// Copyright 2022 Daniel Mowitz
2//
3// This file is part of sing.
4//
5// sing is free software: you can redistribute it and/or modify it 
6// under the terms of the GNU Affero General Public License 
7// as published by the Free Software Foundation, 
8// either version 3 of the License, or (at your option) any later version.
9//
10// sing is distributed in the hope that it will be useful, 
11// but WITHOUT ANY WARRANTY; without even the implied warranty
12// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
13// See the GNU Affero General Public License for more details.
14// 
15// You should have received a copy of the GNU Affero General Public License
16//  along with sing. If not, see <https://www.gnu.org/licenses/>. 
17
18use std::fmt;
19use sing_util::TraitCallMessage;
20use serde::{Serialize, Deserialize};
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct CallObj {
24	 trait_string: Option<String>,
25	 fun_string: String,
26	 index: usize,
27	 data: Vec<String>,
28}
29
30/// Default message representation for the sing_loop! macro.
31impl CallObj {
32    pub fn new((trait_string, fun_string, index): (Option<String>, String, usize), data: Vec<String>) -> Self {
33		  Self{
34				trait_string,
35				fun_string,
36				index,
37				data,
38		  }
39	 }
40}
41
42impl TraitCallMessage for CallObj {
43	 type Representation = String;
44
45	 fn get_fun_name(&self) -> String {
46		  self.fun_string.clone()
47	 }
48
49	 fn get_trait_name(&self) -> Option<String> {
50		  self.trait_string.clone()
51	 }
52
53	 fn get_params(&self) -> Vec<Self::Representation> {
54		  self.data.clone()
55	 }
56
57	 fn new_params(&mut self, p: Vec<Self::Representation>) {
58		  self.data = p;
59	 }
60}
61
62impl fmt::Display for CallObj {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64		  let mut repr = String::new();
65
66		  match &self.trait_string {
67				Some(tstr) => {
68					 repr.push_str(tstr);
69					 repr.push('>');
70				},
71				None => {}
72		  }
73
74		  repr.push_str(&self.fun_string);
75
76		  if self.index != usize::MAX {
77				repr.push('>');
78				repr.push_str(&self.index.to_string());
79		  }
80
81		  for element in &self.data {
82				repr.push(' ');
83				repr.push_str(element);
84		  }
85
86		  write!(f, "{}", repr)
87	 }
88}