valid_toml 0.0.2

Provides the ability to load a TOML file with validation.
Documentation
/* Copyright 2016 Joshua Gentry
 *
 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
 * http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
 * <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
 * option. This file may not be copied, modified, or distributed
 * except according to those terms.
 */
use std::fmt::{Debug, Error, Formatter};

//*************************************************************************************************
/// This enumeration is used to hold a value read from the TOML file and pass it around between
/// structures.
#[derive(Eq, PartialEq)]
pub enum ItemValue
{
    //---------------------------------------------------------------------------------------------
    /// The value is a string.
    String(String),

    //---------------------------------------------------------------------------------------------
    /// The value is a duration.
    Duration(i64),

    //---------------------------------------------------------------------------------------------
    /// The value is an i64.
    I64(i64),

    //---------------------------------------------------------------------------------------------
    /// The value is an i16.
    U16(u16),

    //---------------------------------------------------------------------------------------------
    /// The value is an usize.
    Usize(usize)
}

impl ItemValue
{
    //*********************************************************************************************
    /// Tests if the item is an u16 value.
    pub fn is_u16(&self) -> bool
    {
        match *self
        {
            ItemValue::U16(_) => true,
            _                 => false
        }
    }

    //*********************************************************************************************
    /// Tests if the item is a duration value.
    pub fn is_duration(&self) -> bool
    {
        match *self
        {
            ItemValue::Duration(_) => true,
            _                      => false
        }
    }

    //*********************************************************************************************
    /// Tests if the item is an i64 value.
    pub fn is_i64(&self) -> bool
    {
        match *self
        {
            ItemValue::I64(_) => true,
            _                 => false
        }
    }

    //*********************************************************************************************
    /// Tests if the item is an usize value.
    pub fn is_usize(&self) -> bool
    {
        match *self
        {
            ItemValue::Usize(_) => true,
            _                   => false
        }
    }

    //*********************************************************************************************
    /// Tests if the item is an string value.
    pub fn is_str(&self) -> bool
    {
        match *self
        {
            ItemValue::String(_) => true,
            _                    => false
        }
    }

    //*********************************************************************************************
    /// Extracts the i64 value.
    ///
    /// # Panics
    ///
    /// This method will panic if the element is not an ItemValue::Duration or ItemValue::I64.
    pub fn get_i64(&self) -> i64
    {
        match *self
        {
            ItemValue::Duration(value) => value,
            ItemValue::I64(value)      => value,
            _                          => panic!("Item is a {:?}", self)
        }
    }

    //*********************************************************************************************
    /// Extracts the string value.
    ///
    /// # Panics
    ///
    /// This method will panic if the element is not an ItemValue::String.
    pub fn get_str(&self) -> &str
    {
        match *self
        {
            ItemValue::String(ref value) => value.as_str(),
            _                            => panic!("Item is a {:?}", self)
        }
    }

    //*********************************************************************************************
    /// Extracts the u16 value.
    ///
    /// # Panics
    ///
    /// This method will panic if the element is not an ItemValue::U16.
    pub fn get_u16(&self) -> u16
    {
        match *self
        {
            ItemValue::U16(value) => value,
            _                     => panic!("Item is a {:?}", self)
        }
    }

    //*********************************************************************************************
    /// Extracts the usize value.
    ///
    /// # Panics
    ///
    /// This method will panic if the element is not an ItemValue::Usize.
    pub fn get_usize(&self) -> usize
    {
        match *self
        {
            ItemValue::Usize(value) => value,
            _                       => panic!("Item is a {:?}", self)
        }
    }
}

impl Debug for ItemValue
{
    //*********************************************************************************************
    /// Display the value of the element durring debug.
    fn fmt(
        &self,
        fmt : &mut Formatter
        ) -> Result<(), Error>
    {
        match *self
        {
            ItemValue::String(ref value) => write!(fmt, "String({})", value),
            ItemValue::Duration(value)   => write!(fmt, "Duration({})", value),
            ItemValue::I64(value)        => write!(fmt, "I64({})", value),
            ItemValue::U16(value)        => write!(fmt, "U16({})", value),
            ItemValue::Usize(value)      => write!(fmt, "Usize({})", value)
        }
    }
}