[niepce: 21/29] fwk+engine+rust: Label and RgbColour in Rust



commit 7dfdf3b8e23ad01b3f8c46a09253fc8a53a75c03
Author: Hubert Figuière <hub figuiere net>
Date:   Fri Sep 1 23:27:22 2017 -0400

    fwk+engine+rust: Label and RgbColour in Rust

 src/engine/db/label.rs    |   79 +++++++++++++++++++++++++++++++++++++++++++++
 src/engine/db/mod.rs      |    1 +
 src/fwk/base/mod.rs       |    3 +-
 src/fwk/base/rgbcolour.rs |   51 +++++++++++++++++++++++++++++
 4 files changed, 133 insertions(+), 1 deletions(-)
---
diff --git a/src/engine/db/label.rs b/src/engine/db/label.rs
new file mode 100644
index 0000000..0b57103
--- /dev/null
+++ b/src/engine/db/label.rs
@@ -0,0 +1,79 @@
+/*
+ * niepce - engine/db/label.rs
+ *
+ * Copyright (C) 2017 Hubert Figuière
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+use std::str::FromStr;
+use rusqlite;
+
+use super::LibraryId;
+use super::FromDb;
+use fwk::base::rgbcolour::RgbColour;
+
+pub struct Label {
+    id: LibraryId,
+    label: String,
+    colour: RgbColour,
+}
+
+impl Label {
+
+    pub fn new(id: LibraryId, label: &str, colourstring: &str) -> Label {
+        let colour = RgbColour::from_str(colourstring).unwrap_or(Default::default());
+        Label {
+            id: id,
+            label: String::from(label),
+            colour: colour
+        }
+    }
+
+    pub fn id(&self) -> LibraryId {
+        self.id
+    }
+
+    pub fn label(&self) -> &str {
+        &self.label
+    }
+
+    pub fn set_label(&mut self, label: &str) {
+        self.label = String::from(label)
+    }
+
+    pub fn colour(&self) -> &RgbColour {
+        &self.colour
+    }
+
+    pub fn set_colour(&mut self, c: &RgbColour) {
+        self.colour = c.clone();
+    }
+}
+
+impl FromDb for Label {
+    fn read_db_columns() -> &'static str {
+        "id,name,color"
+    }
+
+    fn read_db_tables() -> &'static str {
+        "labels"
+    }
+
+    fn read_from(row: &rusqlite::Row) -> Self {
+        let label : String = row.get(1);
+        let colour : String = row.get(2);
+        Label::new(row.get(0), &label, &colour)
+    }
+}
diff --git a/src/engine/db/mod.rs b/src/engine/db/mod.rs
index 6cd0204..b946445 100644
--- a/src/engine/db/mod.rs
+++ b/src/engine/db/mod.rs
@@ -20,6 +20,7 @@
 pub mod filebundle;
 pub mod fsfile;
 pub mod keyword;
+pub mod label;
 pub mod libfile;
 pub mod libfolder;
 pub mod library;
diff --git a/src/fwk/base/mod.rs b/src/fwk/base/mod.rs
index 1cf2abe..56b4402 100644
--- a/src/fwk/base/mod.rs
+++ b/src/fwk/base/mod.rs
@@ -17,9 +17,10 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-pub mod fractions;
 pub mod date;
 #[macro_use]
 pub mod debug;
+pub mod fractions;
+pub mod rgbcolour;
 
 pub type PropertyIndex = u32;
diff --git a/src/fwk/base/rgbcolour.rs b/src/fwk/base/rgbcolour.rs
new file mode 100644
index 0000000..0812dce
--- /dev/null
+++ b/src/fwk/base/rgbcolour.rs
@@ -0,0 +1,51 @@
+
+
+use std::num::ParseIntError;
+use std::str::FromStr;
+
+#[repr(C)]
+#[derive(Clone,Default)]
+pub struct RgbColour {
+    r: u16,
+    g: u16,
+    b: u16
+}
+
+#[derive(Debug)]
+pub enum ColourParseError {
+    /// No Error.
+    None,
+    /// Parse Error.
+    ParseError,
+    /// Error parsing one of the 3 int components.
+    ParseIntError,
+}
+
+impl From<ParseIntError> for ColourParseError {
+    fn from(e: ParseIntError) -> ColourParseError {
+        ColourParseError::ParseIntError
+    }
+}
+
+impl RgbColour {
+
+    pub fn new(r: u16, g: u16, b: u16) -> RgbColour {
+        RgbColour{r: r, g: g, b: b}
+    }
+}
+
+impl FromStr for RgbColour {
+
+    type Err = ColourParseError;
+
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        let v: Vec<&str> = s.split(' ').collect();
+        if v.len() != 3 {
+            return Err(ColourParseError::ParseError);
+        }
+        let r = u16::from_str_radix(v[0], 10)?;
+        let g = u16::from_str_radix(v[1], 10)?;
+        let b = u16::from_str_radix(v[2], 10)?;
+        Ok(RgbColour::new(r, g, b))
+    }
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]