Skip to main content

pistonite_cu/
lib.rs

1//! # Cu = Copper
2//! Batteries-included common utils
3//!
4//! (If you are viewing this on docs.rs, please use the [self-hosted
5//! version](https://cu.pistonite.dev) instead)
6//!
7//! # Quick start
8//! When installing, rename the crate to `cu` in `Cargo.toml`:
9//! ```toml
10//! # Cargo.toml
11//! # ...
12//! [dependencies.cu]
13//! package = "pistonite-cu"
14//! version = "..." # check by running `cargo info pistonite-cu`
15//! features = [ "full" ] # see docs
16//!
17//! [dependencies]
18//! # ...
19//! ```
20//!
21//! The goal with using `cu` is use only one `use` statement:
22//! ```rust
23//! # use pistonite_cu as cu;
24//! use cu::pre::*;
25//! ```
26//! This brings into scope a few things:
27//! - Traits that are expected to be used, such as `anyhow::Context`
28//! - Re-exports of modules, such as `json` if the `json` feature is enabled
29//!
30//! If a function or type is not included with `pre::*`, that means the canonical
31//! style for using it is with the full path, for example, you would write:
32//!
33#![cfg_attr(not(feature = "fs"), doc = "```rust,ignore")]
34#![cfg_attr(feature = "fs", doc = "```rust,no_run")]
35//! # use pistonite_cu as cu;
36//! use cu::pre::*;
37//!
38//! fn read_file() -> cu::Result<()> {
39//!     cu::fs::read_string("foo/bar.txt")?;
40//!     cu::info!("successfully read file");
41//!     Ok(())
42//! }
43//! ```
44//!
45//! instead of the below:
46#![cfg_attr(not(feature = "fs"), doc = "```rust,ignore")]
47#![cfg_attr(feature = "fs", doc = "```rust,no_run")]
48//! # use pistonite_cu as cu;
49//! use cu::pre::*;
50//! use cu::{Result, fs, info};
51//! // ^ don't include extra uses!
52//! //   the biggest disadvantage of this is
53//! //   it's easy to confuse with types in the standard library
54//!
55//! fn read_file() -> Result<()> {
56//!     fs::read_string("foo/bar.txt")?;
57//!     info!("successfully read file");
58//!     Ok(())
59//! }
60//! ```
61//!
62//! # Quick Reference
63//! - [Error Handling](macro@crate::check) (via [`anyhow`](https://docs.rs/anyhow))
64//! - [Logging](mod@crate::lv) (via [`log`](https://docs.rs/log))
65//! - [Printing and Command Line Interface](mod@crate::cli) (CLI arg parsing via
66//!   [`clap`](https://docs.rs/clap))
67//! - [Handling Ctrl-C](fn@crate::cli::ctrlc_frame)
68//! - [Progress Bars](fn@crate::progress)
69//! - [Prompting](fn@crate::prompt)
70//! - [Coroutines (Async)](mod@crate::co) (via [`tokio`](https://docs.rs/tokio))
71//! - [File System Paths and Strings](trait@crate::str::PathExtension)
72//! - [File System Operations](mod@crate::fs)
73//! - [Binary Path Registry](mod@crate::fs::bin)
74//! - [Spawning Child Processes](crate::Command)
75//! - [Parsing](trait@Parse) (via [`serde`](https://docs.rs/serde))
76//! - Derive Macros: the `derive` feature, via [`derive_more`](https://docs.rs/derive_more)
77
78#![cfg_attr(any(docsrs, feature = "nightly"), feature(doc_cfg))]
79
80// for macros
81extern crate self as cu;
82
83// --- Basic stuff (no feature needed) ---
84pub mod str;
85pub use str::{ByteFormat, ZString};
86
87mod env_var;
88pub use env_var::*;
89mod atomic; // Atomic helpers
90pub use atomic::*;
91mod misc; // other stuff that doesn't have a place
92pub use misc::*;
93
94// --- Error Handling (no feature needed) ---
95mod errhand;
96pub use errhand::*;
97pub use pistonite_cu_proc_macros::context;
98
99// --- Logging (no feature needed) ---
100pub mod lv;
101pub use lv::{debug, error, info, trace, warn};
102
103// --- Command Line Interface (print/cli/prompt feature) ---
104#[cfg(feature = "print")]
105pub mod cli;
106#[cfg(feature = "print")]
107pub use cli::{CtrlcSignal, ProgressBar, progress};
108#[cfg(feature = "prompt")]
109pub use cli::{password_chars_legal, prompt, yesno};
110#[cfg(feature = "cli")]
111pub use pistonite_cu_proc_macros::cli;
112
113// --- Async (coroutine/coroutine-heavy) ---
114/// Alias for a boxed future
115pub type BoxedFuture<T> = std::pin::Pin<Box<dyn Future<Output = T> + Send + 'static>>;
116#[cfg(feature = "coroutine")]
117pub mod co;
118#[cfg(feature = "coroutine")]
119pub use co::{join, select, try_join};
120
121// --- File System ---
122#[cfg(feature = "fs")]
123pub mod fs;
124#[cfg(feature = "fs")]
125pub use fs::bin;
126#[cfg(feature = "fs")]
127pub use fs::bin::which;
128
129// === above is refactored and documented ===
130
131// --- Child Process ---
132#[cfg(feature = "process")]
133mod process;
134#[cfg(feature = "process")]
135pub use process::{Child, Command, CommandBuilder, Spawn, color_flag, color_flag_eq, pio};
136#[cfg(all(feature = "process", feature = "print"))]
137pub use process::{width_flag, width_flag_eq};
138
139/// Parsing utilities
140#[cfg(feature = "parse")]
141mod parse;
142#[cfg(feature = "parse")]
143pub use parse::*;
144#[cfg(feature = "parse")]
145pub use pistonite_cu_proc_macros::Parse;
146
147#[doc(hidden)]
148pub mod __priv {
149    #[cfg(feature = "process")]
150    pub use crate::process::__ConfigFn;
151}
152
153/// Lib re-exports
154#[doc(hidden)]
155pub mod lib {
156    #[cfg(feature = "cli")]
157    pub use clap;
158    #[cfg(feature = "derive")]
159    pub use derive_more;
160}
161
162/// Prelude imports
163#[doc(hidden)]
164pub mod pre {
165    pub use crate::Context as _;
166    pub use crate::str::{OsStrExtension as _, OsStrExtensionOwned as _};
167
168    #[cfg(feature = "cli")]
169    pub use crate::lib::clap;
170
171    #[cfg(feature = "coroutine")]
172    pub use tokio::io::{AsyncBufReadExt as _, AsyncReadExt as _, AsyncWriteExt as _};
173
174    #[cfg(feature = "fs")]
175    pub use crate::str::PathExtension as _;
176
177    #[cfg(feature = "parse")]
178    pub use crate::ParseTo as _;
179    #[cfg(feature = "process")]
180    pub use crate::Spawn as _;
181    #[cfg(feature = "json")]
182    pub use crate::json;
183    #[cfg(feature = "toml")]
184    pub use crate::toml;
185    #[cfg(feature = "yaml")]
186    pub use crate::yaml;
187    #[cfg(feature = "serde")]
188    pub use ::serde::{Deserialize, Serialize};
189
190    #[cfg(feature = "derive")]
191    pub use crate::lib::derive_more;
192    #[cfg(feature = "derive")]
193    pub use crate::lib::derive_more::{
194        AsMut, AsRef, Binary as DisplayBinary, Constructor, Debug as DebugCustom, Deref, DerefMut,
195        Display, From, Index, IndexMut, Into, IntoIterator, IsVariant, LowerExp as DisplayLowerExp,
196        LowerHex as DisplayLowerHex, Octal as DisplayOctal, Pointer as DisplayPointer,
197        UpperExp as DisplayUpperExp, UpperHex as DisplayUpperHex,
198    };
199}