aquaengine 0.0.2

AquaEngine is a RAD framework for graphics and computes
Documentation
//! Engine definition

use std::result;
use error::Engine as Error;
use version::Version;

/// Result for a engine
pub type Result<T> = result::Result<T, Error>;

/// The default engine
///
/// # Example
/// ```
/// use aquaengine::engine::Aqua;
/// let engine = Aqua::default();
/// ```
#[derive(Debug, Clone, Copy)]
pub struct Aqua {}

/// Engine definition
///
/// The default engine is `Aqua`.
///
/// # Example to implements a custom engine.
/// ```
///  use aquaengine::version::Version;
///  use aquaengine::engine::{Engine, Result};
///
///  struct QuxEngine{}
///
///  impl Engine for QuxEngine {
///      fn name(&self) -> Result<String> {
///          let name = String::from("QuxEngine");
///          Ok(name)
///      }
///
///      fn version(&self) -> Result<Version> {
///          Ok(Version::new(1, 0, 0))
///      }
///  }
///
///  let qux_engine = QuxEngine{};
/// ```
pub trait Engine {
    /// Returns the engine name
    ///
    /// # Example
    /// ```
    /// use aquaengine::error::Engine as Error;
    /// use aquaengine::engine::{Engine, Aqua};
    ///
    /// fn engine_name() -> Result<String, Error> {
    ///     let engine = Aqua{};
    ///     let name = engine.name()?;
    ///
    ///     Ok(name)
    /// }
    ///
    /// fn main() {
    ///     let name = engine_name().unwrap();
    ///     assert_eq!(name, "aquaengine");
    /// }
    /// ```
    fn name(&self) -> Result<String>;

    /// Returns the engine version
    ///
    /// # Example
    /// ```
    /// use aquaengine::version::Version;
    /// use aquaengine::error::Engine as Error;
    /// use aquaengine::engine::{Engine, Aqua};
    ///
    /// fn engine_version() -> Result<Version, Error> {
    ///     let engine = Aqua{};
    ///     let version = engine.version()?;
    ///
    ///     Ok(version)
    /// }
    ///
    /// fn main() {
    ///     let version = engine_version().unwrap();
    ///     assert_eq!(version, Version::new(0, 0, 1));
    ///
    /// }
    /// ```
    fn version(&self) -> Result<Version>;
}

impl Default for Aqua {
    fn default() -> Self {
        Self {}
    }
}

impl Engine for Aqua {
    fn name(&self) -> Result<String> {
        let name = String::from(env!("CARGO_PKG_NAME"));
        Ok(name)
    }

    fn version(&self) -> Result<Version> {
        let version_major = String::from(env!("CARGO_PKG_VERSION_MAJOR"));

        let version_major = match version_major.parse::<u16>() {
            Ok(v) => v,
            Err(_) => {
                return Err(Error::InvalidVersion)
            }
        };

        let version_minor = String::from(env!("CARGO_PKG_VERSION_MINOR"));
        let version_minor = match version_minor.parse::<u16>() {
            Ok(v) => v,
            Err(_) => {
                return Err(Error::InvalidVersion)
            }
        };

        let version_patch = String::from(env!("CARGO_PKG_VERSION_PATCH"));
        let version_patch = match version_patch.parse::<u16>() {
            Ok(v) => v,
            Err(_) => {
                return Err(Error::InvalidVersion)
            }
        };

        Ok(Version::new(version_major, version_minor, version_patch))
    }
}