haystack_types/
h_str.rs

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