Expand description
§Cu = Copper
Batteries-included common utils
(If you are viewing this on docs.rs, please use the self-hosted version instead)
§Quick start
When installing, rename the crate to cu in Cargo.toml:
# Cargo.toml
# ...
[dependencies.cu]
package = "pistonite-cu"
version = "..." # check by running `cargo info pistonite-cu`
features = [ "full" ] # see docs
[dependencies]
# ...The goal with using cu is use only one use statement:
use cu::pre::*;This brings into scope a few things:
- Traits that are expected to be used, such as
anyhow::Context - Re-exports of modules, such as
jsonif thejsonfeature is enabled
If a function or type is not included with pre::*, that means the canonical
style for using it is with the full path, for example, you would write:
use cu::pre::*;
fn read_file() -> cu::Result<()> {
cu::fs::read_string("foo/bar.txt")?;
cu::info!("successfully read file");
Ok(())
}instead of the below:
use cu::pre::*;
use cu::{Result, fs, info};
// ^ don't include extra uses!
// the biggest disadvantage of this is
// it's easy to confuse with types in the standard library
fn read_file() -> Result<()> {
fs::read_string("foo/bar.txt")?;
info!("successfully read file");
Ok(())
}§Quick Reference
- Error Handling (via
anyhow) - Logging (via
log) - Printing and Command Line Interface (CLI arg parsing via
clap) - Handling Ctrl-C
- Progress Bars
- Prompting
- Coroutines (Async) (via
tokio) - File System Paths and Strings
- File System Operations
- Binary Path Registry
- Spawning Child Processes
- Parsing (via
serde) - Derive Macros: the
derivefeature, viaderive_more
Re-exports§
pub use str::Regex;regexpub use str::ByteFormat;pub use str::ZString;pub use cli::CtrlcSignal;printpub use cli::ProgressBar;printpub use cli::progress;printpub use cli::password_chars_legal;promptpub use cli::prompt;promptpub use cli::yesno;promptpub use fs::bin;fspub use fs::bin::which;fs
Modules§
- cli
print - Printting and Command Line Interface
- co
coroutine - Coroutines (Async)
- fs
fs - File System Operations
- json
jsonandparse serdeJSON integration via theserde_jsoncrate- lv
- Logging
- pio
process - Utility to configure IO with child process.
- str
- Working with bytes
- toml
parseandtoml serdeTOML integration via thetomlcrate- yaml
parseandyaml serdeYAML integration via theserde_yaml_ngcrate
Macros§
- args
process - Create a config to add multiple args of different types when building a subprocess.
- bail
- Return early with an error.
- check
- Error Handling
- custom_
print print - Print message with a custom log format and level.
- debug
- Logs a message at the debug level.
- ensure
- Check if an expression is
true, as an assertion - envs
process - Create a config to add multiple environments of different types when building a subprocess.
- error
- Logs a message at the error level.
- fmtand
- Format and invoke a print macro
- fmterr
- Construct an ad-hoc error from a string or existing non-
anyhowerror value. - hint
print - Logs a hint message
- info
- Logs a message at the info level.
- join
coroutine - Waits on multiple concurrent branches, returning when all branches complete.
- json
json - Construct a
serde_json::Valuefrom a JSON literal. - panicand
- Invoke a print macro, then panic with the same message
- path
- Efficient Path-join macro
- print
print - Print something
- progress
print - Update a progress bar
- prompt
coroutineandprintandprompt - Show prompt to the user. See Prompting
- rethrow
- Rethrow an
Err, optionally with additional context - select
coroutine - Waits on multiple concurrent branches, returning when the first branch completes, cancelling the remaining branches.
- some
- Check if an expression is
Some - toml
toml - Construct a
Tablefrom TOML syntax. - trace
- Logs a message at the trace level.
- try_
join coroutine - Waits on multiple concurrent branches, returning when all branches
complete with
Ok(_)or on the firstErr(_). - unimplemented
- Like
unimplemented!in std library, but log a message and return an error instead of panicking - unreachable
- Like
unreachable!in std library, but log a message and return an error instead of panicking reached. This might be less performant in release builds - warn
- Logs a message at the warn level.
- yesno
coroutineandprintandprompt - Show a Yes/No prompt. See Prompting
Structs§
- Atomic
- An atomic wrapper with an underlying atomic storage and conversion to a type T.
- Child
process - A child process spawned with
Command - Command
process - Builder for spawning a child process.
- Error
- The
Errortype, a wrapper around a dynamic error type.
Traits§
- Atomic
Type - Marker type to associate primitive with their atomic versions
- Context
- Provides the
contextmethod forResult. - Parse
parse - The parsing utilities in
cuprovides wrapper to the std library for parsing common data, such as different radix for numbers, and support for common formats likejson,yamlandtoml. - ParseTo
parse - the
.parse_tofunction. - Spawn
process - This trait allows implementing different return types for
Command::spawnbased on the configured IO.
Functions§
- Ok
- Equivalent to
Ok::<_, anyhow::Error>(value). - best_
effort_ panic_ info - Try to get info from a panic payload
- co_copy
coroutine - Copy an async reader to an async writer.
- co_read
coroutineandparse - Parse a value from a
tokio::io::AsyncRead. - color_
flag process - Create a
--color always|neverflag that can be added to a command, based on if color is enabled for the current process using this crate’s cli flags - color_
flag_ eq process - Create a
--color=always|neverflag that can be added to a command, based on if color is enabled for the current process using this crate’s cli flags - copy
- Copy a reader to a writer.
- env_var
- Like
std::env::var, but treat not-set as empty string. Tracing and reporting is built-in. - parse
parse - Parse a value from a
&str. - parse_
owned parse - Parse a value from a
String. - read
parse - Parse a value from a
std::io::Read. - width_
flag printandprocess - Create a
--width WIDTHflag that can be added to a command, based on the current width of the terminal - width_
flag_ eq printandprocess - Create a
--width=WIDTHflag that can be added to a command, based on the current width of the terminal
Type Aliases§
- Boxed
Future - Alias for a boxed future
- Command
Builder process - A
Commandto be built - Result
Result<T, Error>
Attribute Macros§
- cli
cli - For the Command Line Interface feature set,
please refer to the
cu::climodule. - context
- Attribute macro for wrapping a function with an error context