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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use std::{convert::TryInto, path::PathBuf, sync::RwLock};

use crate::{lines_columns_indexes::LineStarts, SourceId, Span};

pub struct Source {
    pub path: PathBuf,
    pub content: String,
    pub(crate) line_starts: LineStarts,
}

#[cfg(feature = "global-source-filesystem")]
pub struct GlobalStore;

// pub struct FileSystemStore;

#[derive(Default)]
pub struct MapFileStore(Vec<Source>);

#[cfg(feature = "global-source-filesystem")]
static SOURCE_IDS_TO_FILES: RwLock<MapFileStore> = RwLock::new(MapFileStore(Vec::new()));

pub trait FileSystem: Sized {
    /// Generate a new [SourceId]
    fn new_source_id(&mut self, path: PathBuf, content: String) -> SourceId {
        self.new_source_id_with_line_starts(path, content).0
    }

    fn new_source_id_with_line_starts(
        &mut self,
        path: PathBuf,
        content: String,
    ) -> (SourceId, LineStarts);

    fn get_source<T, F: for<'a> FnOnce(&'a Source) -> T>(&self, source_id: SourceId, f: F) -> T;

    fn get_file_path_and_content(&self, source_id: SourceId) -> (PathBuf, String) {
        self.get_source(source_id, |Source { path, content, .. }| {
            (path.to_owned(), content.to_owned())
        })
    }

    fn get_file_path(&self, source_id: SourceId) -> PathBuf {
        self.get_source(source_id, |source| source.path.to_owned())
    }

    fn get_file_content(&self, source_id: SourceId) -> String {
        self.get_source(source_id, |source| source.content.to_owned())
    }

    fn get_file_whole_span(&self, source_id: SourceId) -> Span {
        self.get_source(source_id, |source| Span {
            start: 0,
            end: source
                .content
                .len()
                .try_into()
                .expect("File too large to convert into Span"),
            source: source_id,
        })
    }

    /// Note that this does clone the result
    fn get_file_slice<I: std::slice::SliceIndex<str>>(
        &self,
        source_id: SourceId,
        indexer: I,
    ) -> Option<<I::Output as ToOwned>::Owned>
    where
        I::Output: Sized + ToOwned,
    {
        self.get_source(source_id, |s| s.content.get(indexer).map(|v| v.to_owned()))
    }

    #[cfg(feature = "codespan-reporting")]
    fn into_code_span_store(&self) -> CodeSpanStore<Self> {
        CodeSpanStore(self)
    }
}

#[cfg(feature = "global-source-filesystem")]
impl FileSystem for GlobalStore {
    fn new_source_id_with_line_starts(
        &mut self,
        path: PathBuf,
        content: String,
    ) -> (SourceId, LineStarts) {
        SOURCE_IDS_TO_FILES
            .write()
            .unwrap()
            .new_source_id_with_line_starts(path, content)
    }

    fn get_source<T, F: for<'a> FnOnce(&'a Source) -> T>(&self, source_id: SourceId, f: F) -> T {
        SOURCE_IDS_TO_FILES.read().unwrap().get_source(source_id, f)
    }
}

impl FileSystem for MapFileStore {
    fn new_source_id_with_line_starts(
        &mut self,
        path: PathBuf,
        content: String,
    ) -> (SourceId, LineStarts) {
        let line_starts = LineStarts::new(&content);
        let source = Source {
            path,
            content,
            line_starts: line_starts.clone(),
        };
        self.0.push(source);
        // Import that this is after. SourceId(0) is SourceId::NULL
        (SourceId(self.0.len().try_into().unwrap()), line_starts)
    }

    fn get_source<T, F: for<'a> FnOnce(&'a Source) -> T>(&self, source_id: SourceId, f: F) -> T {
        f(&self.0[source_id.0 as usize - 1])
    }
}

#[cfg(feature = "codespan-reporting")]
pub struct CodeSpanStore<'a, T: FileSystem>(&'a T);

#[cfg(feature = "codespan-reporting")]
impl<'a, T: FileSystem> codespan_reporting::files::Files<'a> for CodeSpanStore<'a, T> {
    type FileId = SourceId;
    type Name = String;
    type Source = String;

    fn name(&'a self, id: Self::FileId) -> Result<Self::Name, codespan_reporting::files::Error> {
        Ok(self.0.get_file_path(id).display().to_string())
    }

    fn source(
        &'a self,
        id: Self::FileId,
    ) -> Result<Self::Source, codespan_reporting::files::Error> {
        Ok(self.0.get_file_content(id))
    }

    // Implementation copied from codespan codebase
    fn line_index(
        &'a self,
        id: Self::FileId,
        byte_index: usize,
    ) -> Result<usize, codespan_reporting::files::Error> {
        Ok(self.0.get_source(id, |source| {
            source
                .line_starts
                .0
                .binary_search(&byte_index)
                .unwrap_or_else(|next_line| next_line - 1)
        }))
    }

    // Implementation copied from codespan codebase
    fn line_range(
        &'a self,
        id: Self::FileId,
        line_index: usize,
    ) -> Result<std::ops::Range<usize>, codespan_reporting::files::Error> {
        self.0.get_source(id, |source| {
            /// Copied from codespan-reporting
            fn line_start(
                line_starts: &[usize],
                line_index: usize,
                source_len: usize,
            ) -> Result<usize, codespan_reporting::files::Error> {
                use std::cmp::Ordering;

                match line_index.cmp(&line_starts.len()) {
                    Ordering::Less => Ok(line_starts
                        .get(line_index)
                        .cloned()
                        .expect("failed despite previous check")),
                    Ordering::Equal => Ok(source_len),
                    Ordering::Greater => Err(codespan_reporting::files::Error::LineTooLarge {
                        given: line_index,
                        max: line_starts.len() - 1,
                    }),
                }
            }

            line_start(&source.line_starts.0, line_index, source.content.len()).and_then(
                |prev_line_start| {
                    line_start(&source.line_starts.0, line_index + 1, source.content.len())
                        .map(|next_line_start| prev_line_start..next_line_start)
                },
            )
        })
    }
}