Refinement types.
You can add refining as a dependency with the following command:
$ cargo add refiningOr by directly specifying it in the configuration like so:
[dependencies]
refining = "0.1.0"Alternatively, you can add it directly from the source:
[dependencies.refining]
git = "https://github.com/nekitdev/refining.git"use core::fmt;
use anyhow::Result;
use refining::prelude::*;
type_str!(DeviceName = "device name");
type_str!(DeviceCharge = "device charge");
type Name = Refinement<str, And<Ascii, LengthClosed<1, 32>>, DeviceName>;
type Charge = Refinement<u8, u8::Closed<1, 100>, DeviceCharge>;
struct Device<'a> {
name: &'a Name,
charge: Charge,
}
impl fmt::Display for Device<'_> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"{name} ({charge}%)",
name = self.name,
charge = self.charge
)
}
}
fn main() -> Result<()> {
let name = "nekit".refine_ref()?;
let charge = 42.refine()?;
let device = Device { name, charge };
println!("{device}");
Ok(())
}Running the example will print the following output:
nekit (42%)
The refining crate comes with the following features:
std(enabled by default): depends on the Rust Standard Library (std);empty(enabled by default): providesrefining::emptypredicates;length(enabled by default): providesrefining::lengthpredicates;int(enabled by default): providesrefining::intpredicates;char(enabled by default): providesrefining::charpredicates;str(enabled by default): providesrefining::strpredicates;regex(enabled by default, impliesstd): providesrefining::regexpredicates.
These features can be disabled via the following:
[dependencies.refining]
version = "0.1.0"
default-features = falseThen only the refining::core module will be available; additional features
can be enabled individually.
Again, note that regex implies std, so it can not be used in no_std environments.
You can find the documentation here.
If you need support with the library, you can send an email.
You can find the changelog here.
You can find the Security Policy of refining here.
If you are interested in contributing to refining, make sure to take a look at the
Contributing Guide, as well as the Code of Conduct.
refining is licensed under the MIT License terms. See License for details.