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 enums::{FormatDesc, ValidationError};
use item_value::ItemValue;
use toml_builder::TomlBuilder;

//*************************************************************************************************
/// Identifies the result of an extract() operation.
pub enum ExtractResult
{
    //---------------------------------------------------------------------------------------------
    /// There was an error extracting the value.
    Error(ValidationError),

    //---------------------------------------------------------------------------------------------
    /// A Error item was extracted.
    Item(ItemValue),

    //---------------------------------------------------------------------------------------------
    /// A group was extracted.
    Group(TomlBuilder)
}


impl ExtractResult
{
    //*********************************************************************************************
    /// Creates a cannot parse validation error.
    pub fn cannot_parse(
        format : Option<FormatDesc>
        ) -> ExtractResult
    {
        ExtractResult::Error(
            ValidationError::CannotParse(format)
            )
    }

    //*********************************************************************************************
    /// A duration value is too small.
    ///
    /// # Panics
    ///
    /// This method will panic if min is None.
    pub fn duration_overflow(
        min : Option<String>,
        max : Option<String>
        ) -> ExtractResult
    {
        assert!(max.is_some());

        ExtractResult::Error(
            ValidationError::DurationOverflow(min, max)
            )
    }

    //*********************************************************************************************
    /// A duration value is too small.
    ///
    /// # Panics
    ///
    /// This method will panic if max is None.
    pub fn duration_underrun(
        min : Option<String>,
        max : Option<String>
        ) -> ExtractResult
    {
        assert!(min.is_some());

        ExtractResult::Error(
            ValidationError::DurationUnderrun(min, max)
            )
    }

    //*********************************************************************************************
    /// Creates an incorrect type validation error when the TOML data type is not what was
    /// expected.
    pub fn incorrect_type(
        expected : &'static str
        ) -> ExtractResult
    {
        ExtractResult::Error(
            ValidationError::IncorrectType(expected)
            )
    }

    //*********************************************************************************************
    /// Creates an error that a string is too long.
    ///
    /// # Panics
    ///
    /// This method will panic if max is None.
    pub fn long(
        min : Option<usize>,
        max : Option<usize>
        ) -> ExtractResult
    {
        assert!(max.is_some());

        ExtractResult::Error(
            ValidationError::Long(min, max)
            )
    }

    //*********************************************************************************************
    /// A numeric value is too large.
    ///
    /// # Panics
    ///
    /// This method will panic if max is None.
    pub fn overrun(
        min : Option<i64>,
        max : Option<i64>
        ) -> ExtractResult
    {
        assert!(max.is_some());

        ExtractResult::Error(
            ValidationError::Overflow(min, max)
            )
    }

    //*********************************************************************************************
    /// Creates an error that a string is too short.
    ///
    /// # Panics
    ///
    /// This method will panic if min is None.
    pub fn short(
        min : Option<usize>,
        max : Option<usize>
        ) -> ExtractResult
    {
        assert!(min.is_some());

        ExtractResult::Error(
            ValidationError::Short(min, max)
            )
    }

    //*********************************************************************************************
    /// A numeric value is too small.
    ///
    /// # Panics
    ///
    /// This method will panic if min is None.
    pub fn underrun(
        min : Option<i64>,
        max : Option<i64>
        ) -> ExtractResult
    {
        assert!(min.is_some());

        ExtractResult::Error(
            ValidationError::Underrun(min, max)
            )
    }
}

use std::fmt::{Debug, Formatter, Error};

impl Debug for ExtractResult
{
    //*********************************************************************************************
    /// Display a nice message.
    fn fmt(
        &self,
        fmt : &mut Formatter
        ) -> Result<(), Error>
    {
        match *self
        {
            ExtractResult::Error(ref err) => write!(fmt, "Error({:?})", err),
            ExtractResult::Item(ref val)  => write!(fmt, "Item({:?})", val),
            ExtractResult::Group(_)       => write!(fmt, "Group")
        }
    }
}