optempty/lib.rs
1//! Helpers for dealing with `Option`s/`Result`s of collections or other things
2//! (like `String`s) that may be empty.
3//!
4//! # Examples
5//!
6//! Always start by including the traits:
7//!
8//! ```
9//! use optempty::*;
10//! ```
11//!
12//! These examples only show `Vec<T>`, but they support any type that implements
13//! [`IsEmpty`].
14//!
15//! ## `empty_into_none`
16//!
17//! `Some` with an empty `Vec` becomes `None`.
18//! ```
19//! use optempty::*;
20//!
21//! let some: Option<Vec<&str>> = Some(vec![]);
22//! let none = some.empty_into_none();
23//! assert_eq!(None, none);
24//! ```
25//!
26//! `Some` with a non-empty `Vec` remains unchanged.
27//! ```
28//! # use optempty::*;
29//! #
30//! let some = Some(vec!["a", "b", "c"]);
31//! let still_some = some.clone().empty_into_none();
32//! assert_eq!(some, still_some);
33//! ```
34//!
35//! `None` remains unchanged.
36//! ```
37//! # use optempty::*;
38//! #
39//! let none: Option<Vec<&str>> = None;
40//! let still_none = none.empty_into_none();
41//! assert_eq!(None, still_none);
42//! ```
43//!
44//! ## `empty_into_err`
45//!
46//! `Ok` with an empty `Vec` becomes `Err`.
47//! ```
48//! use optempty::*;
49//!
50//! let ok: Result<Vec<&str>, &str> = Ok(vec![]);
51//! let err = ok.empty_into_err(|| "was empty");
52//! assert_eq!(Err("was empty"), err);
53//! ```
54//!
55//! `Ok` with a non-empty `Vec` remains unchanged.
56//! ```
57//! # use optempty::*;
58//! #
59//! let ok = Ok(vec!["a", "b", "c"]);
60//! let still_ok = ok.empty_into_err(|| "was empty");
61//! assert_eq!(Ok(vec!["a", "b", "c"]), still_ok);
62//! ```
63//!
64//! `Err` remains unchanged.
65//! ```
66//! # use optempty::*;
67//! #
68//! let err: Result<Vec<&str>, &str> = Err("failed");
69//! let still_err = err.empty_into_err(|| "was empty");
70//! assert_eq!(Err("failed"), still_err);
71//! ```
72//!
73//! See more examples at:
74//! * [`IsEmpty`]
75//! * [`EmptyIntoNone`]
76//! * [`EmptyIntoErr`]
77//!
78//! # Features
79//!
80//! Available features are:
81//! * `querymap`
82//! * Adds support for [`query_map::QueryMap`]
83//! * `serdejson`
84//! * Adds support for [`serde_json::Map`]
85//! * `std`
86//! * Adds support for types in `std::collections` in addition to types from `alloc`.
87//!
88//! Default features:
89//! * `std`
90//!
91//! [Option]: std::option::Option
92//! [IsEmpty]: crate::is_empty::IsEmpty
93//! [EmptyIntoNone]: crate::empty_into_none::EmptyIntoNone
94//! [EmptyIntoErr]: crate::empty_into_err::EmptyIntoErr
95
96#![deny(warnings)]
97#![cfg_attr(not(feature = "use_std"), no_std)]
98
99pub mod empty_into_err;
100pub mod empty_into_none;
101pub mod is_empty;
102
103pub use empty_into_err::EmptyIntoErr;
104pub use empty_into_none::EmptyIntoNone;
105pub use is_empty::IsEmpty;