Splitting kana set
This commit is contained in:
1 parent
36e5693564
commit
c74a0eb311
25 files changed
+389
-354
No files matched your search
@@ -1,20 +1,21 @@
|
||||
use crate::dictionary::app_data_dir;
|
||||
use crate::fs::*;
|
||||
use crate::lang::{Id, WordOpenMode};
|
||||
use chrono::{DateTime, Utc};
|
||||
use std::fs;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::Write;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::path::PathBuf;
|
||||
use crate::notification::NotificationData;
|
||||
use crate::notification::NotificationData::PermissionDenied;
|
||||
|
||||
pub fn get_history_of_set(id: Id) -> Vec<HistoryItem> {
|
||||
let app_dir = history_dir();
|
||||
pub fn get_history_of_set(id: Id) -> Result<Vec<HistoryItem>, NotificationData> {
|
||||
let app_dir = history_dir()?;
|
||||
let head = app_dir.clone().join(format!("set_{}_history.csv", id));
|
||||
let mut lines = Vec::new();
|
||||
|
||||
append_lines_if_exists(head, &mut lines);
|
||||
lines.reverse();
|
||||
parse_history_items(lines)
|
||||
Ok(parse_history_items(lines))
|
||||
}
|
||||
|
||||
fn append_lines_if_exists(path: PathBuf, lines: &mut Vec<String>) {
|
||||
@@ -56,15 +57,15 @@ fn parse_history_items(strings: Vec<String>) -> Vec<HistoryItem> {
|
||||
items
|
||||
}
|
||||
|
||||
pub fn push_note(set_id: Id, item: HistoryItem) {
|
||||
let app_dir = history_dir();
|
||||
pub fn push_note(set_id: Id, item: HistoryItem) -> Result<(), NotificationData> {
|
||||
let app_dir = history_dir()?;
|
||||
let path = app_dir.clone().join(format!("set_{}_history.csv", set_id));
|
||||
|
||||
let mut file = OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(path)
|
||||
.unwrap();
|
||||
.map_err(|_| PermissionDenied)?;
|
||||
let line_str = format!(
|
||||
"{};{};{};{};{}",
|
||||
item.timestamp.timestamp(),
|
||||
@@ -78,15 +79,8 @@ pub fn push_note(set_id: Id, item: HistoryItem) {
|
||||
item.before,
|
||||
item.after
|
||||
);
|
||||
writeln!(&mut file, "{}", line_str).unwrap();
|
||||
}
|
||||
|
||||
pub fn history_dir() -> PathBuf {
|
||||
let directory = app_data_dir().join("history");
|
||||
if !directory.exists() {
|
||||
fs::create_dir(directory.clone()).unwrap();
|
||||
}
|
||||
directory
|
||||
writeln!(&mut file, "{}", line_str).map_err(|_| PermissionDenied)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
use crate::dictionary::app_data_dir;
|
||||
use rusqlite::Connection;
|
||||
use crate::fs::*;
|
||||
|
||||
pub fn create_db() {
|
||||
let path = app_data_dir();
|
||||
let db_file = path.join("data.db");
|
||||
let db_file = db_path();
|
||||
if !db_file.exists() {
|
||||
std::fs::File::create(&db_file).unwrap();
|
||||
let connection = Connection::open(&db_file).unwrap();
|
||||
@@ -51,8 +50,7 @@ fn make_card_stats(conn: &Connection) -> Result<(), rusqlite::Error> {
|
||||
}
|
||||
|
||||
pub fn default_connection() -> Connection {
|
||||
let path = app_data_dir();
|
||||
let db_file = path.join("data.db");
|
||||
let db_file = db_path();
|
||||
let connection = Connection::open(db_file).unwrap();
|
||||
connection.execute("PRAGMA foreign_keys = ON;", []).unwrap();
|
||||
connection
|
||||
|
||||
@@ -1,17 +1,14 @@
|
||||
use crate::dictionary::app_data_dir;
|
||||
use reqwest::Client;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use crate::fs::*;
|
||||
use crate::notification::NotificationData;
|
||||
use crate::notification::NotificationData::*;
|
||||
|
||||
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(|_| PermissionDenied)?;
|
||||
}
|
||||
let mut path = voice_dir()?;
|
||||
|
||||
let hash = format!("{}.wav", hex::encode(Sha256::digest(text.as_bytes())));
|
||||
path.push(hash);
|
||||
if !path.exists() {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use crate::dictionary::app_data_dir;
|
||||
use std::io;
|
||||
use log::info;
|
||||
use tokio::fs::{File, OpenOptions};
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use zstd::{DEFAULT_COMPRESSION_LEVEL, Decoder, Encoder};
|
||||
use crate::fs::*;
|
||||
use crate::notification::NotificationData;
|
||||
use crate::notification::NotificationData::{PermissionDenied, WebApiUnavailable};
|
||||
|
||||
@@ -11,8 +11,7 @@ use crate::notification::NotificationData::{PermissionDenied, WebApiUnavailable}
|
||||
const API_URL: &str = "https://learning.micialware.ru/";
|
||||
|
||||
pub async fn send_data(id: String) -> Result<(), NotificationData> {
|
||||
let path = app_data_dir();
|
||||
let db_file = path.join("data.db");
|
||||
let db_file = db_path();
|
||||
let data = tokio::fs::read(db_file).await.map_err(|_| WebApiUnavailable)?;
|
||||
let data = compress(data);
|
||||
|
||||
@@ -102,7 +101,7 @@ pub async fn get_web_version(key: &str) -> Result<u32, NotificationData> {
|
||||
}
|
||||
|
||||
pub async fn get_local_version() -> u32 {
|
||||
let file = app_data_dir().join("data.meta");
|
||||
let file = db_meta_path();
|
||||
if file.exists() {
|
||||
let mut data = String::new();
|
||||
File::open(file.clone())
|
||||
@@ -126,7 +125,7 @@ pub async fn get_local_version() -> u32 {
|
||||
}
|
||||
|
||||
pub async fn set_local_version(version: u32) {
|
||||
let file_path = app_data_dir().join("data.meta");
|
||||
let file_path = db_meta_path();
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
|
||||
Reference in new issue
Block a user