Skip to content

nekitdev/refinement-types

Repository files navigation

refinement-types

License Version Downloads Test

Refinement types.

Installation

cargo

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"

Examples

Library

// 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 }
    }
}

Binary

// 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(())
}

Documentation

You can find the documentation here.

Support

If you need support with the library, you can send an email.

Changelog

You can find the changelog here.

Security Policy

You can find the Security Policy of refinement-types here.

Contributing

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.

License

refinement-types is licensed under the MIT License terms. See License for details.

About

Refinement types.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages