Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions core/crates/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ pub enum DriverError {
#[error("no permission to open {}", .path.display())]
FileAccessDenied { path: PathBuf },

// A new database is made only where nothing is, so a file another program made there is kept.
#[error("a file is already at {}", .path.display())]
FileExists { path: PathBuf },

#[error("{} is not a database file", .path.display())]
NotADatabase { path: PathBuf },

Expand Down Expand Up @@ -172,6 +176,7 @@ impl DriverError {
Self::FileNotFound { .. } => "driver.file_not_found",
Self::DatabaseNotFound { .. } => "driver.database_not_found",
Self::FileAccessDenied { .. } => "driver.file_access_denied",
Self::FileExists { .. } => "driver.file_exists",
Self::NotADatabase { .. } => "driver.not_a_database",
Self::FileIo { .. } => "driver.file_io",
Self::Busy(..) => "driver.busy",
Expand Down Expand Up @@ -217,6 +222,7 @@ impl DriverError {
| Self::PartiallyApplied { .. } => ErrorCategory::WriteOutcome,
Self::FileNotFound { .. }
| Self::FileAccessDenied { .. }
| Self::FileExists { .. }
| Self::NotADatabase { .. }
| Self::FileIo { .. } => ErrorCategory::File,
Self::Busy(_) | Self::EditorSessionBusy => ErrorCategory::Busy,
Expand Down Expand Up @@ -274,6 +280,17 @@ mod tests {
);
}

#[test]
fn a_file_already_there_is_a_file_problem_with_its_own_code() {
let there = DriverError::FileExists {
path: PathBuf::from("/data/app.sqlite3"),
};

assert_eq!(there.category(), ErrorCategory::File);
assert_eq!(there.code(), "driver.file_exists");
assert!(there.to_string().contains("/data/app.sqlite3"), "{there}");
}

#[test]
fn a_saves_cause_is_found_under_its_outcome() {
let lost = DriverError::ConnectionLost {
Expand Down
23 changes: 19 additions & 4 deletions core/crates/core/src/file_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ impl FileEndpoint {
Self::with_mode(path, FileOpenMode::CreateNew)
}

pub fn replace(path: PathBuf) -> Result<Self, ConfigError> {
Self::with_mode(path, FileOpenMode::Replace)
}

fn with_mode(path: PathBuf, mode: FileOpenMode) -> Result<Self, ConfigError> {
if !path.is_absolute() {
return Err(ConfigError::RelativePath);
Expand Down Expand Up @@ -70,13 +74,24 @@ mod tests {

let created = FileEndpoint::create_new(absolute("/srv/new.sqlite")).unwrap();
assert_eq!(created.mode(), FileOpenMode::CreateNew);

let replaced = FileEndpoint::replace(absolute("/srv/old.sqlite")).unwrap();
assert_eq!(replaced.mode(), FileOpenMode::Replace);
assert_eq!(
FileEndpoint::replace(PathBuf::from("old.sqlite")),
Err(ConfigError::RelativePath)
);
}

#[test]
fn for_reopen_is_open_existing_for_the_same_path() {
let created = FileEndpoint::create_new(absolute("/srv/new.sqlite")).unwrap();
let reopened = created.for_reopen();
assert_eq!(reopened.path(), created.path());
assert_eq!(reopened.mode(), FileOpenMode::OpenExisting);
for created in [
FileEndpoint::create_new(absolute("/srv/new.sqlite")).unwrap(),
FileEndpoint::replace(absolute("/srv/new.sqlite")).unwrap(),
] {
let reopened = created.for_reopen();
assert_eq!(reopened.path(), created.path());
assert_eq!(reopened.mode(), FileOpenMode::OpenExisting);
}
}
}
3 changes: 3 additions & 0 deletions core/crates/core/src/file_open_mode.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FileOpenMode {
OpenExisting,
// Only where nothing is; a file there is refused, never replaced.
CreateNew,
// Over a file the user agreed to replace.
Replace,
}
Loading
Loading