aquaengine/engine.rs
1//! Engine definition
2
3use std::result;
4use error::Engine as Error;
5use version::Version;
6
7/// Result for a engine
8pub type Result<T> = result::Result<T, Error>;
9
10/// The default engine
11///
12/// # Example
13/// ```
14/// use aquaengine::engine::Aqua;
15/// let engine = Aqua::default();
16/// ```
17#[derive(Debug, Clone, Copy)]
18pub struct Aqua {}
19
20/// Engine definition
21///
22/// The default engine is `Aqua`.
23///
24/// # Example to implements a custom engine.
25/// ```
26/// use aquaengine::version::Version;
27/// use aquaengine::engine::{Engine, Result};
28///
29/// struct QuxEngine{}
30///
31/// impl Engine for QuxEngine {
32/// fn name(&self) -> Result<String> {
33/// let name = String::from("QuxEngine");
34/// Ok(name)
35/// }
36///
37/// fn version(&self) -> Result<Version> {
38/// Ok(Version::new(1, 0, 0))
39/// }
40/// }
41///
42/// let qux_engine = QuxEngine{};
43/// ```
44pub trait Engine {
45 /// Returns the engine name
46 ///
47 /// # Example
48 /// ```
49 /// use aquaengine::error::Engine as Error;
50 /// use aquaengine::engine::{Engine, Aqua};
51 ///
52 /// fn engine_name() -> Result<String, Error> {
53 /// let engine = Aqua{};
54 /// let name = engine.name()?;
55 ///
56 /// Ok(name)
57 /// }
58 ///
59 /// fn main() {
60 /// let name = engine_name().unwrap();
61 /// assert_eq!(name, "aquaengine");
62 /// }
63 /// ```
64 fn name(&self) -> Result<String>;
65
66 /// Returns the engine version
67 ///
68 /// # Example
69 /// ```
70 /// use aquaengine::version::Version;
71 /// use aquaengine::error::Engine as Error;
72 /// use aquaengine::engine::{Engine, Aqua};
73 ///
74 /// fn engine_version() -> Result<Version, Error> {
75 /// let engine = Aqua{};
76 /// let version = engine.version()?;
77 ///
78 /// Ok(version)
79 /// }
80 ///
81 /// fn main() {
82 /// let version = engine_version().unwrap();
83 /// assert_eq!(version, Version::new(0, 0, 1));
84 ///
85 /// }
86 /// ```
87 fn version(&self) -> Result<Version>;
88}
89
90impl Default for Aqua {
91 fn default() -> Self {
92 Self {}
93 }
94}
95
96impl Engine for Aqua {
97 fn name(&self) -> Result<String> {
98 let name = String::from(env!("CARGO_PKG_NAME"));
99 Ok(name)
100 }
101
102 fn version(&self) -> Result<Version> {
103 let version_major = String::from(env!("CARGO_PKG_VERSION_MAJOR"));
104
105 let version_major = match version_major.parse::<u16>() {
106 Ok(v) => v,
107 Err(_) => {
108 return Err(Error::InvalidVersion)
109 }
110 };
111
112 let version_minor = String::from(env!("CARGO_PKG_VERSION_MINOR"));
113 let version_minor = match version_minor.parse::<u16>() {
114 Ok(v) => v,
115 Err(_) => {
116 return Err(Error::InvalidVersion)
117 }
118 };
119
120 let version_patch = String::from(env!("CARGO_PKG_VERSION_PATCH"));
121 let version_patch = match version_patch.parse::<u16>() {
122 Ok(v) => v,
123 Err(_) => {
124 return Err(Error::InvalidVersion)
125 }
126 };
127
128 Ok(Version::new(version_major, version_minor, version_patch))
129 }
130}