Introduce MaybeError type
This commit is contained in:
1 parent
d836b57d2a
commit
a60ed37cd4
16 files changed
+127
-93
No files matched your search
+13
-12
@@ -3,6 +3,8 @@ use hashbrown::HashMap;
|
||||
use log::info;
|
||||
use rusqlite::Connection;
|
||||
use serde_json::Value;
|
||||
use crate::helpers::into_result;
|
||||
use crate::notification::NotificationData::IncorrectAnkiFile;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ImportData(pub(crate) Vec<ImportGroup>);
|
||||
@@ -26,41 +28,40 @@ pub struct ImportNote {
|
||||
pub fn load_groups(connection: &Connection) -> Result<ImportData, NotificationData> {
|
||||
let json_models: String = connection
|
||||
.query_one("select models from col", (), |row| row.get(0))
|
||||
.map_err(|_| NotificationData::IncorrectAnkiFile)?;
|
||||
.map_err(|_| IncorrectAnkiFile)?;
|
||||
let raw: Value = serde_json::from_str(json_models.as_str())
|
||||
.map_err(|_| NotificationData::IncorrectAnkiFile)?;
|
||||
.map_err(|_| IncorrectAnkiFile)?;
|
||||
let sets = raw.as_object();
|
||||
if let Some(sets) = sets {
|
||||
let mut total_data = ImportData(vec![]);
|
||||
|
||||
let mut count_stmt = connection
|
||||
.prepare("select mid, count(id) from notes group by mid")
|
||||
.map_err(|_| NotificationData::IncorrectAnkiFile)?;
|
||||
.map_err(|_| IncorrectAnkiFile)?;
|
||||
|
||||
let counts = count_stmt
|
||||
.query_map([], |row| Ok((row.get(0)?, row.get(1)?)))
|
||||
.map_err(|_| NotificationData::IncorrectAnkiFile)?
|
||||
.map_err(|_| IncorrectAnkiFile)?
|
||||
.filter_map(|x| x.ok())
|
||||
.collect::<Vec<(i64, i64)>>();
|
||||
|
||||
for (_, collection) in sets {
|
||||
let fields_property = collection.get("fields");
|
||||
if let Some(fields_property) = fields_property {
|
||||
let fields = fields_property
|
||||
.as_array()
|
||||
.unwrap()
|
||||
let fields = into_result(fields_property
|
||||
.as_array(), IncorrectAnkiFile)?
|
||||
.iter()
|
||||
.filter_map(|x| x.get("name"))
|
||||
.map(|x| x.as_str().unwrap().to_string())
|
||||
.collect();
|
||||
let id = collection.get("id");
|
||||
if id.is_none() || id.unwrap().as_u64().is_none(){
|
||||
return Err(NotificationData::IncorrectAnkiFile);
|
||||
return Err(IncorrectAnkiFile);
|
||||
}
|
||||
let id = id.unwrap().as_u64().unwrap();
|
||||
let name = collection.get("name");
|
||||
if name.is_none() || name.unwrap().as_str().is_none() {
|
||||
return Err(NotificationData::IncorrectAnkiFile);
|
||||
return Err(IncorrectAnkiFile);
|
||||
}
|
||||
let name = name.unwrap().as_str().unwrap().to_string();
|
||||
|
||||
@@ -81,13 +82,13 @@ pub fn load_groups(connection: &Connection) -> Result<ImportData, NotificationDa
|
||||
}
|
||||
return Ok(total_data)
|
||||
}
|
||||
Err(NotificationData::IncorrectAnkiFile)
|
||||
Err(IncorrectAnkiFile)
|
||||
}
|
||||
|
||||
pub fn get_words_of_group(connection: &Connection, group_id: u64) -> Result<Vec<ImportNote>, NotificationData> {
|
||||
let mut count_stmt = connection
|
||||
.prepare("select tags, flds from notes where mid == ?1;")
|
||||
.map_err(|_| NotificationData::IncorrectAnkiFile)?;
|
||||
.map_err(|_| IncorrectAnkiFile)?;
|
||||
|
||||
Ok(count_stmt
|
||||
.query_map((group_id as i64,), |row| {
|
||||
@@ -100,7 +101,7 @@ pub fn get_words_of_group(connection: &Connection, group_id: u64) -> Result<Vec<
|
||||
.collect(),
|
||||
})
|
||||
})
|
||||
.map_err(|_| NotificationData::IncorrectAnkiFile)?
|
||||
.map_err(|_| IncorrectAnkiFile)?
|
||||
.filter_map(|x| x.ok())
|
||||
.collect::<Vec<ImportNote>>())
|
||||
}
|
||||
Reference in new issue
Block a user