forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.rs
More file actions
139 lines (119 loc) · 3.68 KB
/
Copy pathfiles.rs
File metadata and controls
139 lines (119 loc) · 3.68 KB
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
use crate::utils::keccak;
use crate::Span;
use codespan_reporting as cs;
use cs::files::Error as CsError;
use std::collections::HashMap;
use std::convert::TryInto;
use std::ops::Range;
use std::path::Path;
use std::{fs, io};
pub struct SourceFile {
id: SourceFileId,
name: String,
content: String,
line_starts: Vec<usize>,
}
#[derive(PartialEq, Copy, Clone, Eq, Hash, Debug)]
pub struct SourceFileId(pub u128);
impl SourceFile {
pub fn new(name: &str, content: &str) -> Self {
let hash = keccak::full_as_bytes(content.as_bytes());
let line_starts = cs::files::line_starts(&content).collect();
Self {
id: SourceFileId(u128::from_be_bytes(hash[..16].try_into().unwrap())),
name: name.to_string(),
content: content.to_string(),
line_starts,
}
}
pub fn line_index(&self, byte_index: usize) -> usize {
self.line_starts
.binary_search(&byte_index)
.unwrap_or_else(|next_line| next_line - 1)
}
pub fn line_span(&self, line_index: usize) -> Option<Span> {
let end = if line_index == self.line_starts.len() - 1 {
self.content.len()
} else {
*self.line_starts.get(line_index + 1)?
};
Some(Span::new(*self.line_starts.get(line_index)?, end))
}
}
pub trait FileLoader {
fn load_file(&self, path: &Path) -> io::Result<String>;
}
pub struct OsFileLoader;
impl FileLoader for OsFileLoader {
fn load_file(&self, path: &Path) -> io::Result<String> {
fs::read_to_string(path)
}
}
pub struct FileStore {
files: HashMap<SourceFileId, SourceFile>,
loader: Box<dyn FileLoader>,
}
impl FileStore {
pub fn new() -> Self {
Self {
files: HashMap::new(),
loader: Box::new(OsFileLoader),
}
}
pub fn with_loader(loader: Box<dyn FileLoader>) -> Self {
Self {
files: HashMap::new(),
loader,
}
}
pub fn add_file(&mut self, path: &str, content: &str) -> SourceFileId {
let file = SourceFile::new(path, content);
let id = file.id;
self.files.insert(id, file);
id
}
pub fn load_file(&mut self, path: &str) -> io::Result<(String, SourceFileId)> {
let content = self.loader.load_file(&Path::new(&path))?;
let id = self.add_file(path, &content);
Ok((content, id))
}
pub fn get_file(&self, id: SourceFileId) -> Option<&SourceFile> {
self.files.get(&id)
}
}
impl<'a> cs::files::Files<'a> for FileStore {
type FileId = SourceFileId;
type Name = &'a str;
type Source = &'a str;
fn name(&'a self, id: SourceFileId) -> Result<Self::Name, CsError> {
self.get_file(id)
.map(|file| file.name.as_str())
.ok_or(CsError::FileMissing)
}
fn source(&'a self, id: SourceFileId) -> Result<Self::Source, CsError> {
self.get_file(id)
.map(|file| file.content.as_str())
.ok_or(CsError::FileMissing)
}
fn line_index(&'a self, id: SourceFileId, byte_index: usize) -> Result<usize, CsError> {
Ok(self
.get_file(id)
.ok_or(CsError::FileMissing)?
.line_index(byte_index))
}
fn line_range(&'a self, id: SourceFileId, line_index: usize) -> Result<Range<usize>, CsError> {
let file = self.get_file(id).ok_or(CsError::FileMissing)?;
Ok(file
.line_span(line_index)
.ok_or(CsError::LineTooLarge {
given: line_index,
max: file.line_starts.len() - 1,
})?
.into())
}
}
impl Default for FileStore {
fn default() -> Self {
Self::new()
}
}