Refinement types.
You can add refinement-types
as a dependency with the following command:
$ cargo add refinement-types
Or by directly specifying it in the configuration like so:
[dependencies]
refinement-types = "0.3.0"
Alternatively, you can add it directly from the source:
[dependencies.refinement-types]
git = "https://github.com/nekitdev/refinement-types.git"
// lib.rs
#![no_std]
use core::fmt;
use refinement_types::{Refinement, int::u8, length, logic::And, str};
/// Represents device names.
pub type Name<'n> = Refinement<&'n str, And<str::Ascii, length::Closed<1, 32>>>;
/// Represents device charge, in percentage.
pub type Charge = Refinement<u8, u8::LessOrEqual<100>>;
/// Represents devices.
#[derive(Debug)]
pub struct Device<'d> {
/// The name of the device.
name: Name<'d>,
/// The charge of the device.
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
)
}
}
impl<'d> Device<'d> {
/// Constructs [`Self`].
pub fn new(name: Name<'d>, charge: Charge) -> Self {
Self { name, charge }
}
}
// main.rs
use device::{Charge, Device, Name};
use miette::Result;
fn main() -> Result<()> {
let charge = Charge::refine(13)?;
let name = Name::refine("nekit")?;
let device = Device::new(name, charge);
println!("{device}"); // nekit: 13%
Ok(())
}
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 refinement-types
here.
If you are interested in contributing to refinement-types
, make sure to take a look at the
Contributing Guide, as well as the Code of Conduct.
refinement-types
is licensed under the MIT License terms. See License for details.