Skip to content

Commit

Permalink
Make remaining modules public in order to gain the documentation check
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielSimonetto committed Oct 30, 2021
1 parent 70b787b commit 7c82f2b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/dialogs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@ use crate::utils::colors;

/// Represents a confirmation dialog
pub struct Confirmation<'a> {
/// Represents the message to the displayed
/// e.g.: "Do you want to overwrite 'FILE'?"
pub prompt: &'a str,

/// Represents a placeholder to be changed at runtime
/// e.g.: Some("FILE")
pub placeholder: Option<&'a str>,
}

impl<'a> Confirmation<'a> {
/// New Confirmation
pub const fn new(prompt: &'a str, pattern: Option<&'a str>) -> Self {
Self { prompt, placeholder: pattern }
}

/// Creates user message and receives a boolean input to be used on the program
pub fn ask(&self, substitute: Option<&'a str>) -> crate::Result<bool> {
let message = match (self.placeholder, substitute) {
(None, _) => Cow::Borrowed(self.prompt),
Expand Down
23 changes: 23 additions & 0 deletions src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{ffi::OsStr, fmt, path::Path};

use self::CompressionFormat::*;

#[allow(missing_docs)]
#[derive(Clone, PartialEq, Eq, Debug)]
/// Accepted extensions for input and output
pub enum CompressionFormat {
Expand Down Expand Up @@ -40,6 +41,19 @@ impl fmt::Display for CompressionFormat {
}
}

// use crate::extension::CompressionFormat::*;
//

/// Extracts extensions from a path,
/// return both the remaining path and the list of extension objects
///
/// ```rust
/// use ouch::extension::{separate_known_extensions_from_name, CompressionFormat};
/// use std::path::Path;
///
/// let mut path = Path::new("bolovo.tar.gz");
/// assert_eq!(separate_known_extensions_from_name(&path), (Path::new("bolovo"), vec![CompressionFormat::Tar, CompressionFormat::Gzip]));
/// ```
pub fn separate_known_extensions_from_name(mut path: &Path) -> (&Path, Vec<CompressionFormat>) {
// // TODO: check for file names with the name of an extension
// // TODO2: warn the user that currently .tar.gz is a .gz file named .tar
Expand Down Expand Up @@ -76,6 +90,15 @@ pub fn separate_known_extensions_from_name(mut path: &Path) -> (&Path, Vec<Compr
(path, extensions)
}

/// Extracts extensions from a path, return only the list of extension objects
///
/// ```rust
/// use ouch::extension::{extensions_from_path, CompressionFormat};
/// use std::path::Path;
///
/// let mut path = Path::new("bolovo.tar.gz");
/// assert_eq!(extensions_from_path(&path), vec![CompressionFormat::Tar, CompressionFormat::Gzip]);
/// ```
pub fn extensions_from_path(path: &Path) -> Vec<CompressionFormat> {
let (_, extensions) = separate_known_extensions_from_name(path);
extensions
Expand Down
12 changes: 5 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
pub mod archive;
pub mod cli;
pub mod commands;

// Private modules
mod dialogs;
mod error;
mod extension;
mod macros;
mod utils;
pub mod dialogs;
pub mod error;
pub mod extension;
pub mod macros;
pub mod utils;

pub use error::{Error, Result};

Expand Down
3 changes: 3 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Macros used on ouch.

#[macro_export]
/// Macro that prints message in INFO mode
macro_rules! info {
Expand All @@ -7,6 +9,7 @@ macro_rules! info {
};
}

/// Prints the `[Info]` tag
pub fn _info_helper() {
use crate::utils::colors::{RESET, YELLOW};

Expand Down
15 changes: 14 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Utils used on ouch.

use std::{
cmp, env,
ffi::OsStr,
Expand All @@ -15,6 +17,7 @@ pub fn dir_is_empty(dir_path: &Path) -> bool {
dir_path.read_dir().map(is_empty).unwrap_or_default()
}

/// Creates the dir if non existent.
pub fn create_dir_if_non_existent(path: &Path) -> crate::Result<()> {
if !path.exists() {
fs::create_dir_all(path)?;
Expand All @@ -23,6 +26,9 @@ pub fn create_dir_if_non_existent(path: &Path) -> crate::Result<()> {
Ok(())
}

/// Removes the current dir from the beginning of a path
/// normally used for presentation sake.
/// If this function fails, it will return source path as a PathBuf.
pub fn strip_cur_dir(source_path: &Path) -> PathBuf {
source_path
.strip_prefix(Component::CurDir)
Expand All @@ -43,6 +49,8 @@ pub fn cd_into_same_dir_as(filename: &Path) -> crate::Result<PathBuf> {
Ok(previous_location)
}

/// Centralizes the decision of overwriting a file or not,
/// whether the user has already passed a question_policy or not.
pub fn user_wants_to_overwrite(path: &Path, question_policy: QuestionPolicy) -> crate::Result<bool> {
match question_policy {
QuestionPolicy::AlwaysYes => Ok(true),
Expand All @@ -56,11 +64,13 @@ pub fn user_wants_to_overwrite(path: &Path, question_policy: QuestionPolicy) ->
}
}

/// Converts an OsStr to utf8.
pub fn to_utf(os_str: impl AsRef<OsStr>) -> String {
let text = format!("{:?}", os_str.as_ref());
text.trim_matches('"').to_string()
}

/// Treats weird paths for better user messages.
pub fn nice_directory_display(os_str: impl AsRef<OsStr>) -> String {
let text = to_utf(os_str);
if text == "." {
Expand All @@ -70,6 +80,7 @@ pub fn nice_directory_display(os_str: impl AsRef<OsStr>) -> String {
}
}

/// Struct used to overload functionality onto Byte presentation.
pub struct Bytes {
bytes: f64,
}
Expand All @@ -86,6 +97,7 @@ pub mod colors {
macro_rules! color {
($name:ident = $value:literal) => {
#[cfg(target_family = "unix")]
/// Inserts color onto text based on configuration
pub static $name: Lazy<&str> = Lazy::new(|| if *DISABLE_COLORED_TEXT { "" } else { $value });
#[cfg(not(target_family = "unix"))]
pub static $name: &&str = &"";
Expand All @@ -106,6 +118,7 @@ pub mod colors {
impl Bytes {
const UNIT_PREFIXES: [&'static str; 6] = ["", "k", "M", "G", "T", "P"];

/// New Byte structure
pub fn new(bytes: u64) -> Self {
Self { bytes: bytes as f64 }
}
Expand All @@ -129,7 +142,7 @@ impl std::fmt::Display for Bytes {
#[derive(Debug, PartialEq, Clone, Copy)]
/// How overwrite questions should be handled
pub enum QuestionPolicy {
/// Ask ever time
/// Ask everytime
Ask,
/// Skip overwrite questions positively
AlwaysYes,
Expand Down

0 comments on commit 7c82f2b

Please sign in to comment.