From d169037b7a56d1b13da1a2da40f2a80ea680ae9f Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 9 Jan 2022 20:12:06 +0100 Subject: [PATCH] Don't store file content string in File --- src/files.rs | 15 ++++++++++++--- src/files/commands.rs | 6 +----- src/files/parse.rs | 6 +++--- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/files.rs b/src/files.rs index a62c396..0edc0ca 100644 --- a/src/files.rs +++ b/src/files.rs @@ -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(()) } diff --git a/src/files/commands.rs b/src/files/commands.rs index f8f6f4f..1b0ee1a 100644 --- a/src/files/commands.rs +++ b/src/files/commands.rs @@ -381,7 +381,6 @@ pub enum Command { #[derive(Debug)] pub struct File { - pub contents: String, pub commands: Vec>, } @@ -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![] } } } diff --git a/src/files/parse.rs b/src/files/parse.rs index 8637fb0..629ffea 100644 --- a/src/files/parse.rs +++ b/src/files/parse.rs @@ -834,7 +834,7 @@ fn parse_command(p: Pair<'_, Rule>) -> Result> { Ok(Spanned::new(span, command)) } -pub fn parse_file(p: Pair<'_, Rule>, contents: String) -> Result { +pub fn parse_file(p: Pair<'_, Rule>) -> Result { 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 { commands.push(parse_command(p)?); } - Ok(File { contents, commands }) + Ok(File { commands }) } pub fn parse(path: &Path, input: &str) -> Result { @@ -857,5 +857,5 @@ pub fn parse(path: &Path, input: &str) -> Result { 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)) }