summaryrefslogtreecommitdiffstats
path: root/third_party/rust/jsparagus-ast/src/source_slice_list.rs
blob: c7005b36013862d67cf968d0304875bdb489dd22 (plain)
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
/// Index into SourceSliceList.items.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct SourceSliceIndex {
    index: usize,
}
impl SourceSliceIndex {
    fn new(index: usize) -> Self {
        Self { index }
    }
}

impl From<SourceSliceIndex> for usize {
    fn from(index: SourceSliceIndex) -> usize {
        index.index
    }
}

/// Set of slices inside source, including the following:
///
///   * RegExp source
///   * BigInt literal
///
/// Compared to `SourceAtomSet`, this is not atomized.
#[derive(Debug)]
pub struct SourceSliceList<'alloc> {
    items: Vec<&'alloc str>,
}

impl<'alloc> SourceSliceList<'alloc> {
    pub fn new() -> Self {
        Self { items: Vec::new() }
    }

    pub fn push(&mut self, s: &'alloc str) -> SourceSliceIndex {
        let index = self.items.len();
        self.items.push(s);
        SourceSliceIndex::new(index)
    }

    pub fn get(&self, index: SourceSliceIndex) -> &'alloc str {
        self.items.get(usize::from(index)).unwrap()
    }
}

impl<'alloc> From<SourceSliceList<'alloc>> for Vec<&'alloc str> {
    fn from(set: SourceSliceList<'alloc>) -> Vec<&'alloc str> {
        set.items.into_iter().collect()
    }
}