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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use crate::error::*;
use crate::prelude::*;
use std::str;
use std::cell::Ref;

use ouroboros::self_referencing;

#[derive(Debug, Clone)]
pub struct Lines {
    pub parent: ObjectCell<StringObject>,
}

impl Object for ObjectCell<Lines> {
    fn technetium_clone(&self, _context: &mut RuntimeContext<'_>) -> RuntimeResult<ObjectRef> {
        let this = self.try_borrow()?;
        Ok(ObjectRef::new(Lines {
            parent: ObjectCell::clone(&this.parent),
        }))
    }

    fn technetium_type_name(&self) -> String {
        "lines".to_string()
    }

    fn make_iter(&self, _context: &mut RuntimeContext<'_>) -> RuntimeResult<ObjectRef> {
        let this = self.try_borrow()?;
        let linesiterbuild = LinesIteratorBuilder {
            head: ObjectCell::clone(&this.parent),
            s_builder: |head| head.try_borrow().unwrap(),
            lines_builder: |s| s.val.lines()
        };

        Ok(ObjectRef::new(linesiterbuild.build()))
    }
}

// Rentals must be used because str::Lines takes a reference
// to a String, and we own the string it takes a reference to

#[self_referencing]
pub struct LinesIterator {
    head: ObjectCell<StringObject>,
    #[covariant]
    #[borrows(head)]
    s: Ref<'this, StringObject>,
    #[not_covariant]
    #[borrows(s)]
    lines: str::Lines<'this>,
}

impl Object for ObjectCell<LinesIterator> {
    fn technetium_type_name(&self) -> String {
        "iterator(lines)".to_string()
    }

    fn take_iter(&self, _context: &mut RuntimeContext<'_>) -> RuntimeResult<Option<ObjectRef>> {
        let mut this = self.try_borrow_mut()?;
        this.with_mut(|fields| {
            let next = fields.lines.next().map(|val| val.to_string());
            Ok(next.map(StringObject::new))
        })
    }
}

#[derive(Debug, Clone)]
pub struct Chars {
    pub parent: ObjectCell<StringObject>,
}

impl Object for ObjectCell<Chars> {
    fn technetium_clone(&self, _context: &mut RuntimeContext<'_>) -> RuntimeResult<ObjectRef> {
        let this = self.try_borrow()?;
        Ok(ObjectRef::new(Chars {
            parent: ObjectCell::clone(&this.parent),
        }))
    }

    fn technetium_type_name(&self) -> String {
        "chars".to_string()
    }

    fn make_iter(&self, _context: &mut RuntimeContext<'_>) -> RuntimeResult<ObjectRef> {
        let this = self.try_borrow()?;

        let charsiterbuild =
            CharsIteratorBuilder {
                head: ObjectCell::clone(&this.parent),
                s_builder: |head| head.try_borrow().unwrap(),
                chars_builder: |s| s.val.chars()
            };

        Ok(ObjectRef::new(charsiterbuild.build()))
    }
}

#[self_referencing]
pub struct CharsIterator {
    head: ObjectCell<StringObject>,
    #[covariant]
    #[borrows(head)]
    s: Ref<'this, StringObject>,
    #[not_covariant]
    #[borrows(s)]
    chars: str::Chars<'this>,
}

impl Object for ObjectCell<CharsIterator> {
    fn technetium_type_name(&self) -> String {
        "iterator(chars)".to_string()
    }

    fn take_iter(&self, _context: &mut RuntimeContext<'_>) -> RuntimeResult<Option<ObjectRef>> {
        let mut this = self.try_borrow_mut()?;
        this.with_mut(|fields| {
            let next = fields.chars.next();
            Ok(next.map(CharObject::new))
        })
    }
}

func_object!(StripPrefix, (2..=2), _c, args -> {
    downcast!((str_obj: StringObject = args[0]) -> {
        downcast!((str_obj2: StringObject = args[1]) -> {
            let val1 = &str_obj.val;
            let val2 = &str_obj2.val;
            let newval = val1.strip_prefix(val2);
            if let Some(newval) = newval {
                Ok(StringObject::new(String::from(newval)))
            } else {
                Ok(UnitObject::new())
            }
        } else {
            Err(RuntimeError::type_error("Expected strings as arguments to strip_prefix"))
        })
    } else {
        Err(RuntimeError::type_error("Expected string as argument to strip_prefix"))
    })
});

func_object!(StripSuffix, (2..=2), _c, args -> {
    downcast!((str_obj: StringObject = args[0]) -> {
        downcast!((str_obj2: StringObject = args[1]) -> {
            let val1 = &str_obj.val;
            let val2 = &str_obj2.val;
            let newval = val1.strip_suffix(val2);
            if let Some(newval) = newval {
                Ok(StringObject::new(String::from(newval)))
            } else {
                Ok(UnitObject::new())
            }
        } else {
            Err(RuntimeError::type_error("Expected strings as arguments to strip_prefix"))
        })
    } else {
        Err(RuntimeError::type_error("Expected string as argument to strip_prefix"))
    })
});