Skip to content

Commit

Permalink
不要なエラーを排除/パニック化
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip committed Oct 21, 2023
1 parent 0098be9 commit 48d47de
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 46 deletions.
21 changes: 4 additions & 17 deletions crates/voicevox_core_java_api/src/common.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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
Expand All @@ -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),
}
7 changes: 5 additions & 2 deletions crates/voicevox_core_java_api/src/open_jtalk.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::sync::{Arc, Mutex};
use std::{
borrow::Cow,
sync::{Arc, Mutex},
};

use crate::common::throw_if_err;
use jni::{
Expand Down Expand Up @@ -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))?;
Expand Down
28 changes: 16 additions & 12 deletions crates/voicevox_core_java_api/src/synthesizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;
Expand Down Expand Up @@ -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)?;

Expand Down Expand Up @@ -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)?;

Expand Down Expand Up @@ -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)?;

Expand Down Expand Up @@ -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)?;

Expand All @@ -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<voicevox_core::AccentPhraseModel> =
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
Expand All @@ -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())
})
Expand All @@ -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<voicevox_core::AccentPhraseModel> =
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
Expand All @@ -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())
})
Expand All @@ -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<voicevox_core::AccentPhraseModel> =
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
Expand All @@ -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())
})
Expand All @@ -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
Expand Down
31 changes: 18 additions & 13 deletions crates/voicevox_core_java_api/src/user_dict.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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)?;
Expand Down Expand Up @@ -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);

Expand All @@ -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)?;

Expand Down
4 changes: 2 additions & 2 deletions crates/voicevox_core_java_api/src/voice_model.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{borrow::Cow, sync::Arc};

use crate::common::{throw_if_err, RUNTIME};
use jni::{
Expand All @@ -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))?;

Expand Down

0 comments on commit 48d47de

Please sign in to comment.