Skip to content

Commit

Permalink
refactor: Utilize same template creation code
Browse files Browse the repository at this point in the history
  • Loading branch information
brianp committed Dec 14, 2019
1 parent 9f1f04e commit 46ecae5
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 32 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions new/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ pub fn exec(args: Args) -> Result<(), String> {
}

fn modified_template(template: &str, file: &PathBuf) -> String {
template.replace("{file}", file.to_str().unwrap())
template.replace("{file}", file.to_str().expect("Couldn't convert the {:?} path into a String to write into the new file"))
}

fn write_template<S>(template: S, path: &PathBuf, force: bool) -> Result<(), String>
pub fn write_template<S>(template: S, path: &PathBuf, force: bool) -> Result<(), String>
where
S: Into<String>,
{
Expand Down
1 change: 1 addition & 0 deletions snapshot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ doctest = false

[dependencies]
common = { path = "../common" }
new = { path = "../new" }
regex = "1.3.1"
serde = { version = "1.0.103", features = ["derive"] }
serde_yaml = "0.8"
32 changes: 2 additions & 30 deletions snapshot/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Muxedsnapshot. A tmux session cloner for Muxed.
extern crate common;
extern crate new;
extern crate regex;
extern crate serde;
extern crate serde_yaml;
Expand All @@ -10,9 +11,7 @@ pub mod tmux;
use common::args::Args;
use common::first_run::check_first_run;
use common::project_paths::project_paths;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use new::write_template as write_config;

/// The main execution method.
/// Accepts two arguments. -n for the name of the project file and -t to target
Expand Down Expand Up @@ -45,33 +44,6 @@ pub fn exec(args: Args) -> Result<(), String> {
Ok(())
}

/// Write the new file
fn write_config<S>(template: S, path: &PathBuf, force: bool) -> Result<(), String>
where
S: Into<String>,
{
let path_str = path.to_str().expect("Path could not be opened");
let mut file = OpenOptions::new()
.write(true)
.truncate(force)
.create(force)
.create_new(!force)
.open(path)
.map_err(|e| format!("Could not create the file {}. Error: {}", &path_str, e))?;

file.write_all(template.into().as_bytes()).map_err(|e| {
format!(
"Could not write contents of template to the file {}. Error {}",
&path_str, e
)
})?;

file.sync_all()
.map_err(|e| format!("Could not sync OS data post-write. Error: {}", e))?;

Ok(())
}

#[cfg(test)]
mod test {
use common::rand_names;
Expand Down

0 comments on commit 46ecae5

Please # to comment.