use std::fs::read_to_string;
use std::path::PathBuf;
use std::rc::Rc;
#[derive(Clone, PartialEq, Hash, Default)]
pub struct Source {
pub contents: String,
pub path: PathBuf,
}
impl Source {
pub fn new(source: &str, path: &str) -> Rc<Source> {
Rc::new(Source {
contents: source.to_string(),
path: PathBuf::from(path),
})
}
pub fn contents(source: &str) -> Rc<Source> {
Rc::new(Source {
contents: source.to_string(),
path: PathBuf::from("./main"),
})
}
pub fn from_file(path: &str) -> Result<Rc<Source>, String> {
match read_to_string(path) {
Ok(src) => Ok(Source::new(&src, path)),
Err(err) => Err(err.to_string()),
}
}
pub fn merge(&mut self, other: Rc<Source>) {
self.contents += &other.as_ref().contents.clone();
}
pub fn len(&mut self) -> usize {
self.contents.len()
}
pub fn is_empty(&mut self) -> bool {
self.contents.is_empty()
}
}