More notifications and move start code to another file

This commit is contained in:
micialware committed 2026-08-23 21:49:01 +03:00
1 parent e1bfd9494d
commit 5a926b1419
17 files changed
+240 -198

No files matched your search

+57 -41
View File
@@ -1,3 +1,4 @@
use crate::notification::NotificationData;
use hashbrown::HashMap;
use rusqlite::Connection;
use serde_json::Value;
@@ -21,58 +22,73 @@ pub struct ImportNote {
pub fields: Vec<String>,
}
pub fn load_groups(connection: &Connection) -> ImportData {
pub fn load_groups(connection: &Connection) -> Result<ImportData, NotificationData> {
let json_models: String = connection
.query_one("select models from col", (), |row| row.get(0))
.unwrap();
let raw: Value = serde_json::from_str(json_models.as_str()).unwrap();
let sets = raw.as_object().unwrap();
let mut total_data = ImportData(vec![]);
.map_err(|_| NotificationData::IncorrectAnkiFile)?;
let raw: Value = serde_json::from_str(json_models.as_str())
.map_err(|_| NotificationData::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")
.unwrap();
let mut count_stmt = connection
.prepare("select mid, count(id) from notes group by mid")
.map_err(|_| NotificationData::IncorrectAnkiFile)?;
let counts = count_stmt
.query_map([], |row| Ok((row.get(0)?, row.get(1)?)))
.unwrap()
.map(|x| x.unwrap())
.collect::<Vec<(i64, i64)>>();
let counts = count_stmt
.query_map([], |row| Ok((row.get(0)?, row.get(1)?)))
.unwrap()
.map(|x| x.unwrap())
.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()
.iter()
.map(|x| x["name"].as_str().unwrap().to_string())
.collect();
let mut group = ImportGroup {
id: collection["id"].as_u64().unwrap(),
name: collection["name"].as_str().unwrap().to_string(),
fields,
mapping: Default::default(),
length: 0,
imported: false,
};
if let Some((_, count)) = counts.iter().find(|(id, _)| *id == group.id as i64) {
group.length = *count as u64
for (_, collection) in sets {
let fields_property = collection.get("fields");
if let Some(fields_property) = fields_property {
let fields = fields_property
.as_array()
.unwrap()
.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() {
return Err(NotificationData::IncorrectAnkiFile);
}
let id = id.unwrap().as_u64().unwrap();
let name = collection.get("name");
if name.is_none() {
return Err(NotificationData::IncorrectAnkiFile);
}
let name = name.unwrap().as_str().unwrap().to_string();
let mut group = ImportGroup {
id,
name,
fields,
mapping: Default::default(),
length: 0,
imported: false,
};
if let Some((_, count)) = counts.iter().find(|(id, _)| *id == group.id as i64) {
group.length = *count as u64
}
println!("{:?}", group);
total_data.0.push(group);
}
println!("{:?}", group);
total_data.0.push(group);
}
return Ok(total_data)
}
total_data
Err(NotificationData::IncorrectAnkiFile)
}
pub fn get_words_of_group(connection: &Connection, group_id: u64) -> Vec<ImportNote> {
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;")
.unwrap();
.map_err(|_| NotificationData::IncorrectAnkiFile)?;
count_stmt
Ok(count_stmt
.query_map((group_id as i64,), |row| {
Ok(ImportNote {
tags: row.get(0)?,
@@ -83,7 +99,7 @@ pub fn get_words_of_group(connection: &Connection, group_id: u64) -> Vec<ImportN
.collect(),
})
})
.unwrap()
.map_err(|_| NotificationData::IncorrectAnkiFile)?
.map(|x| x.unwrap())
.collect::<Vec<ImportNote>>()
.collect::<Vec<ImportNote>>())
}
+9 -7
View File
@@ -3,12 +3,14 @@ use reqwest::Client;
use sha2::{Digest, Sha256};
use std::fs::File;
use std::io::BufReader;
use crate::notification::NotificationData;
use crate::notification::NotificationData::*;
pub async fn get_voice(text: &str) -> Result<BufReader<File>, ()> {
pub async fn get_voice(text: &str) -> Result<BufReader<File>, NotificationData> {
let mut path = app_data_dir();
path.push("voice");
if !path.exists() {
std::fs::create_dir(path.clone()).map_err(|_| ())?;
std::fs::create_dir(path.clone()).map_err(|_| PermissionDenied)?;
}
let hash = format!("{}.wav", hex::encode(Sha256::digest(text.as_bytes())));
path.push(hash);
@@ -20,7 +22,7 @@ pub async fn get_voice(text: &str) -> Result<BufReader<File>, ()> {
.send()
.await;
let query = query.map_err(|_| ())?.text().await.map_err(|_| ())?;
let query = query.map_err(|_| VoiceVoxUnavailable)?.text().await.map_err(|_| VoiceVoxUnavailable)?;
// Synthesis
let audio = client
.post(format!("{engine_url}/synthesis?speaker=11"))
@@ -28,13 +30,13 @@ pub async fn get_voice(text: &str) -> Result<BufReader<File>, ()> {
.body(query)
.send()
.await
.map_err(|_| ())?
.map_err(|_| VoiceVoxUnavailable)?
.bytes()
.await
.map_err(|_| ())?;
.map_err(|_| VoiceVoxUnavailable)?;
tokio::fs::write(&path, &audio).await.map_err(|_| ())?;
tokio::fs::write(&path, &audio).await.map_err(|_| PermissionDenied)?;
}
Ok(BufReader::new(File::open(path).map_err(|_| ())?))
Ok(BufReader::new(File::open(path).map_err(|_| PermissionDenied)?))
}