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

Create subproject oof, a thin argparsing lib #12

Merged
merged 8 commits into from
Apr 5, 2021
Merged
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
11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,33 @@ version = "0.1.4"
authors = ["Vinícius Rodrigues Miguel <vrmiguel99@gmail.com>", "João M. Bezerra <marcospb19@hotmail.com>"]
edition = "2018"
readme = "README.md"
homepage = "https://github.com/vrmiguel/ouch"
repository = "https://github.com/vrmiguel/ouch"
license = "MIT"
keywords = ["decompression", "compression", "zip", "tar", "gzip"]
categories = ["command-line-utilities", "compression", "encoding"]
description = "A command-line utility for easily compressing and decompressing files and directories."


# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
colored = "2.0.0"
walkdir = "2.3.2"
clap = "2.33.3"
tar = "0.4.33"
xz2 = "0.1.6"
bzip2 = "0.4.2"
flate2 = "1.0.14"
zip = "0.5.11"

# Dependency from workspace
oof = { path = "./oof" }

[profile.release]
lto = true
codegen-units = 1
opt-level = 3

[workspace]
members = [
".",
"oof",
]
9 changes: 9 additions & 0 deletions oof/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "oof"
version = "0.1.0"
authors = ["João M. Bezerra <marcospb19@hotmail.com>"]
edition = "2018"
description = "Ouch's argparsing library"
repository = "https://github.com/vrmiguel/ouch"

[dependencies]
35 changes: 35 additions & 0 deletions oof/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::{error, ffi::OsString, fmt};

use crate::Flag;

#[derive(Debug)]
pub enum OofError {
FlagValueConflict {
flag: Flag,
previous_value: OsString,
new_value: OsString,
},
}

impl error::Error for OofError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
None
}
}

impl fmt::Display for OofError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// TODO: implement proper debug messages
match self {
OofError::FlagValueConflict {
flag,
previous_value,
new_value,
} => write!(
f,
"CLI flag value conflicted for flag '--{}', previous: {:?}, new: {:?}.",
flag.long, previous_value, new_value
),
}
}
}
110 changes: 110 additions & 0 deletions oof/src/flags.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use std::{
collections::{BTreeMap, BTreeSet},
ffi::{OsStr, OsString},
};

/// Shallow type, created to indicate a `Flag` that accepts a argument.
///
/// ArgFlag::long(), is actually a Flag::long(), but sets a internal attribute.
///
/// Examples in here pls
#[derive(Debug)]
pub struct ArgFlag;

impl ArgFlag {
pub fn long(name: &'static str) -> Flag {
Flag {
long: name,
short: None,
takes_value: true,
}
}
}

#[derive(Debug, PartialEq, Clone)]
pub struct Flag {
// Also the name
pub long: &'static str,
pub short: Option<char>,
pub takes_value: bool,
}

impl Flag {
pub fn long(name: &'static str) -> Self {
Self {
long: name,
short: None,
takes_value: false,
}
}

pub fn short(mut self, short_flag_char: char) -> Self {
self.short = Some(short_flag_char);
self
}
}

#[derive(Default, PartialEq, Eq, Debug)]
pub struct Flags {
pub boolean_flags: BTreeSet<&'static str>,
pub argument_flags: BTreeMap<&'static str, OsString>,
}

impl Flags {
pub fn new() -> Self {
Self::default()
}
}

impl Flags {
pub fn is_present(&self, flag_name: &str) -> bool {
self.boolean_flags.contains(flag_name) || self.argument_flags.contains_key(flag_name)
}

pub fn arg(&self, flag_name: &str) -> Option<&OsString> {
self.argument_flags.get(flag_name)
}

pub fn take_arg(&mut self, flag_name: &str) -> Option<OsString> {
self.argument_flags.remove(flag_name)
}
}

#[derive(Debug)]
pub enum FlagType {
None,
Short,
Long,
}

impl FlagType {
pub fn from(text: impl AsRef<OsStr>) -> Self {
let text = text.as_ref();

let mut iter;

#[cfg(target_family = "unix")]
{
use std::os::unix::ffi::OsStrExt;
iter = text.as_bytes().iter();
}
#[cfg(target_family = "windows")]
{
use std::os::windows::ffi::OsStrExt;
iter = text.encode_wide
}

// 45 is the code for a hyphen
// Typed as 45_u16 for Windows
// Typed as 45_u8 for Unix
if let Some(45) = iter.next() {
if let Some(45) = iter.next() {
Self::Long
} else {
Self::Short
}
} else {
Self::None
}
}
}
Loading