strval 0.5.0

Parse strings into values
Documentation
//! _strval_ is collection of types that keep values that can all be set from a
//! string value.  It can be thought of as a generalized version of the
//! `FromStr` trait.
//!
//! ```
//! use strval::{
//!   StrVal, Bool, BinU64Count, BinU64Size, DecU64Count, DecU64Size
//! };
//!
//! let v: Bool = "true".parse().unwrap();
//! assert!(v.get());
//! assert_eq!(v.val_str(), Some(String::from("true")));
//!
//! // "count" variant is assumed to not count bytes
//! let v: BinU64Count = "64k".parse().unwrap();
//! assert_eq!(v.get(), 65536);
//! assert_eq!(v.val_str(), Some(String::from("65536")));
//!
//! // "size" variant is assumed to be a byte size
//! let v: BinU64Size = "64kb".parse().unwrap();
//! assert_eq!(v.get(), 65536);
//! assert_eq!(v.val_str(), Some(String::from("65536")));
//!
//! // "count" variant is assumed to not count bytes
//! let v: DecU64Count = "64k".parse().unwrap();
//! assert_eq!(v.get(), 64000);
//! assert_eq!(v.val_str(), Some(String::from("64000")));
//!
//! // "size" variant is assumed to be a byte size
//! let v: DecU64Size = "64kb".parse().unwrap();
//! assert_eq!(v.get(), 64000);
//! assert_eq!(v.val_str(), Some(String::from("64000")));
//! ```
//!
//! # Features
//! | Feature    | Function
//! |------------|----------
//! | `rusqlite` | Implement `ToSql`/`FromSql` support for value types.

#![cfg_attr(docsrs, feature(doc_cfg))]

mod binu64count;
mod binu64size;
mod binusizecount;
mod binusizesize;
mod boolean;
mod decu64count;
mod decu64size;
mod decusizecount;
mod decusizesize;
mod dur;
mod err;
mod percent;
mod relabslim;
mod select;
mod str;

use std::str::FromStr;

pub use {
  binu64count::BinU64Count, binu64size::BinU64Size,
  binusizecount::BinUsizeCount, binusizesize::BinUsizeSize, boolean::Bool,
  decu64count::DecU64Count, decu64size::DecU64Size,
  decusizecount::DecUsizeCount, decusizesize::DecUsizeSize, dur::Dur,
  err::Error, percent::Percent, relabslim::RelAbsLim, select::Select,
  str::Str
};

/// Traits for values that can be set from strings.
pub trait StrVal: AsRef<str> + FromStr {
  type Type;

  /// # Errors
  fn set(&mut self, sval: &str) -> Result<Self::Type, Error>;

  fn get(&self) -> Self::Type;

  /// Return a string representation of the value.
  fn val_str(&self) -> Option<String>;

  /// Return a string representation of the configuration parameter's value,
  /// with an optional "evaluated" string.
  ///
  /// If the "evaluated" string is identical to the regular string
  /// representation, the second field will be `None`.
  fn get_str_vals(&self) -> (String, Option<String>);
}

/// Define a default (string) value and provide a value validator.
pub trait Controller {
  type Type;

  fn def() -> String;

  /// # Errors
  fn validate(val: &Self::Type) -> Result<(), Error>;
}


/// Accept any `u64`.
#[derive(Clone, Debug)]
pub struct AnyU64;

impl Controller for AnyU64 {
  type Type = u64;

  fn def() -> String {
    String::from("0")
  }

  fn validate(_val: &Self::Type) -> Result<(), Error> {
    Ok(())
  }
}

/// Accept any `usize`.
#[derive(Clone, Debug)]
pub struct AnyUsize;

impl Controller for AnyUsize {
  type Type = usize;

  fn def() -> String {
    String::from("0")
  }

  fn validate(_val: &Self::Type) -> Result<(), Error> {
    Ok(())
  }
}

// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :