sing/sing_parse/src/callobj.rs

71 lines
1.4 KiB
Rust

use std::fmt;
use sing_util::TraitCallMessage;
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CallObj {
trait_string: Option<String>,
fun_string: String,
index: usize,
data: Vec<String>,
}
/// Default message representation for the sing_loop! macro.
impl CallObj {
pub fn new((trait_string, fun_string, index): (Option<String>, String, usize), data: Vec<String>) -> Self {
Self{
trait_string,
fun_string,
index,
data,
}
}
}
impl TraitCallMessage for CallObj {
type Representation = String;
fn get_fun_name(&self) -> String {
self.fun_string.clone()
}
fn get_trait_name(&self) -> Option<String> {
self.trait_string.clone()
}
fn get_params(&self) -> Vec<Self::Representation> {
self.data.clone()
}
fn new_params(&mut self, p: Vec<Self::Representation>) {
self.data = p;
}
}
impl fmt::Display for CallObj {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut repr = String::new();
match &self.trait_string {
Some(tstr) => {
repr.push_str(tstr);
repr.push('>');
},
None => {}
}
repr.push_str(&self.fun_string);
if self.index != usize::MAX {
repr.push('>');
repr.push_str(&self.index.to_string());
}
for element in &self.data {
repr.push(' ');
repr.push_str(element);
}
write!(f, "{}", repr)
}
}