[niepce] library: return Option<> when applicable
- From: Hubert Figuière <hub src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [niepce] library: return Option<> when applicable
- Date: Fri, 17 Nov 2017 02:17:06 +0000 (UTC)
commit dc5bdba9ae851606642eb6975723be4f7ce99e3d
Author: Hubert Figuière <hub figuiere net>
Date: Sun Nov 5 20:15:35 2017 -0500
library: return Option<> when applicable
src/engine/db/library.rs | 124 ++++++++++++++++++---------------------
src/engine/library/commands.rs | 68 +++++++++++++---------
2 files changed, 96 insertions(+), 96 deletions(-)
---
diff --git a/src/engine/db/library.rs b/src/engine/db/library.rs
index 4bc0ca2..a47ef95 100644
--- a/src/engine/db/library.rs
+++ b/src/engine/db/library.rs
@@ -234,27 +234,21 @@ impl Library {
Some(String::from(try_opt!(name.to_str())))
}
- fn get_content(&self, id: LibraryId, sql_where: &str) -> Vec<LibFile> {
- if let Some(ref conn) = self.dbconn {
- let sql = format!("SELECT {} FROM {} \
- WHERE {} \
- AND files.main_file=fsfiles.id",
- LibFile::read_db_columns(),
- LibFile::read_db_tables(),
- sql_where);
- if let Ok(mut stmt) = conn.prepare(&sql) {
- let mut files: Vec<LibFile> = vec!();
- let mut rows = stmt.query(&[&id]).unwrap();
- while let Some(Ok(row)) = rows.next() {
- let libfile = LibFile::read_from(&row);
- files.push(libfile);
- }
- return files;
- }
+ fn get_content(&self, id: LibraryId, sql_where: &str) -> Option<Vec<LibFile>> {
+ let conn = try_opt!(self.dbconn.as_ref());
+ let sql = format!("SELECT {} FROM {} \
+ WHERE {} \
+ AND files.main_file=fsfiles.id",
+ LibFile::read_db_columns(),
+ LibFile::read_db_tables(),
+ sql_where);
+ let mut stmt = try_opt!(conn.prepare(&sql).ok());
+ let mut rows = try_opt!(stmt.query(&[&id]).ok());
+ let mut files: Vec<LibFile> = vec!();
+ while let Some(Ok(row)) = rows.next() {
+ files.push(LibFile::read_from(&row));
}
-
- vec!()
-
+ Some(files)
}
pub fn add_folder(&self, folder: &str) -> Option<LibFolder> {
@@ -291,25 +285,21 @@ impl Library {
return Some(LibFolder::read_from(&row));
}
- pub fn get_all_folders(&self) -> Vec<LibFolder> {
- if let Some(ref conn) = self.dbconn {
- let sql = format!("SELECT {} FROM {}",
- LibFolder::read_db_columns(),
- LibFolder::read_db_tables());
- if let Ok(mut stmt) = conn.prepare(&sql) {
- let mut folders: Vec<LibFolder> = vec!();
- let mut rows = stmt.query(&[]).unwrap();
- while let Some(Ok(row)) = rows.next() {
- let libfolder = LibFolder::read_from(&row);
- folders.push(libfolder);
- }
- return folders;
- }
+ pub fn get_all_folders(&self) -> Option<Vec<LibFolder>> {
+ let conn = try_opt!(self.dbconn.as_ref());
+ let sql = format!("SELECT {} FROM {}",
+ LibFolder::read_db_columns(),
+ LibFolder::read_db_tables());
+ let mut stmt = try_opt!(conn.prepare(&sql).ok());
+ let mut rows = try_opt!(stmt.query(&[]).ok());
+ let mut folders: Vec<LibFolder> = vec!();
+ while let Some(Ok(row)) = rows.next() {
+ folders.push(LibFolder::read_from(&row));
}
- vec!()
+ Some(folders)
}
- pub fn get_folder_content(&self, folder_id: LibraryId) -> Vec<LibFile> {
+ pub fn get_folder_content(&self, folder_id: LibraryId) -> Option<Vec<LibFile>> {
self.get_content(folder_id, "parent_id = :1")
}
@@ -326,22 +316,18 @@ impl Library {
-1
}
- pub fn get_all_keywords(&self) -> Vec<Keyword> {
- if let Some(ref conn) = self.dbconn {
- let sql = format!("SELECT {} FROM {}",
- Keyword::read_db_columns(),
- Keyword::read_db_tables());
- if let Ok(mut stmt) = conn.prepare(&sql) {
- let mut keywords: Vec<Keyword> = vec!();
- let mut rows = stmt.query(&[]).unwrap();
- while let Some(Ok(row)) = rows.next() {
- let keyword = Keyword::read_from(&row);
- keywords.push(keyword);
- }
- return keywords;
- }
+ pub fn get_all_keywords(&self) -> Option<Vec<Keyword>> {
+ let conn = try_opt!(self.dbconn.as_ref());
+ let sql = format!("SELECT {} FROM {}",
+ Keyword::read_db_columns(),
+ Keyword::read_db_tables());
+ let mut stmt = try_opt!(conn.prepare(&sql).ok());
+ let mut rows = try_opt!(stmt.query(&[]).ok());
+ let mut keywords: Vec<Keyword> = vec!();
+ while let Some(Ok(row)) = rows.next() {
+ keywords.push(Keyword::read_from(&row));
}
- vec!()
+ Some(keywords)
}
pub fn add_fs_file(&self, file: &str) -> LibraryId {
@@ -511,7 +497,7 @@ impl Library {
false
}
- pub fn get_keyword_content(&self, keyword_id: LibraryId) -> Vec<LibFile> {
+ pub fn get_keyword_content(&self, keyword_id: LibraryId) -> Option<Vec<LibFile>> {
self.get_content(keyword_id, "files.id IN \
(SELECT file_id FROM keywording \
WHERE keyword_id=:1) ")
@@ -654,22 +640,18 @@ impl Library {
false
}
- pub fn get_all_labels(&self) -> Vec<Label> {
- if let Some(ref conn) = self.dbconn {
- let sql = format!("SELECT {} FROM {} ORDER BY id;",
- Label::read_db_columns(),
- Label::read_db_tables());
- if let Ok(mut stmt) = conn.prepare(&sql) {
- let mut labels: Vec<Label> = vec!();
- let mut rows = stmt.query(&[]).unwrap();
- while let Some(Ok(row)) = rows.next() {
- let label = Label::read_from(&row);
- labels.push(label);
- }
- return labels;
- }
+ pub fn get_all_labels(&self) -> Option<Vec<Label>> {
+ let conn = try_opt!(self.dbconn.as_ref());
+ let sql = format!("SELECT {} FROM {} ORDER BY id;",
+ Label::read_db_columns(),
+ Label::read_db_tables());
+ let mut stmt = try_opt!(conn.prepare(&sql).ok());
+ let mut rows = try_opt!(stmt.query(&[]).ok());
+ let mut labels: Vec<Label> = vec!();
+ while let Some(Ok(row)) = rows.next() {
+ labels.push(Label::read_from(&row));
}
- vec!()
+ Some(labels)
}
pub fn add_label(&self, name: &str, colour: &str) -> LibraryId {
@@ -854,6 +836,8 @@ mod test {
assert_eq!(f.parent(), id);
let folders = lib.get_all_folders();
+ assert!(folders.is_some());
+ let folders = folders.unwrap();
assert_eq!(folders.len(), 3);
let file_id = lib.add_file(folder_added.id(), "foo/myfile", super::Managed::NO);
@@ -865,6 +849,8 @@ mod test {
assert_eq!(count, 1);
let fl = lib.get_folder_content(folder_added.id());
+ assert!(fl.is_some());
+ let fl = fl.unwrap();
assert_eq!(fl.len(), count as usize);
assert_eq!(fl[0].id(), file_id);
@@ -882,10 +868,14 @@ mod test {
assert!(lib.assign_keyword(kwid2, file_id));
let fl2 = lib.get_keyword_content(kwid1);
+ assert!(fl2.is_some());
+ let fl2 = fl2.unwrap();
assert_eq!(fl2.len(), 1);
assert_eq!(fl2[0].id(), file_id);
let kl = lib.get_all_keywords();
+ assert!(kl.is_some());
+ let kl = kl.unwrap();
assert_eq!(kl.len(), 2);
}
}
diff --git a/src/engine/library/commands.rs b/src/engine/library/commands.rs
index 641f332..02d2510 100644
--- a/src/engine/library/commands.rs
+++ b/src/engine/library/commands.rs
@@ -39,21 +39,25 @@ use super::notification::{
use root::eng::NiepceProperties as Np;
pub fn cmd_list_all_keywords(lib: &Library) -> bool {
- let list = lib.get_all_keywords();
- // XXX change this to "LoadKeywords"
- for kw in list {
- lib.notify(Box::new(LibNotification::AddedKeyword(kw)));
+ if let Some(list) = lib.get_all_keywords() {
+ // XXX change this to "LoadKeywords"
+ for kw in list {
+ lib.notify(Box::new(LibNotification::AddedKeyword(kw)));
+ }
+ return true;
}
- true
+ false
}
pub fn cmd_list_all_folders(lib: &Library) -> bool {
- let list = lib.get_all_folders();
- // XXX change this to "LoadedFodlers"
- for folder in list {
- lib.notify(Box::new(LibNotification::AddedFolder(folder)));
+ if let Some(list) = lib.get_all_folders() {
+ // XXX change this to "LoadedFodlers"
+ for folder in list {
+ lib.notify(Box::new(LibNotification::AddedFolder(folder)));
+ }
+ return true;
}
- true
+ false
}
pub fn cmd_import_file(lib: &Library, path: &str, manage: Managed) -> bool {
@@ -118,16 +122,18 @@ pub fn cmd_request_metadata(lib: &Library, file_id: LibraryId) -> bool {
}
pub fn cmd_query_folder_content(lib: &Library, folder_id: LibraryId) -> bool {
- let fl = lib.get_folder_content(folder_id);
- let mut value = Box::new(
- LibNotification::FolderContentQueried(unsafe { Content::new(folder_id) }));
- if let LibNotification::FolderContentQueried(ref mut content) = *value {
- for f in fl {
- unsafe { content.push(Box::into_raw(Box::new(f)) as *mut c_void) };
+ if let Some(fl) = lib.get_folder_content(folder_id) {
+ let mut value = Box::new(
+ LibNotification::FolderContentQueried(unsafe { Content::new(folder_id) }));
+ if let LibNotification::FolderContentQueried(ref mut content) = *value {
+ for f in fl {
+ unsafe { content.push(Box::into_raw(Box::new(f)) as *mut c_void) };
+ }
}
+ lib.notify(value);
+ return true;
}
- lib.notify(value);
- true
+ false
}
pub fn cmd_set_metadata(lib: &Library, id: LibraryId, meta: Np,
@@ -146,13 +152,15 @@ pub fn cmd_count_folder(lib: &Library, folder_id: LibraryId) -> bool {
}
pub fn cmd_query_keyword_content(lib: &Library, keyword_id: LibraryId) -> bool {
- let fl = lib.get_keyword_content(keyword_id);
- let mut content = unsafe { Content::new(keyword_id) };
- for f in fl {
- unsafe { content.push(Box::into_raw(Box::new(f)) as *mut c_void) };
+ if let Some(fl) = lib.get_keyword_content(keyword_id) {
+ let mut content = unsafe { Content::new(keyword_id) };
+ for f in fl {
+ unsafe { content.push(Box::into_raw(Box::new(f)) as *mut c_void) };
+ }
+ lib.notify(Box::new(LibNotification::KeywordContentQueried(content)));
+ return true;
}
- lib.notify(Box::new(LibNotification::KeywordContentQueried(content)));
- true
+ false
}
pub fn cmd_write_metadata(lib: &Library, file_id: LibraryId) -> bool {
@@ -175,12 +183,14 @@ pub fn cmd_move_file_to_folder(lib: &Library, file_id: LibraryId, from: LibraryI
}
pub fn cmd_list_all_labels(lib: &Library) -> bool {
- let l = lib.get_all_labels();
- // XXX change this notification type
- for label in l {
- lib.notify(Box::new(LibNotification::AddedLabel(label)));
+ if let Some(l) = lib.get_all_labels() {
+ // XXX change this notification type
+ for label in l {
+ lib.notify(Box::new(LibNotification::AddedLabel(label)));
+ }
+ return true;
}
- true
+ false
}
pub fn cmd_create_label(lib: &Library, name: &str, colour: &str) -> bool {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]