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
use core::fmt::Write;
use crate::runtime::Variant;
use crate::runtime::strings::{StringValue, StrBuffer};
use crate::runtime::types::{Type, MetaObject};
use crate::runtime::errors::{ExecResult};


impl MetaObject for StringValue {
    fn type_tag(&self) -> Type { Type::String }
    
    fn len(&self) -> Option<ExecResult<usize>> {
        Some(Ok(self.char_count()))
    }
    
    fn op_add(&self, rhs: &Variant) -> Option<ExecResult<Variant>> {
        if let Some(rhs) = rhs.as_strval() {
            return Some(self.concat(&rhs).map(Variant::from))
        }
        None
    }
    
    fn op_radd(&self, lhs: &Variant) -> Option<ExecResult<Variant>> {
        if let Some(lhs) = lhs.as_strval() {
            return Some(lhs.concat(self).map(Variant::from))
        }
        None
    }
    
    fn cmp_eq(&self, other: &Variant) -> Option<ExecResult<bool>> {
        if let Some(other) = other.as_strval() {
            return Some(Ok(*self == other))
        }
        None
    }
    
    fn cmp_lt(&self, other: &Variant) -> Option<ExecResult<bool>> {
        if let Some(other) = other.as_strval() {
            return Some(Ok(*self < other))
        }
        None
    }
    
    fn cmp_le(&self, other: &Variant) -> Option<ExecResult<bool>> {
        if let Some(other) = other.as_strval() {
            return Some(Ok(*self <= other))
        }
        None
    }
    
    fn fmt_echo(&self) -> ExecResult<StringValue> {
        let mut buf = StrBuffer::<64>::new();
        if write!(buf, "\"{}\"", self).is_ok() {
            Ok(StringValue::new_uninterned(buf))
        } else {
            // resort to allocated buffer
            Ok(StringValue::new_uninterned(format!("\"{}\"", self)))
        }
    }
}