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
use crate::{
    Error,
};

#[derive(Debug,Clone,Copy,Eq,PartialEq,Ord,PartialOrd)]
pub struct Snip {
    pub offset: usize,
    pub length: usize,
}

pub trait Localize: Sized {
    fn localize(self, chars: Snip, bytes: Snip) -> Local<Self> {
        Local { chars, bytes, data: self }
    }
}
impl<T: Sized> Localize for T {}

#[derive(Debug,Clone,Copy,Eq,PartialEq,Ord,PartialOrd)]
pub struct Local<E> {
    chars: Snip,
    bytes: Snip,
    data: E,
}
impl<E> Local<E> {
    pub fn into_inner(self) -> (Local<()>,E) {
        (Local {
            chars: self.chars,
            bytes: self.bytes,
            data: (),
        }, self.data)
    }
    pub fn data(&self) -> &E {
        &self.data
    }
    pub fn chars(&self) -> Snip {
        self.chars
    }
    pub fn bytes(&self) -> Snip {
        self.bytes
    }

    pub fn local<T>(&self, data: T) -> Local<T>
    {
        Local {
            chars: self.chars,
            bytes: self.bytes,
            data,
        }
    }
    pub fn map<F,T>(self, mut mapper: F) -> Local<T>
    where F: FnMut(E) -> T
    {
        Local {
            chars: self.chars,
            bytes: self.bytes,
            data: mapper(self.data),
        }
    }
    pub fn with_inner<T>(self, inner: T) -> Local<T> {
        Local {
            chars: self.chars,
            bytes: self.bytes,
            data: inner,
        }
    }
    pub fn with_shift(mut self, char_offset: usize, byte_offset: usize) -> Local<E> {
        self.chars.offset += char_offset;
        self.bytes.offset += byte_offset;
        self
    }

    pub fn from_segment<T>(begin: Local<E>, end: Local<T>) -> Result<Local<E>,Error> {
        if (begin.chars.offset <= end.chars.offset) && (begin.bytes.offset <= end.bytes.offset) {
            Ok(Local {
                chars: Snip {
                    offset: begin.chars.offset,
                    length: end.chars.length + end.chars.offset - begin.chars.offset,
                },
                bytes: Snip {
                    offset: begin.bytes.offset,
                    length: end.bytes.length + end.bytes.offset - begin.bytes.offset,
                },
                data: begin.data,
            })
        } else {
            Err(Error::EndBeforeBegin)
        }
    }
}