mirror of
https://github.com/patarapolw/cjdic2.git
synced 2026-11-08 17:35:18 +00:00
remove zipSource
This commit is contained in:
1 parent
9f88b6d93c
commit
413376d3f3
5 files changed
+16
-34
No files matched your search
@@ -10,6 +10,7 @@
|
||||
"build:desktop": "tauri build",
|
||||
"build:android": "tauri android build",
|
||||
"install:android": "adb install -r src-tauri/gen/android/app/build/outputs/apk/universal/release/app-universal-release.apk",
|
||||
"run:android": "tauri android run",
|
||||
"build:android:debug": "tauri android build --debug",
|
||||
"install:android:debug": "adb install -r src-tauri/gen/android/app/build/outputs/apk/universal/debug/app-universal-debug.apk",
|
||||
"run:desktop": ".\\target\\release\\cjdic2.exe",
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ mod yomitan;
|
||||
pub use yomitan::YomitanRow;
|
||||
|
||||
mod yomitan_writer;
|
||||
pub use yomitan_writer::{YomitanProgress, YomitanWriter, YomitanZipImportResult, ZipSource};
|
||||
pub use yomitan_writer::{YomitanProgress, YomitanWriter, YomitanZipImportResult};
|
||||
|
||||
use crate::{CJDicError, tokenizer::Tokenizer};
|
||||
|
||||
|
||||
@@ -4,11 +4,10 @@ use rusqlite::{Connection, OptionalExtension, Result, Transaction, params};
|
||||
use serde::Serialize;
|
||||
use serde_json::Value;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
collections::HashMap,
|
||||
env::current_dir,
|
||||
fs::{File, create_dir_all},
|
||||
io::{self, Cursor, Read},
|
||||
io::{self, Read},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use zip::ZipArchive;
|
||||
@@ -30,21 +29,6 @@ pub(super) fn normalize_term(s: &str) -> String {
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub trait ZipSource {
|
||||
fn file_name(&self) -> &str;
|
||||
fn bytes(&self) -> std::io::Result<Cow<'_, [u8]>>;
|
||||
}
|
||||
|
||||
impl ZipSource for PathBuf {
|
||||
fn file_name(&self) -> &str {
|
||||
Path::file_name(self).and_then(|n| n.to_str()).unwrap_or("")
|
||||
}
|
||||
|
||||
fn bytes(&self) -> std::io::Result<Cow<'_, [u8]>> {
|
||||
std::fs::read(self).map(Cow::Owned)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug, Clone)]
|
||||
pub struct YomitanProgress {
|
||||
pub message: String,
|
||||
@@ -446,24 +430,23 @@ impl YomitanWriter {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn import_dictionary_zip_file<Z, Callback>(
|
||||
pub fn import_dictionary_zip_file<Callback>(
|
||||
&mut self,
|
||||
zip_file: &Z,
|
||||
zip_file: &PathBuf,
|
||||
asset_dir: &PathBuf,
|
||||
lang: &str,
|
||||
progress_callback: Callback,
|
||||
) -> anyhow::Result<YomitanZipImportResult, CJDicError>
|
||||
where
|
||||
Z: ZipSource,
|
||||
Callback: Fn(YomitanProgress),
|
||||
{
|
||||
let bundle_name = zip_file.file_name();
|
||||
let bundle_name = zip_file.file_name().and_then(|n| n.to_str()).unwrap_or("");
|
||||
let _timer = Timer::new(format!("Importing {}", bundle_name));
|
||||
|
||||
self.conn.execute_batch("PRAGMA foreign_keys = off")?;
|
||||
|
||||
let cursor = Cursor::new(zip_file.bytes()?);
|
||||
let mut archive = ZipArchive::new(cursor)?;
|
||||
let file = File::open(&zip_file)?;
|
||||
let mut archive = ZipArchive::new(file)?;
|
||||
|
||||
let mut asset_count = 0;
|
||||
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ mod service;
|
||||
mod tokenizer;
|
||||
mod util;
|
||||
|
||||
pub use db::{YomitanProgress, YomitanRow, YomitanZipImportResult, ZipSource};
|
||||
pub use db::{YomitanProgress, YomitanRow, YomitanZipImportResult};
|
||||
pub use error::CJDicError;
|
||||
pub use service::{AppService, SqlParam, YomitanDictEntry};
|
||||
pub use tokenizer::TokenizeSegment;
|
||||
|
||||
@@ -7,7 +7,7 @@ use rusqlite::{Connection, params_from_iter};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
Timer, ZipSource,
|
||||
Timer,
|
||||
db::{
|
||||
DBFILE, Database, DbChild, YomitanProgress, YomitanRow, YomitanWriter,
|
||||
YomitanZipImportResult,
|
||||
@@ -163,24 +163,23 @@ impl AppService {
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
pub fn load_yomitan_zip_dir<Z, LoadCallback, ImportCallback>(
|
||||
pub fn load_yomitan_zip_dir<LoadCallback, ImportCallback>(
|
||||
&self,
|
||||
zip_dir: Vec<Z>,
|
||||
zip_dir: Vec<PathBuf>,
|
||||
asset_dir: &PathBuf,
|
||||
lang: &str,
|
||||
load_callback: LoadCallback,
|
||||
import_callback: ImportCallback,
|
||||
) -> Result<LoadYomitanZipDirResult, CJDicError>
|
||||
where
|
||||
Z: ZipSource,
|
||||
LoadCallback: Fn(LoadYomitanZipDirResult),
|
||||
ImportCallback: Fn(YomitanProgress),
|
||||
{
|
||||
let mut dir_zip_map: HashMap<String, &Z> = HashMap::new();
|
||||
let mut dir_zip_map: HashMap<String, &PathBuf> = HashMap::new();
|
||||
let mut zip_dir_ord: HashMap<String, usize> = HashMap::new();
|
||||
|
||||
for (i, entry) in zip_dir.iter().enumerate() {
|
||||
let filename = entry.file_name();
|
||||
let filename = entry.file_name().and_then(|n| n.to_str()).unwrap_or("");
|
||||
dir_zip_map.insert(filename.to_string(), entry);
|
||||
zip_dir_ord.insert(filename.to_string(), i);
|
||||
}
|
||||
@@ -276,15 +275,14 @@ impl AppService {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn import_yomitan_zip_file<Z, Callback>(
|
||||
pub fn import_yomitan_zip_file<Callback>(
|
||||
writer: &mut YomitanWriter, // Reuse existing connection
|
||||
zip_file: &Z,
|
||||
zip_file: &PathBuf,
|
||||
asset_dir: &PathBuf,
|
||||
lang: &str,
|
||||
progress_callback: Callback,
|
||||
) -> Result<YomitanZipImportResult, CJDicError>
|
||||
where
|
||||
Z: ZipSource,
|
||||
Callback: Fn(YomitanProgress),
|
||||
{
|
||||
Ok(writer.import_dictionary_zip_file(zip_file, asset_dir, lang, progress_callback)?)
|
||||
|
||||
Reference in new issue
Block a user