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;
85#[cfg(feature = "regex")]
86pub use str::Regex;
87pub use str::{ByteFormat, ZString};
88
89mod env_var;
90pub use env_var::*;
91mod atomic; // Atomic helpers
92pub use atomic::*;
93mod misc; // other stuff that doesn't have a place
94pub use misc::*;
95
96// --- Error Handling (no feature needed) ---
97mod errhand;
98pub use errhand::*;
99pub use pistonite_cu_proc_macros::context;
100
101// --- Logging (no feature needed) ---
102pub mod lv;
103pub use lv::{debug, error, info, trace, warn};
104
105// --- Command Line Interface (print/cli/prompt feature) ---
106#[cfg(feature = "print")]
107pub mod cli;
108#[cfg(feature = "print")]
109pub use cli::{CtrlcSignal, ProgressBar, progress};
110#[cfg(feature = "prompt")]
111pub use cli::{password_chars_legal, prompt, yesno};
112#[cfg(feature = "cli")]
113pub use pistonite_cu_proc_macros::cli;
114
115// --- Async (coroutine/coroutine-heavy) ---
116/// Alias for a boxed future
117pub type BoxedFuture<T> = std::pin::Pin<Box<dyn Future<Output = T> + Send + 'static>>;
118#[cfg(feature = "coroutine")]
119pub mod co;
120#[cfg(feature = "coroutine")]
121pub use co::{join, select, try_join};
122
123// --- File System ---
124#[cfg(feature = "fs")]
125pub mod fs;
126#[cfg(feature = "fs")]
127pub use fs::bin;
128#[cfg(feature = "fs")]
129pub use fs::bin::which;
130
131// === above is refactored and documented ===
132
133// --- Child Process ---
134#[cfg(feature = "process")]
135mod process;
136#[cfg(feature = "process")]
137pub use process::{Child, Command, CommandBuilder, Spawn, color_flag, color_flag_eq, pio};
138#[cfg(all(feature = "process", feature = "print"))]
139pub use process::{width_flag, width_flag_eq};
140
141/// Parsing utilities
142#[cfg(feature = "parse")]
143mod parse;
144#[cfg(feature = "parse")]
145pub use parse::*;
146#[cfg(feature = "parse")]
147pub use pistonite_cu_proc_macros::Parse;
148
149#[doc(hidden)]
150pub mod __priv {
151    #[cfg(feature = "process")]
152    pub use crate::process::__ConfigFn;
153}
154
155/// Lib re-exports
156#[doc(hidden)]
157pub mod lib {
158    #[cfg(feature = "cli")]
159    pub use clap;
160    #[cfg(feature = "derive")]
161    pub use derive_more;
162}
163
164/// Prelude imports
165#[doc(hidden)]
166pub mod pre {
167    pub use crate::Context as _;
168    pub use crate::str::{OsStrExtension as _, OsStrExtensionOwned as _};
169
170    #[cfg(feature = "cli")]
171    pub use crate::lib::clap;
172
173    #[cfg(feature = "coroutine")]
174    pub use tokio::io::{AsyncBufReadExt as _, AsyncReadExt as _, AsyncWriteExt as _};
175
176    #[cfg(feature = "fs")]
177    pub use crate::str::PathExtension as _;
178
179    #[cfg(feature = "parse")]
180    pub use crate::ParseTo as _;
181    #[cfg(feature = "process")]
182    pub use crate::Spawn as _;
183    #[cfg(feature = "json")]
184    pub use crate::json;
185    #[cfg(feature = "toml")]
186    pub use crate::toml;
187    #[cfg(feature = "yaml")]
188    pub use crate::yaml;
189    #[cfg(feature = "serde")]
190    pub use ::serde::{Deserialize, Serialize};
191
192    #[cfg(feature = "derive")]
193    pub use crate::lib::derive_more;
194    #[cfg(feature = "derive")]
195    pub use crate::lib::derive_more::{
196        AsMut, AsRef, Binary as DisplayBinary, Constructor, Debug as DebugCustom, Deref, DerefMut,
197        Display, From, Index, IndexMut, Into, IntoIterator, IsVariant, LowerExp as DisplayLowerExp,
198        LowerHex as DisplayLowerHex, Octal as DisplayOctal, Pointer as DisplayPointer,
199        UpperExp as DisplayUpperExp, UpperHex as DisplayUpperHex,
200    };
201}