diff --git a/crates/voicevox_core_java_api/src/common.rs b/crates/voicevox_core_java_api/src/common.rs index 2da808aae..2c6847f73 100644 --- a/crates/voicevox_core_java_api/src/common.rs +++ b/crates/voicevox_core_java_api/src/common.rs @@ -1,4 +1,4 @@ -use std::{error::Error as _, iter, str::Utf8Error}; +use std::{error::Error as _, iter}; use derive_more::From; use jni::{objects::JThrowable, JNIEnv}; @@ -194,23 +194,16 @@ where JavaApiError::Jni(error) => { or_panic!(env.throw_new("java/lang/RuntimeException", error.to_string())) } - JavaApiError::Utf8(error) => { + JavaApiError::Uuid(error) => { or_panic!( env.throw_new("java/lang/IllegalArgumentException", error.to_string()) ) } - JavaApiError::Uuid(error) => { + JavaApiError::DeJson(error) => { or_panic!( env.throw_new("java/lang/IllegalArgumentException", error.to_string()) ) } - JavaApiError::Json(error) => { - or_panic!(env.throw_new("java/lang/RuntimeException", error.to_string())) - } - JavaApiError::IllegalAccelerationMode => or_panic!(env.throw_new( - "java/lang/IllegalArgumentException", - "Invalid acceleration mode".to_owned(), - )), }; } fallback @@ -226,14 +219,8 @@ pub enum JavaApiError { #[from] Jni(jni::errors::Error), - #[from] - Utf8(Utf8Error), - #[from] Uuid(uuid::Error), - #[from] - Json(serde_json::Error), - - IllegalAccelerationMode, + DeJson(serde_json::Error), } diff --git a/crates/voicevox_core_java_api/src/open_jtalk.rs b/crates/voicevox_core_java_api/src/open_jtalk.rs index 98feaacb0..f26128d35 100644 --- a/crates/voicevox_core_java_api/src/open_jtalk.rs +++ b/crates/voicevox_core_java_api/src/open_jtalk.rs @@ -1,4 +1,7 @@ -use std::sync::{Arc, Mutex}; +use std::{ + borrow::Cow, + sync::{Arc, Mutex}, +}; use crate::common::throw_if_err; use jni::{ @@ -26,7 +29,7 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_OpenJtalk_rsNewWithInit ) { throw_if_err(env, (), |env| { let open_jtalk_dict_dir = env.get_string(&open_jtalk_dict_dir)?; - let open_jtalk_dict_dir = open_jtalk_dict_dir.to_str()?; + let open_jtalk_dict_dir = &*Cow::from(&open_jtalk_dict_dir); let internal = voicevox_core::OpenJtalk::new_with_initialize(open_jtalk_dict_dir)?; env.set_rust_field(&this, "handle", Arc::new(internal))?; diff --git a/crates/voicevox_core_java_api/src/synthesizer.rs b/crates/voicevox_core_java_api/src/synthesizer.rs index 94a5ed16f..3ddd1b47f 100644 --- a/crates/voicevox_core_java_api/src/synthesizer.rs +++ b/crates/voicevox_core_java_api/src/synthesizer.rs @@ -39,7 +39,7 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsNewWithIn } else if env.is_same_object(&acceleration_mode, gpu)? { voicevox_core::AccelerationMode::Gpu } else { - return Err(JavaApiError::IllegalAccelerationMode); + panic!("予期しない`AccelerationMode`です: {acceleration_mode:?}"); }; } let cpu_num_threads = env.get_field(&builder, "cpuNumThreads", "I")?; @@ -150,7 +150,7 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsAudioQuer )? }; - let query_json = serde_json::to_string(&audio_query)?; + let query_json = serde_json::to_string(&audio_query).expect("should not fail"); let j_audio_query = env.new_string(query_json)?; @@ -178,7 +178,7 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsAudioQuer RUNTIME.block_on(internal.audio_query(&text, voicevox_core::StyleId::new(style_id)))? }; - let query_json = serde_json::to_string(&audio_query)?; + let query_json = serde_json::to_string(&audio_query).expect("should not fail"); let j_audio_query = env.new_string(query_json)?; @@ -211,7 +211,7 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsAccentPhr )? }; - let query_json = serde_json::to_string(&accent_phrases)?; + let query_json = serde_json::to_string(&accent_phrases).expect("should not fail"); let j_accent_phrases = env.new_string(query_json)?; @@ -241,7 +241,7 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsAccentPhr )? }; - let query_json = serde_json::to_string(&accent_phrases)?; + let query_json = serde_json::to_string(&accent_phrases).expect("should not fail"); let j_accent_phrases = env.new_string(query_json)?; @@ -259,7 +259,7 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsReplaceMo throw_if_err(env, std::ptr::null_mut(), |env| { let accent_phrases_json: String = env.get_string(&accent_phrases_json)?.into(); let accent_phrases: Vec = - serde_json::from_str(&accent_phrases_json)?; + serde_json::from_str(&accent_phrases_json).map_err(JavaApiError::DeJson)?; let style_id = style_id as u32; let internal = env @@ -273,7 +273,8 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsReplaceMo )? }; - let replaced_accent_phrases_json = serde_json::to_string(&replaced_accent_phrases)?; + let replaced_accent_phrases_json = + serde_json::to_string(&replaced_accent_phrases).expect("should not fail"); Ok(env.new_string(replaced_accent_phrases_json)?.into_raw()) }) @@ -291,7 +292,7 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsReplacePh throw_if_err(env, std::ptr::null_mut(), |env| { let accent_phrases_json: String = env.get_string(&accent_phrases_json)?.into(); let accent_phrases: Vec = - serde_json::from_str(&accent_phrases_json)?; + serde_json::from_str(&accent_phrases_json).map_err(JavaApiError::DeJson)?; let style_id = style_id as u32; let internal = env @@ -306,7 +307,8 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsReplacePh )? }; - let replaced_accent_phrases_json = serde_json::to_string(&replaced_accent_phrases)?; + let replaced_accent_phrases_json = + serde_json::to_string(&replaced_accent_phrases).expect("should not fail"); Ok(env.new_string(replaced_accent_phrases_json)?.into_raw()) }) @@ -322,7 +324,7 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsReplaceMo throw_if_err(env, std::ptr::null_mut(), |env| { let accent_phrases_json: String = env.get_string(&accent_phrases_json)?.into(); let accent_phrases: Vec = - serde_json::from_str(&accent_phrases_json)?; + serde_json::from_str(&accent_phrases_json).map_err(JavaApiError::DeJson)?; let style_id = style_id as u32; let internal = env @@ -336,7 +338,8 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsReplaceMo )? }; - let replaced_accent_phrases_json = serde_json::to_string(&replaced_accent_phrases)?; + let replaced_accent_phrases_json = + serde_json::to_string(&replaced_accent_phrases).expect("should not fail"); Ok(env.new_string(replaced_accent_phrases_json)?.into_raw()) }) @@ -352,7 +355,8 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsSynthesis ) -> jobject { throw_if_err(env, std::ptr::null_mut(), |env| { let audio_query: String = env.get_string(&query_json)?.into(); - let audio_query: voicevox_core::AudioQueryModel = serde_json::from_str(&audio_query)?; + let audio_query: voicevox_core::AudioQueryModel = + serde_json::from_str(&audio_query).map_err(JavaApiError::DeJson)?; let style_id = style_id as u32; let internal = env diff --git a/crates/voicevox_core_java_api/src/user_dict.rs b/crates/voicevox_core_java_api/src/user_dict.rs index 4f7bc78d8..e85085a34 100644 --- a/crates/voicevox_core_java_api/src/user_dict.rs +++ b/crates/voicevox_core_java_api/src/user_dict.rs @@ -1,7 +1,10 @@ use jni::objects::JClass; -use std::sync::{Arc, Mutex}; +use std::{ + borrow::Cow, + sync::{Arc, Mutex}, +}; -use crate::common::throw_if_err; +use crate::common::{throw_if_err, JavaApiError}; use jni::{ objects::{JObject, JString}, sys::jobject, @@ -34,9 +37,10 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_UserDict_rsAddWord<'loc .clone(); let word_json = env.get_string(&word_json)?; - let word_json = word_json.to_str()?; + let word_json = &Cow::from(&word_json); - let word: voicevox_core::UserDictWord = serde_json::from_str(word_json)?; + let word: voicevox_core::UserDictWord = + serde_json::from_str(word_json).map_err(JavaApiError::DeJson)?; let uuid = { let mut internal = internal.lock().unwrap(); @@ -63,11 +67,12 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_UserDict_rsUpdateWord<' .clone(); let uuid = env.get_string(&uuid)?; - let uuid = uuid.to_str()?.parse()?; + let uuid = Cow::from(&uuid).parse()?; let word_json = env.get_string(&word_json)?; - let word_json = word_json.to_str()?; + let word_json = &Cow::from(&word_json); - let word: voicevox_core::UserDictWord = serde_json::from_str(word_json)?; + let word: voicevox_core::UserDictWord = + serde_json::from_str(word_json).map_err(JavaApiError::DeJson)?; { let mut internal = internal.lock().unwrap(); @@ -90,7 +95,7 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_UserDict_rsRemoveWord<' .clone(); let uuid = env.get_string(&uuid)?; - let uuid = uuid.to_str()?.parse()?; + let uuid = Cow::from(&uuid).parse()?; { let mut internal = internal.lock().unwrap(); @@ -137,7 +142,7 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_UserDict_rsLoad<'local> .clone(); let path = env.get_string(&path)?; - let path = path.to_str()?; + let path = &Cow::from(&path); { let mut internal = internal.lock().unwrap(); @@ -160,7 +165,7 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_UserDict_rsSave<'local> .clone(); let path = env.get_string(&path)?; - let path = path.to_str()?; + let path = &Cow::from(&path); { let internal = internal.lock().unwrap(); @@ -183,7 +188,7 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_UserDict_rsGetWords<'lo let words = { let internal = internal.lock().unwrap(); - serde_json::to_string(internal.words())? + serde_json::to_string(internal.words()).expect("should not fail") }; let words = env.new_string(words)?; @@ -211,7 +216,7 @@ extern "system" fn Java_jp_hiroshiba_voicevoxcore_UserDict_rsToZenkaku<'local>( ) -> jobject { throw_if_err(env, std::ptr::null_mut(), |env| { let text = env.get_string(&text)?; - let text = text.to_str()?; + let text = &Cow::from(&text); let text = voicevox_core::__internal::to_zenkaku(text); @@ -228,7 +233,7 @@ extern "system" fn Java_jp_hiroshiba_voicevoxcore_UserDict_rsValidatePronunciati ) { throw_if_err(env, (), |env| { let text = env.get_string(&text)?; - let text = text.to_str()?; + let text = &Cow::from(&text); voicevox_core::__internal::validate_pronunciation(text)?; diff --git a/crates/voicevox_core_java_api/src/voice_model.rs b/crates/voicevox_core_java_api/src/voice_model.rs index d94904877..cd971a1f7 100644 --- a/crates/voicevox_core_java_api/src/voice_model.rs +++ b/crates/voicevox_core_java_api/src/voice_model.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::{borrow::Cow, sync::Arc}; use crate::common::{throw_if_err, RUNTIME}; use jni::{ @@ -15,7 +15,7 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_VoiceModel_rsFromPath<' ) { throw_if_err(env, (), |env| { let model_path = env.get_string(&model_path)?; - let model_path = model_path.to_str()?; + let model_path = &*Cow::from(&model_path); let internal = RUNTIME.block_on(voicevox_core::VoiceModel::from_path(model_path))?;