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
use std::path::PathBuf;
use std::io::Read;
use std::fs::File;
use std::rc::Rc;
#[derive(Debug, PartialEq, Eq)]
pub struct Source {
pub contents: String,
pub path: PathBuf,
}
impl Source {
pub fn new(source: &str, path: PathBuf) -> Rc<Source> {
Rc::new(Source { contents: source.to_string(), path })
}
pub fn path(path: PathBuf) -> std::io::Result<Rc<Source>> {
let mut source = String::new();
let mut file = File::open(path.clone())?;
file.read_to_string(&mut source)?;
Ok(Source::new(&source, path))
}
pub fn source(source: &str) -> Rc<Source> {
Source::new(&source.to_string(), PathBuf::from("./source"))
}
}