Tons of cleanup. More useless git notices.
This commit is contained in:
73
src/io.rs
73
src/io.rs
@@ -1,5 +1,7 @@
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::{self, Seek, SeekFrom};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::cli::CONFIG;
|
||||
|
||||
use anyhow::Context;
|
||||
|
||||
@@ -17,28 +19,49 @@ pub fn get_stream_length<S: Seek>(stream: &mut S) -> io::Result<u64> {
|
||||
len
|
||||
}
|
||||
|
||||
/// Generates a file path if one not provided.
|
||||
/// source_name for fallback name.
|
||||
pub fn get_path<P>(
|
||||
file_path: &Option<P>,
|
||||
source_name: &P,
|
||||
extension: &str,
|
||||
) -> anyhow::Result<PathBuf>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
if let Some(f) = file_path {
|
||||
return Ok(f.as_ref().to_path_buf());
|
||||
}
|
||||
|
||||
Ok(PathBuf::from(format!(
|
||||
"{}.{}",
|
||||
source_name
|
||||
.as_ref()
|
||||
.to_str()
|
||||
.context("source_name path was not UTF-8 valid.")?,
|
||||
extension
|
||||
))
|
||||
.as_path()
|
||||
.to_owned())
|
||||
pub fn load_input() -> anyhow::Result<File> {
|
||||
OpenOptions::new()
|
||||
.read(true)
|
||||
.open(&CONFIG.input)
|
||||
.with_context(|| format!("Failed to open input file: {}", &CONFIG.input.display()))
|
||||
}
|
||||
|
||||
pub fn load_output() -> anyhow::Result<File> {
|
||||
OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.create(true)
|
||||
.open(crate::path::OUTPUT_PATH.clone())
|
||||
.with_context(|| {
|
||||
format!(
|
||||
"Failed to open/create output file at: {}",
|
||||
crate::path::OUTPUT_PATH.display()
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn load_map_read() -> anyhow::Result<File> {
|
||||
OpenOptions::new()
|
||||
.read(true)
|
||||
.open(crate::path::MAP_PATH.clone())
|
||||
.with_context(|| {
|
||||
format!(
|
||||
"Failed to open/create mapping file at: {}",
|
||||
crate::path::MAP_PATH.display()
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn load_map_write() -> anyhow::Result<File> {
|
||||
OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true) // Wipe old map. Should really make a backup first.
|
||||
.open(crate::path::MAP_PATH.clone())
|
||||
.with_context(|| {
|
||||
format!(
|
||||
"Failed to open map file at: {}",
|
||||
crate::path::MAP_PATH.display()
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user