haystack_types/
h_ref.rs

1use num::Float;
2use crate::{HType, HVal, NumTrait};
3use std::fmt::{self,Write,Display};
4use std::str::FromStr;
5use crate::common::escape_str;
6
7#[derive(PartialEq)]
8pub struct HRef {
9    id: String,
10    dis: Option<String>,
11}
12
13pub type Ref = HRef;
14
15const THIS_TYPE: HType = HType::Ref;
16
17impl HRef {
18    pub fn new(id: String, dis: Option<String>) -> HRef {
19        HRef { id, dis }
20    }
21}
22
23impl <'a,T: NumTrait + 'a>HVal<'a,T> for HRef {
24    fn to_zinc(&self, buf: &mut String) -> fmt::Result {
25        write!(buf,"@{}",self.id)?;
26        match &self.dis {
27            Some(dis) => {
28                buf.push(' ');
29                dis.chars().try_for_each(|c| { escape_str(c,buf) })?;
30                Ok(())
31            },
32            None => Ok(()),
33        }
34    }
35    fn to_json(&self, buf: &mut String) -> fmt::Result {
36        write!(buf,"r:{}",self.id)?;
37        match &self.dis {
38            Some(dis) => {
39                buf.push(' ');
40                dis.chars().try_for_each(|c| { escape_str(c,buf) })?;
41                Ok(())
42            },
43            None => Ok(()),
44        }
45    }
46    fn haystack_type(&self) -> HType { THIS_TYPE }
47
48    set_trait_eq_method!(get_ref_val,'a,T);
49    set_get_method!(get_ref_val, HRef);
50}