mirror of
https://github.com/loot/libloot.git
synced 2026-07-27 14:16:01 -07:00
Replace overwrite boolean with an enum
This commit is contained in:
+15
-3
@@ -4,7 +4,7 @@ use std::{
|
||||
};
|
||||
|
||||
use delegate::delegate;
|
||||
use libloot::error::DatabaseLockPoisonError;
|
||||
use libloot::{WriteMode, error::DatabaseLockPoisonError};
|
||||
|
||||
use crate::{
|
||||
UnsupportedEnumValueError, VerboseError,
|
||||
@@ -54,10 +54,16 @@ impl Database {
|
||||
output_path: &str,
|
||||
overwrite: bool,
|
||||
) -> Result<(), VerboseError> {
|
||||
let write_mode = if overwrite {
|
||||
WriteMode::CreateOrTruncate
|
||||
} else {
|
||||
WriteMode::Create
|
||||
};
|
||||
|
||||
self.0
|
||||
.read()
|
||||
.map_err(|_| DatabaseLockPoisonError)?
|
||||
.write_user_metadata(Path::new(output_path), overwrite)
|
||||
.write_user_metadata(Path::new(output_path), write_mode)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
@@ -66,10 +72,16 @@ impl Database {
|
||||
output_path: &str,
|
||||
overwrite: bool,
|
||||
) -> Result<(), VerboseError> {
|
||||
let write_mode = if overwrite {
|
||||
WriteMode::CreateOrTruncate
|
||||
} else {
|
||||
WriteMode::Create
|
||||
};
|
||||
|
||||
self.0
|
||||
.read()
|
||||
.map_err(|_| DatabaseLockPoisonError)?
|
||||
.write_minimal_list(Path::new(output_path), overwrite)
|
||||
.write_minimal_list(Path::new(output_path), write_mode)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
|
||||
+15
-3
@@ -4,7 +4,7 @@ use std::{
|
||||
sync::{Arc, RwLock},
|
||||
};
|
||||
|
||||
use libloot::error::DatabaseLockPoisonError;
|
||||
use libloot::{WriteMode, error::DatabaseLockPoisonError};
|
||||
use pyo3::{
|
||||
Bound, PyResult, pyclass, pymethods,
|
||||
types::{PyAnyMethods, PyTypeMethods},
|
||||
@@ -54,10 +54,16 @@ impl Database {
|
||||
output_path: PathBuf,
|
||||
overwrite: bool,
|
||||
) -> Result<(), VerboseError> {
|
||||
let write_mode = if overwrite {
|
||||
WriteMode::CreateOrTruncate
|
||||
} else {
|
||||
WriteMode::Create
|
||||
};
|
||||
|
||||
self.0
|
||||
.read()
|
||||
.map_err(|_| DatabaseLockPoisonError)?
|
||||
.write_user_metadata(&output_path, overwrite)
|
||||
.write_user_metadata(&output_path, write_mode)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
@@ -66,10 +72,16 @@ impl Database {
|
||||
output_path: PathBuf,
|
||||
overwrite: bool,
|
||||
) -> Result<(), VerboseError> {
|
||||
let write_mode = if overwrite {
|
||||
WriteMode::CreateOrTruncate
|
||||
} else {
|
||||
WriteMode::Create
|
||||
};
|
||||
|
||||
self.0
|
||||
.read()
|
||||
.map_err(|_| DatabaseLockPoisonError)?
|
||||
.write_minimal_list(&output_path, overwrite)
|
||||
.write_minimal_list(&output_path, write_mode)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
|
||||
+29
-20
@@ -20,6 +20,15 @@ use crate::{
|
||||
};
|
||||
pub use error::{ConditionEvaluationError, MetadataRetrievalError};
|
||||
|
||||
/// Control behaviour when writing to files.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||
pub enum WriteMode {
|
||||
/// Create the file if it does not exist, otherwise error.
|
||||
Create,
|
||||
/// Create the file if it does not exist, otherwise replace its contents.
|
||||
CreateOrTruncate,
|
||||
}
|
||||
|
||||
/// The interface through which metadata can be accessed.
|
||||
#[derive(Debug)]
|
||||
pub struct Database {
|
||||
@@ -85,9 +94,9 @@ impl Database {
|
||||
pub fn write_user_metadata(
|
||||
&self,
|
||||
output_path: &Path,
|
||||
overwrite: bool,
|
||||
mode: WriteMode,
|
||||
) -> Result<(), WriteMetadataError> {
|
||||
validate_write_path(output_path, overwrite)?;
|
||||
validate_write_path(output_path, mode)?;
|
||||
|
||||
self.userlist.save(output_path)
|
||||
}
|
||||
@@ -100,9 +109,9 @@ impl Database {
|
||||
pub fn write_minimal_list(
|
||||
&self,
|
||||
output_path: &Path,
|
||||
overwrite: bool,
|
||||
mode: WriteMode,
|
||||
) -> Result<(), WriteMetadataError> {
|
||||
validate_write_path(output_path, overwrite)?;
|
||||
validate_write_path(output_path, mode)?;
|
||||
|
||||
let mut doc = MetadataDocument::default();
|
||||
|
||||
@@ -285,13 +294,13 @@ impl Database {
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_write_path(output_path: &Path, overwrite: bool) -> Result<(), WriteMetadataError> {
|
||||
fn validate_write_path(output_path: &Path, mode: WriteMode) -> Result<(), WriteMetadataError> {
|
||||
if !output_path.parent().map(|p| p.exists()).unwrap_or(false) {
|
||||
Err(WriteMetadataError::new(
|
||||
output_path.into(),
|
||||
WriteMetadataErrorReason::ParentDirectoryNotFound,
|
||||
))
|
||||
} else if !overwrite && output_path.exists() {
|
||||
} else if mode == WriteMode::Create && output_path.exists() {
|
||||
Err(WriteMetadataError::new(
|
||||
output_path.into(),
|
||||
WriteMetadataErrorReason::PathAlreadyExists,
|
||||
@@ -487,7 +496,7 @@ plugins:
|
||||
|
||||
let output_path = fixture.inner.local_path.join("userlist.yaml");
|
||||
database
|
||||
.write_user_metadata(&output_path, false)
|
||||
.write_user_metadata(&output_path, WriteMode::Create)
|
||||
.unwrap();
|
||||
|
||||
let content = std::fs::read_to_string(output_path).unwrap();
|
||||
@@ -503,7 +512,7 @@ plugins:
|
||||
|
||||
assert!(
|
||||
database
|
||||
.write_user_metadata(&output_path, false)
|
||||
.write_user_metadata(&output_path, WriteMode::Create)
|
||||
.is_ok()
|
||||
);
|
||||
}
|
||||
@@ -516,7 +525,7 @@ plugins:
|
||||
|
||||
assert!(
|
||||
database
|
||||
.write_user_metadata(&output_path, true)
|
||||
.write_user_metadata(&output_path, WriteMode::CreateOrTruncate)
|
||||
.is_ok()
|
||||
);
|
||||
}
|
||||
@@ -531,7 +540,7 @@ plugins:
|
||||
|
||||
assert!(
|
||||
database
|
||||
.write_user_metadata(&output_path, true)
|
||||
.write_user_metadata(&output_path, WriteMode::CreateOrTruncate)
|
||||
.is_ok()
|
||||
);
|
||||
}
|
||||
@@ -544,7 +553,7 @@ plugins:
|
||||
|
||||
assert!(
|
||||
database
|
||||
.write_user_metadata(&output_path, false)
|
||||
.write_user_metadata(&output_path, WriteMode::Create)
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
@@ -563,7 +572,7 @@ plugins:
|
||||
|
||||
assert!(
|
||||
database
|
||||
.write_user_metadata(&output_path, true)
|
||||
.write_user_metadata(&output_path, WriteMode::CreateOrTruncate)
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
@@ -578,7 +587,7 @@ plugins:
|
||||
|
||||
assert!(
|
||||
database
|
||||
.write_user_metadata(&output_path, false)
|
||||
.write_user_metadata(&output_path, WriteMode::Create)
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
@@ -599,7 +608,7 @@ plugins:
|
||||
|
||||
assert!(
|
||||
database
|
||||
.write_minimal_list(&output_path, false)
|
||||
.write_minimal_list(&output_path, WriteMode::Create)
|
||||
.is_ok()
|
||||
);
|
||||
|
||||
@@ -643,7 +652,7 @@ plugins:
|
||||
|
||||
assert!(
|
||||
database
|
||||
.write_minimal_list(&output_path, false)
|
||||
.write_minimal_list(&output_path, WriteMode::Create)
|
||||
.is_ok()
|
||||
);
|
||||
}
|
||||
@@ -656,7 +665,7 @@ plugins:
|
||||
|
||||
assert!(
|
||||
database
|
||||
.write_minimal_list(&output_path, true)
|
||||
.write_minimal_list(&output_path, WriteMode::CreateOrTruncate)
|
||||
.is_ok()
|
||||
);
|
||||
}
|
||||
@@ -671,7 +680,7 @@ plugins:
|
||||
|
||||
assert!(
|
||||
database
|
||||
.write_minimal_list(&output_path, true)
|
||||
.write_minimal_list(&output_path, WriteMode::CreateOrTruncate)
|
||||
.is_ok()
|
||||
);
|
||||
}
|
||||
@@ -684,7 +693,7 @@ plugins:
|
||||
|
||||
assert!(
|
||||
database
|
||||
.write_minimal_list(&output_path, false)
|
||||
.write_minimal_list(&output_path, WriteMode::Create)
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
@@ -703,7 +712,7 @@ plugins:
|
||||
|
||||
assert!(
|
||||
database
|
||||
.write_minimal_list(&output_path, true)
|
||||
.write_minimal_list(&output_path, WriteMode::CreateOrTruncate)
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
@@ -718,7 +727,7 @@ plugins:
|
||||
|
||||
assert!(
|
||||
database
|
||||
.write_minimal_list(&output_path, false)
|
||||
.write_minimal_list(&output_path, WriteMode::Create)
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@ mod sorting;
|
||||
mod tests;
|
||||
mod version;
|
||||
|
||||
pub use database::Database;
|
||||
pub use database::{Database, WriteMode};
|
||||
pub use game::{Game, GameType};
|
||||
pub use logging::{LogLevel, set_log_level, set_logging_callback};
|
||||
pub use plugin::Plugin;
|
||||
|
||||
Reference in New Issue
Block a user