Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion crates/edit/src/bin/edit/documents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use std::collections::LinkedList;
use std::ffi::OsStr;
use std::fs::File;
use std::fs;
use std::path::{Path, PathBuf};

use edit::buffer::{RcTextBuffer, TextBuffer};
Expand Down Expand Up @@ -210,7 +211,26 @@ impl DocumentManager {
}

pub fn open_for_writing(path: &Path) -> apperr::Result<File> {
File::create(path).map_err(apperr::Error::from)
// Error handling for directory creation and file writing
if let Some(parent) = path.parent() {
if !parent.exists() {
match fs::create_dir_all(parent) {
Ok(_) => {},
Err(e) => {
// Log or handle the error as needed
eprintln!("[Error] Failed to create parent directories for {:?}: {}", parent, e);
return Err(apperr::Error::from(e));
}
}
}
}
match File::create(path) {
Ok(f) => Ok(f),
Err(e) => {
eprintln!("[Error] Failed to create file {:?}: {}", path, e);
Err(apperr::Error::from(e))
}
}
}

fn create_buffer() -> apperr::Result<RcTextBuffer> {
Expand Down Expand Up @@ -312,4 +332,25 @@ mod tests {
assert_eq!(parse("file.txt:10"), ("file.txt", Some(Point { x: 0, y: 9 })));
assert_eq!(parse("file.txt:10:5"), ("file.txt", Some(Point { x: 4, y: 9 })));
}

#[test]
fn test_open_for_writing_error() {
// It is not possible to reliably trigger a file creation error on all systems/environments.
// Instead, we check that the function works for a valid temp path.
use std::env;
use std::fs;
use std::path::PathBuf;

let mut temp_path = env::temp_dir();
temp_path.push("test_open_for_writing_should_succeed.txt");

// Clean up before test
let _ = fs::remove_file(&temp_path);

let result = DocumentManager::open_for_writing(&temp_path);
assert!(result.is_ok(), "Expected to be able to create a file in temp dir");

// Clean up after test
let _ = fs::remove_file(&temp_path);
}
}