Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crate organize with module system #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ mod errors {
}
use errors::*;

include!("util.rs");
mod util;
use util::*;

const GRAVEYARD: &str = "/tmp/graveyard";
const RECORD: &str = ".record";
Expand Down
16 changes: 10 additions & 6 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/// Concatenate two paths, even if the right argument is an absolute path.
fn join_absolute<A: AsRef<Path>, B: AsRef<Path>>(left: A, right: B) -> PathBuf {
use std::path::{PathBuf, Path};
use std::{env, fs, io};
use std::io::{BufReader, Read, Write};

pub fn join_absolute<A: AsRef<Path>, B: AsRef<Path>>(left: A, right: B) -> PathBuf {
let (left, right) = (left.as_ref(), right.as_ref());
left.join(
if let Ok(stripped) = right.strip_prefix("/") {
Expand All @@ -10,16 +14,16 @@ fn join_absolute<A: AsRef<Path>, B: AsRef<Path>>(left: A, right: B) -> PathBuf {
)
}

fn symlink_exists<P: AsRef<Path>>(path: P) -> bool {
pub fn symlink_exists<P: AsRef<Path>>(path: P) -> bool {
fs::symlink_metadata(path).is_ok()
}

fn get_user() -> String {
pub fn get_user() -> String {
env::var("USER").unwrap_or_else(|_| String::from("unknown"))
}

/// Prompt for user input, returning True if the first character is 'y' or 'Y'
fn prompt_yes<T: AsRef<str>>(prompt: T) -> bool {
pub fn prompt_yes<T: AsRef<str>>(prompt: T) -> bool {
print!("{} (y/N) ", prompt.as_ref());
if io::stdout().flush().is_err() {
// If stdout wasn't flushed properly, fallback to println
Expand All @@ -34,7 +38,7 @@ fn prompt_yes<T: AsRef<str>>(prompt: T) -> bool {
}

/// Add a numbered extension to duplicate filenames to avoid overwriting files.
fn rename_grave<G: AsRef<Path>>(grave: G) -> PathBuf {
pub fn rename_grave<G: AsRef<Path>>(grave: G) -> PathBuf {
let grave = grave.as_ref();
let name = grave.to_str().expect("Filename must be valid unicode.");
(1_u64..)
Expand All @@ -43,7 +47,7 @@ fn rename_grave<G: AsRef<Path>>(grave: G) -> PathBuf {
.expect("Failed to rename duplicate file or directory")
}

fn humanize_bytes(bytes: u64) -> String {
pub fn humanize_bytes(bytes: u64) -> String {
let values = ["bytes", "KB", "MB", "GB", "TB"];
let pair = values.iter()
.enumerate()
Expand Down