From b0d02e7f43841ffaf76893994317916e04ee6036 Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Fri, 24 Apr 2020 17:18:23 -0500 Subject: [PATCH] change ~ reorganize code layout/structure --- src/uucore/Cargo.toml | 4 +- src/uucore/src/{lib.rs => lib/features.rs} | 45 +++-------- src/uucore/src/{ => lib/features}/encoding.rs | 4 + src/uucore/src/{ => lib/features}/entries.rs | 0 src/uucore/src/{ => lib/features}/fs.rs | 4 +- src/uucore/src/{ => lib/features}/mode.rs | 4 +- .../src/{ => lib/features}/parse_time.rs | 0 src/uucore/src/{ => lib/features}/process.rs | 1 - src/uucore/src/{ => lib/features}/signals.rs | 0 src/uucore/src/{ => lib/features}/utmpx.rs | 3 - src/uucore/src/{ => lib/features}/wide.rs | 0 .../mod.rs => lib/features/zero_copy.rs} | 0 .../features/zero_copy/platform.rs} | 0 .../features}/zero_copy/platform/default.rs | 2 +- .../features}/zero_copy/platform/linux.rs | 4 +- .../features}/zero_copy/platform/unix.rs | 2 +- .../features}/zero_copy/platform/windows.rs | 2 +- src/uucore/src/lib/lib.rs | 75 +++++++++++++++++++ src/uucore/src/{ => lib}/macros.rs | 34 +++++++++ src/uucore/src/lib/mods.rs | 6 ++ src/uucore/src/{ => lib/mods}/coreopts.rs | 4 +- src/uucore/src/{ => lib/mods}/panic.rs | 0 22 files changed, 141 insertions(+), 53 deletions(-) rename src/uucore/src/{lib.rs => lib/features.rs} (51%) rename src/uucore/src/{ => lib/features}/encoding.rs (98%) rename src/uucore/src/{ => lib/features}/entries.rs (100%) rename src/uucore/src/{ => lib/features}/fs.rs (99%) rename src/uucore/src/{ => lib/features}/mode.rs (97%) rename src/uucore/src/{ => lib/features}/parse_time.rs (100%) rename src/uucore/src/{ => lib/features}/process.rs (99%) rename src/uucore/src/{ => lib/features}/signals.rs (100%) rename src/uucore/src/{ => lib/features}/utmpx.rs (99%) rename src/uucore/src/{ => lib/features}/wide.rs (100%) rename src/uucore/src/{zero_copy/mod.rs => lib/features/zero_copy.rs} (100%) rename src/uucore/src/{zero_copy/platform/mod.rs => lib/features/zero_copy/platform.rs} (100%) rename src/uucore/src/{ => lib/features}/zero_copy/platform/default.rs (90%) rename src/uucore/src/{ => lib/features}/zero_copy/platform/linux.rs (97%) rename src/uucore/src/{ => lib/features}/zero_copy/platform/unix.rs (85%) rename src/uucore/src/{ => lib/features}/zero_copy/platform/windows.rs (87%) create mode 100644 src/uucore/src/lib/lib.rs rename src/uucore/src/{ => lib}/macros.rs (85%) create mode 100644 src/uucore/src/lib/mods.rs rename src/uucore/src/{ => lib/mods}/coreopts.rs (98%) rename src/uucore/src/{ => lib/mods}/panic.rs (100%) diff --git a/src/uucore/Cargo.toml b/src/uucore/Cargo.toml index 5150212ae..f26ba6418 100644 --- a/src/uucore/Cargo.toml +++ b/src/uucore/Cargo.toml @@ -15,6 +15,9 @@ license = "MIT" appveyor = { repository = "uutils/uucore" } travis-ci = { repository = "uutils/uucore" } +[lib] +path="src/lib/lib.rs" + [dependencies] dunce = "1.0.0" getopts = "<= 0.2.21" @@ -34,7 +37,6 @@ data-encoding = { version = "~2.1", optional = true } # * libc: initial utmp support added in 0.2.15, but 0.2.68 break the build for MinSRV 1.31.0 libc = { version = "0.2.15, <= 0.2.66", optional = true } - [target.'cfg(target_os = "redox")'.dependencies] termion = "1.5" diff --git a/src/uucore/src/lib.rs b/src/uucore/src/lib/features.rs similarity index 51% rename from src/uucore/src/lib.rs rename to src/uucore/src/lib/features.rs index 4362758ad..438963135 100644 --- a/src/uucore/src/lib.rs +++ b/src/uucore/src/lib/features.rs @@ -1,33 +1,6 @@ -extern crate wild; +// features ~ feature-gated modules (core/bundler file) -pub fn args() -> impl Iterator { - wild::args() -} - -#[cfg(feature = "failure")] -extern crate failure; -#[cfg(feature = "libc")] -pub extern crate libc; -#[cfg(feature = "winapi")] -pub extern crate winapi; -#[cfg(feature = "failure_derive")] -#[macro_use] -extern crate failure_derive; -#[cfg(feature = "nix")] -extern crate nix; -#[cfg(all(feature = "lazy_static", target_os = "linux"))] -#[macro_use] -extern crate lazy_static; -#[cfg(feature = "platform-info")] -extern crate platform_info; - -#[macro_use] -mod macros; - -#[macro_use] -pub mod coreopts; - -pub mod panic; +// spell-checker:ignore (uucore/uutils) coreopts libc musl utmpx uucore uutils winapi #[cfg(feature = "encoding")] pub mod encoding; @@ -35,11 +8,16 @@ pub mod encoding; pub mod fs; #[cfg(feature = "parse_time")] pub mod parse_time; +#[cfg(feature = "zero-copy")] +pub mod zero_copy; -#[cfg(all(unix, feature = "entries"))] -pub mod entries; +// * (platform-specific) feature-gated modules +// ** non-windows #[cfg(all(not(windows), feature = "mode"))] pub mod mode; +// ** unix-only +#[cfg(all(unix, feature = "entries"))] +pub mod entries; #[cfg(all(unix, feature = "process"))] pub mod process; #[cfg(all(unix, not(target_os = "fuchsia"), feature = "signals"))] @@ -51,9 +29,6 @@ pub mod signals; feature = "utmpx" ))] pub mod utmpx; - -#[cfg(feature = "zero-copy")] -pub mod zero_copy; - +// ** windows-only #[cfg(all(windows, feature = "wide"))] pub mod wide; diff --git a/src/uucore/src/encoding.rs b/src/uucore/src/lib/features/encoding.rs similarity index 98% rename from src/uucore/src/encoding.rs rename to src/uucore/src/lib/features/encoding.rs index b5b233e4d..8544eb61c 100644 --- a/src/uucore/src/encoding.rs +++ b/src/uucore/src/lib/features/encoding.rs @@ -7,7 +7,11 @@ // extern crate data_encoding; +extern crate failure; + use self::data_encoding::{DecodeError, BASE32, BASE64}; + +use failure::Fail; use std::io::{self, Read, Write}; #[derive(Fail, Debug)] diff --git a/src/uucore/src/entries.rs b/src/uucore/src/lib/features/entries.rs similarity index 100% rename from src/uucore/src/entries.rs rename to src/uucore/src/lib/features/entries.rs diff --git a/src/uucore/src/fs.rs b/src/uucore/src/lib/features/fs.rs similarity index 99% rename from src/uucore/src/fs.rs rename to src/uucore/src/lib/features/fs.rs index edd661264..d1c0a3f7c 100644 --- a/src/uucore/src/fs.rs +++ b/src/uucore/src/lib/features/fs.rs @@ -12,9 +12,7 @@ extern crate dunce; extern crate termion; #[cfg(unix)] -use super::libc; -#[cfg(unix)] -use super::libc::{ +use libc::{ mode_t, S_IRGRP, S_IROTH, S_IRUSR, S_ISGID, S_ISUID, S_ISVTX, S_IWGRP, S_IWOTH, S_IWUSR, S_IXGRP, S_IXOTH, S_IXUSR, }; diff --git a/src/uucore/src/mode.rs b/src/uucore/src/lib/features/mode.rs similarity index 97% rename from src/uucore/src/mode.rs rename to src/uucore/src/lib/features/mode.rs index 26e237a7b..0b43fdb02 100644 --- a/src/uucore/src/mode.rs +++ b/src/uucore/src/lib/features/mode.rs @@ -6,8 +6,6 @@ // file that was distributed with this source code. // -use std::error::Error; - pub fn parse_numeric(fperm: u32, mut mode: &str) -> Result { let (op, pos) = parse_op(mode, Some('='))?; mode = mode[pos..].trim_start_matches('0'); @@ -21,7 +19,7 @@ pub fn parse_numeric(fperm: u32, mut mode: &str) -> Result { '=' => change, _ => unreachable!(), }), - Err(err) => Err(err.description().to_owned()), + Err(err) => Err(err.to_string()), } } } diff --git a/src/uucore/src/parse_time.rs b/src/uucore/src/lib/features/parse_time.rs similarity index 100% rename from src/uucore/src/parse_time.rs rename to src/uucore/src/lib/features/parse_time.rs diff --git a/src/uucore/src/process.rs b/src/uucore/src/lib/features/process.rs similarity index 99% rename from src/uucore/src/process.rs rename to src/uucore/src/lib/features/process.rs index 11888ae66..d0ca39ff4 100644 --- a/src/uucore/src/process.rs +++ b/src/uucore/src/lib/features/process.rs @@ -7,7 +7,6 @@ // that was distributed with this source code. // -use super::libc; use libc::{c_int, gid_t, pid_t, uid_t}; use std::fmt; use std::io; diff --git a/src/uucore/src/signals.rs b/src/uucore/src/lib/features/signals.rs similarity index 100% rename from src/uucore/src/signals.rs rename to src/uucore/src/lib/features/signals.rs diff --git a/src/uucore/src/utmpx.rs b/src/uucore/src/lib/features/utmpx.rs similarity index 99% rename from src/uucore/src/utmpx.rs rename to src/uucore/src/lib/features/utmpx.rs index aa6a8ac12..f1b692e7c 100644 --- a/src/uucore/src/utmpx.rs +++ b/src/uucore/src/lib/features/utmpx.rs @@ -31,7 +31,6 @@ //! } //! ``` -use super::libc; pub extern crate time; use self::time::{Timespec, Tm}; @@ -111,8 +110,6 @@ mod ut { #[cfg(target_os = "freebsd")] mod ut { - use super::libc; - pub static DEFAULT_FILE: &str = ""; pub const UT_LINESIZE: usize = 16; diff --git a/src/uucore/src/wide.rs b/src/uucore/src/lib/features/wide.rs similarity index 100% rename from src/uucore/src/wide.rs rename to src/uucore/src/lib/features/wide.rs diff --git a/src/uucore/src/zero_copy/mod.rs b/src/uucore/src/lib/features/zero_copy.rs similarity index 100% rename from src/uucore/src/zero_copy/mod.rs rename to src/uucore/src/lib/features/zero_copy.rs diff --git a/src/uucore/src/zero_copy/platform/mod.rs b/src/uucore/src/lib/features/zero_copy/platform.rs similarity index 100% rename from src/uucore/src/zero_copy/platform/mod.rs rename to src/uucore/src/lib/features/zero_copy/platform.rs diff --git a/src/uucore/src/zero_copy/platform/default.rs b/src/uucore/src/lib/features/zero_copy/platform/default.rs similarity index 90% rename from src/uucore/src/zero_copy/platform/default.rs rename to src/uucore/src/lib/features/zero_copy/platform/default.rs index f6ddfd4bd..47239a361 100644 --- a/src/uucore/src/zero_copy/platform/default.rs +++ b/src/uucore/src/lib/features/zero_copy/platform/default.rs @@ -1,4 +1,4 @@ -use crate::zero_copy::RawObject; +use crate::features::zero_copy::RawObject; use std::io::{self, Write}; diff --git a/src/uucore/src/zero_copy/platform/linux.rs b/src/uucore/src/lib/features/zero_copy/platform/linux.rs similarity index 97% rename from src/uucore/src/zero_copy/platform/linux.rs rename to src/uucore/src/lib/features/zero_copy/platform/linux.rs index 36d658ca3..4f2412226 100644 --- a/src/uucore/src/zero_copy/platform/linux.rs +++ b/src/uucore/src/lib/features/zero_copy/platform/linux.rs @@ -9,9 +9,9 @@ use nix::sys::uio::IoVec; use nix::unistd::pipe; use platform_info::{PlatformInfo, Uname}; -use crate::zero_copy::{FromRawObject, RawObject}; +use crate::features::zero_copy::{FromRawObject, RawObject}; -lazy_static! { +lazy_static::lazy_static! { static ref IN_WSL: bool = { let info = PlatformInfo::new().unwrap(); info.release().contains("Microsoft") diff --git a/src/uucore/src/zero_copy/platform/unix.rs b/src/uucore/src/lib/features/zero_copy/platform/unix.rs similarity index 85% rename from src/uucore/src/zero_copy/platform/unix.rs rename to src/uucore/src/lib/features/zero_copy/platform/unix.rs index 1305830e4..553549c9b 100644 --- a/src/uucore/src/zero_copy/platform/unix.rs +++ b/src/uucore/src/lib/features/zero_copy/platform/unix.rs @@ -1,6 +1,6 @@ use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; -use crate::zero_copy::{AsRawObject, FromRawObject}; +use crate::features::zero_copy::{AsRawObject, FromRawObject}; pub type RawObject = RawFd; diff --git a/src/uucore/src/zero_copy/platform/windows.rs b/src/uucore/src/lib/features/zero_copy/platform/windows.rs similarity index 87% rename from src/uucore/src/zero_copy/platform/windows.rs rename to src/uucore/src/lib/features/zero_copy/platform/windows.rs index ef86e8635..8134bfda3 100644 --- a/src/uucore/src/zero_copy/platform/windows.rs +++ b/src/uucore/src/lib/features/zero_copy/platform/windows.rs @@ -1,6 +1,6 @@ use std::os::windows::io::{AsRawHandle, FromRawHandle, RawHandle}; -use crate::zero_copy::{AsRawObject, FromRawObject}; +use crate::features::zero_copy::{AsRawObject, FromRawObject}; pub type RawObject = RawHandle; diff --git a/src/uucore/src/lib/lib.rs b/src/uucore/src/lib/lib.rs new file mode 100644 index 000000000..d9e341976 --- /dev/null +++ b/src/uucore/src/lib/lib.rs @@ -0,0 +1,75 @@ +// library ~ (core/bundler file) + +// spell-checker:ignore (uucore/uutils) coreopts libc musl utmpx uucore uutils winapi + +//## external crates + +extern crate wild; + +// * feature-gated external crates +#[cfg(feature = "failure")] +extern crate failure; +#[cfg(feature = "failure_derive")] +extern crate failure_derive; +#[cfg(all(feature = "lazy_static", target_os = "linux"))] +extern crate lazy_static; +#[cfg(feature = "nix")] +extern crate nix; +#[cfg(feature = "platform-info")] +extern crate platform_info; + +// * feature-gated external crates (re-shared as public internal modules) +#[cfg(feature = "libc")] +pub extern crate libc; +#[cfg(feature = "winapi")] +pub extern crate winapi; + +//## internal modules + +mod macros; // crate macros (macro_rules-type; exported to `crate::...`) + +mod features; // feature-gated code modules +mod mods; // core cross-platform modules + +// * cross-platform modules +pub use mods::coreopts; +pub use mods::panic; + +// * feature-gated modules +#[cfg(feature = "encoding")] +pub use features::encoding; +#[cfg(feature = "fs")] +pub use features::fs; +#[cfg(feature = "parse_time")] +pub use features::parse_time; +#[cfg(feature = "zero-copy")] +pub use features::zero_copy; + +// * (platform-specific) feature-gated modules +// ** non-windows +#[cfg(all(not(windows), feature = "mode"))] +pub use features::mode; +// ** unix-only +#[cfg(all(unix, feature = "entries"))] +pub use features::entries; +#[cfg(all(unix, feature = "process"))] +pub use features::process; +#[cfg(all(unix, not(target_os = "fuchsia"), feature = "signals"))] +pub use features::signals; +#[cfg(all( + unix, + not(target_os = "fuchsia"), + not(target_env = "musl"), + feature = "utmpx" +))] +pub use features::utmpx; +// ** windows-only +#[cfg(all(windows, feature = "wide"))] +pub use features::wide; + +//## core functions + +// args() ... +pub fn args() -> impl Iterator { + wild::args() +} diff --git a/src/uucore/src/macros.rs b/src/uucore/src/lib/macros.rs similarity index 85% rename from src/uucore/src/macros.rs rename to src/uucore/src/lib/macros.rs index 5356d9d5d..f5bef7565 100644 --- a/src/uucore/src/macros.rs +++ b/src/uucore/src/lib/macros.rs @@ -7,6 +7,40 @@ * file that was distributed with this source code. */ +// #[macro_export] +// macro_rules! main { ($($arg:tt)+) => ({ +// extern crate uu_arch; +// use std::io::Write; +// use uu_arch::uumain; + +// fn main() { +// uucore::panic::install_sigpipe_hook(); + +// let code = uumain(uucore::args().collect()); +// // Since stdout is line-buffered by default, we need to ensure any pending +// // writes are flushed before exiting. Ideally, this should be enforced by +// // each utility. +// // +// // See: https://github.com/rust-lang/rust/issues/23818 +// // +// std::io::stdout().flush().expect("could not flush stdout"); +// std::process::exit(code); +// } +// })} + +// extern crate proc_macro; +// use proc_macro::TokenStream; +// #[proc_macro_attribute] +// pub fn hello(attr: TokenStream, item: TokenStream) -> TokenStream { +// let result = quote! { +// fn main() { +// uucore::panic::install_sigpipe_hook(); +// std::io::stdout().flush().expect("could not flush stdout"); +// std::process::exit(code); +// } +// }; +// } + #[macro_export] macro_rules! executable( () => ({ diff --git a/src/uucore/src/lib/mods.rs b/src/uucore/src/lib/mods.rs new file mode 100644 index 000000000..542db5cd1 --- /dev/null +++ b/src/uucore/src/lib/mods.rs @@ -0,0 +1,6 @@ +// mods ~ cross-platforms modules (core/bundler file) + +// spell-checker:ignore (uucore/uutils) coreopts libc musl utmpx uucore uutils winapi + +pub mod coreopts; +pub mod panic; diff --git a/src/uucore/src/coreopts.rs b/src/uucore/src/lib/mods/coreopts.rs similarity index 98% rename from src/uucore/src/coreopts.rs rename to src/uucore/src/lib/mods/coreopts.rs index 8c62ec9f2..a7ae0d399 100644 --- a/src/uucore/src/coreopts.rs +++ b/src/uucore/src/lib/mods/coreopts.rs @@ -109,10 +109,10 @@ impl<'a> CoreOptions<'a> { usage_str, self.help_text.long_help ); - exit!(0); + crate::exit!(0); } else if matches.opt_present("version") { println!("{} {}", self.help_text.name, self.help_text.version); - exit!(0); + crate::exit!(0); } matches } diff --git a/src/uucore/src/panic.rs b/src/uucore/src/lib/mods/panic.rs similarity index 100% rename from src/uucore/src/panic.rs rename to src/uucore/src/lib/mods/panic.rs