Don't store file content string in File

This commit is contained in:
Joscha 2022-01-09 20:12:06 +01:00
parent 721718eee4
commit d169037b7a
3 changed files with 16 additions and 11 deletions

View file

@ -290,16 +290,24 @@ impl Files {
pub fn save(&self) -> Result<()> {
for file in &self.files {
if file.dirty {
Self::save_file(file)?;
self.save_file(file)?;
}
}
Ok(())
}
fn save_file(file: &LoadedFile) -> Result<()> {
fn save_file(&self, file: &LoadedFile) -> Result<()> {
// TODO Sort commands within file
let previous = self
.cs_files
.get(file.cs_id)
.expect("cs id is valid")
.source();
let formatted = file.file.format(&file.removed);
if file.file.contents == formatted {
if previous == &formatted {
println!("Unchanged file {:?}", file.name);
} else {
println!("Saving file {:?}", file.name);
@ -308,6 +316,7 @@ impl Files {
error: e,
})?;
}
Ok(())
}

View file

@ -381,7 +381,6 @@ pub enum Command {
#[derive(Debug)]
pub struct File {
pub contents: String,
pub commands: Vec<Spanned<Command>>,
}
@ -389,9 +388,6 @@ impl File {
/// Create an empty dummy file. This file should only be used as a
/// placeholder value.
pub fn dummy() -> Self {
Self {
contents: String::new(),
commands: vec![],
}
Self { commands: vec![] }
}
}

View file

@ -834,7 +834,7 @@ fn parse_command(p: Pair<'_, Rule>) -> Result<Spanned<Command>> {
Ok(Spanned::new(span, command))
}
pub fn parse_file(p: Pair<'_, Rule>, contents: String) -> Result<File> {
pub fn parse_file(p: Pair<'_, Rule>) -> Result<File> {
assert_eq!(p.as_rule(), Rule::file);
let mut commands = vec![];
@ -847,7 +847,7 @@ pub fn parse_file(p: Pair<'_, Rule>, contents: String) -> Result<File> {
commands.push(parse_command(p)?);
}
Ok(File { contents, commands })
Ok(File { commands })
}
pub fn parse(path: &Path, input: &str) -> Result<File> {
@ -857,5 +857,5 @@ pub fn parse(path: &Path, input: &str) -> Result<File> {
let file_pair = pairs.next().unwrap();
assert_eq!(pairs.next(), None);
parse_file(file_pair, input.to_string()).map_err(|e| e.with_path(&pathstr))
parse_file(file_pair).map_err(|e| e.with_path(&pathstr))
}