Implement EditorBuffer::save_file()
Needs more tests though.
This commit is contained in:
parent
acd456c22a
commit
47a5ccb575
|
|
@ -6,3 +6,4 @@ edition = "2024"
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
ntest = "0.9.3"
|
ntest = "0.9.3"
|
||||||
rand = {version="0.9.2", features=["small_rng", "alloc"]}
|
rand = {version="0.9.2", features=["small_rng", "alloc"]}
|
||||||
|
tempfile = "3.23.0"
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use super::{CommandResponse, EditorBuffer};
|
use super::{CommandResponse, EditorBuffer};
|
||||||
use crate::{Point, TextBuffer, TextBufferWriter};
|
use crate::{Point, TextBuffer, TextBufferReader, TextBufferWriter};
|
||||||
|
|
||||||
impl EditorBuffer {
|
impl EditorBuffer {
|
||||||
pub fn open_file(&mut self, filepath: PathBuf) -> CommandResponse {
|
pub fn open_file(&mut self, filepath: PathBuf) -> CommandResponse {
|
||||||
|
|
@ -35,16 +35,31 @@ impl EditorBuffer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save_file(&mut self, _filepath: Option<PathBuf>) -> CommandResponse {
|
pub fn save_file(&mut self, filepath: Option<PathBuf>) -> CommandResponse {
|
||||||
todo!()
|
if let Some(filepath) = filepath.as_ref().or(self.filepath.as_ref()) {
|
||||||
|
match std::fs::File::create(filepath) {
|
||||||
|
Ok(mut file) => {
|
||||||
|
match std::io::copy(&mut TextBufferReader::new(&self.buffer), &mut file) {
|
||||||
|
Ok(bytes_read) => CommandResponse::Success(format!(
|
||||||
|
"Read {bytes_read} bytes to \"{}\"",
|
||||||
|
filepath.to_string_lossy()
|
||||||
|
)),
|
||||||
|
Err(err) => CommandResponse::Failure(format!("{}", err)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => CommandResponse::Failure(format!("{}", err)),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
CommandResponse::Failure("Attempting to same file with no name.".into())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{Command, CommandResponse, TextBufferReader, EditorBuffer};
|
use crate::{Command, CommandResponse, EditorBuffer, TextBufferReader};
|
||||||
use std::io::Read;
|
use std::{io::Read, path::PathBuf};
|
||||||
use std::path::PathBuf;
|
use tempfile::tempdir;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn load_file_loads_expected_contents() {
|
fn load_file_loads_expected_contents() {
|
||||||
|
|
@ -81,4 +96,26 @@ mod tests {
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn save_file_saves_file_with_expected_contents() {
|
||||||
|
let test_file_path: PathBuf = [
|
||||||
|
env!("CARGO_MANIFEST_DIR"),
|
||||||
|
r"test_data/Les_Trois_Mousquetaires.txt",
|
||||||
|
]
|
||||||
|
.iter()
|
||||||
|
.collect();
|
||||||
|
let mut target = EditorBuffer::new();
|
||||||
|
target.execute(Command::OpenFile(test_file_path.clone()));
|
||||||
|
let temp_dir = tempdir().unwrap();
|
||||||
|
let tmp_file_path = temp_dir.path().join(r"Les_Trois_Mousquetaires.txt");
|
||||||
|
assert!(matches!(
|
||||||
|
dbg!(target.execute(Command::SaveAs(tmp_file_path.clone()))),
|
||||||
|
CommandResponse::Success(_)
|
||||||
|
));
|
||||||
|
|
||||||
|
let read_text = std::fs::read_to_string(&tmp_file_path).unwrap();
|
||||||
|
let expected_text = std::fs::read_to_string(&test_file_path).unwrap();
|
||||||
|
assert_eq!(&expected_text, &read_text);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue