diff --git a/crates/voicevox_core/src/__internal/interop.rs b/crates/voicevox_core/src/__internal/interop.rs index fce2ad0ed..0085a3222 100644 --- a/crates/voicevox_core/src/__internal/interop.rs +++ b/crates/voicevox_core/src/__internal/interop.rs @@ -1,6 +1,7 @@ pub mod raii; pub use crate::{ + convert::ToJsonValue, metas::merge as merge_metas, synthesizer::{ blocking::PerformInference, DEFAULT_CPU_NUM_THREADS, DEFAULT_ENABLE_INTERROGATIVE_UPSPEAK, diff --git a/crates/voicevox_core/src/convert.rs b/crates/voicevox_core/src/convert.rs new file mode 100644 index 000000000..d7851c724 --- /dev/null +++ b/crates/voicevox_core/src/convert.rs @@ -0,0 +1,7 @@ +pub trait ToJsonValue { + fn to_json_value(&self) -> serde_json::Value; + + fn to_json(&self) -> String { + self.to_json_value().to_string() + } +} diff --git a/crates/voicevox_core/src/devices.rs b/crates/voicevox_core/src/devices.rs index cdf59f872..4b49456be 100644 --- a/crates/voicevox_core/src/devices.rs +++ b/crates/voicevox_core/src/devices.rs @@ -7,6 +7,8 @@ use std::{ use derive_more::BitAnd; use serde::Serialize; +use crate::convert::ToJsonValue; + pub(crate) fn test_gpus( gpus: impl IntoIterator, inference_rt_name: &'static str, @@ -124,8 +126,10 @@ impl SupportedDevices { } else { panic!("either `load-onnxruntime` or `link-onnxruntime` must be enabled"); }; +} - pub fn to_json(self) -> serde_json::Value { +impl ToJsonValue for SupportedDevices { + fn to_json_value(&self) -> serde_json::Value { serde_json::to_value(self).expect("should not fail") } } diff --git a/crates/voicevox_core/src/lib.rs b/crates/voicevox_core/src/lib.rs index 2495d685a..14beee6f1 100644 --- a/crates/voicevox_core/src/lib.rs +++ b/crates/voicevox_core/src/lib.rs @@ -49,6 +49,7 @@ const _: () = { }; mod asyncs; +mod convert; mod core; mod devices; /// cbindgen:ignore diff --git a/crates/voicevox_core_c_api/src/compatible_engine.rs b/crates/voicevox_core_c_api/src/compatible_engine.rs index 793a224c8..eef591a10 100644 --- a/crates/voicevox_core_c_api/src/compatible_engine.rs +++ b/crates/voicevox_core_c_api/src/compatible_engine.rs @@ -8,7 +8,10 @@ use std::{ use libc::c_int; use tracing::warn; -use voicevox_core::{StyleId, VoiceModelId, __internal::interop::PerformInference as _}; +use voicevox_core::{ + StyleId, VoiceModelId, + __internal::interop::{PerformInference as _, ToJsonValue as _}, +}; use crate::{helpers::display_error, init_logger_once}; @@ -218,16 +221,8 @@ pub extern "C" fn supported_devices() -> *const c_char { init_logger_once(); return SUPPORTED_DEVICES.as_ptr(); - static SUPPORTED_DEVICES: LazyLock = LazyLock::new(|| { - CString::new( - ONNXRUNTIME - .supported_devices() - .unwrap() - .to_json() - .to_string(), - ) - .unwrap() - }); + static SUPPORTED_DEVICES: LazyLock = + LazyLock::new(|| CString::new(ONNXRUNTIME.supported_devices().unwrap().to_json()).unwrap()); } /// # Safety diff --git a/crates/voicevox_core_c_api/src/lib.rs b/crates/voicevox_core_c_api/src/lib.rs index 3019929cc..6c00ee218 100644 --- a/crates/voicevox_core_c_api/src/lib.rs +++ b/crates/voicevox_core_c_api/src/lib.rs @@ -38,6 +38,7 @@ use std::sync::Once; use tracing_subscriber::fmt::format::Writer; use tracing_subscriber::EnvFilter; use uuid::Uuid; +use voicevox_core::__internal::interop::ToJsonValue as _; use voicevox_core::{AccentPhrase, AudioQuery, StyleId, UserDictWord}; fn init_logger_once() { @@ -703,8 +704,7 @@ pub unsafe extern "C" fn voicevox_onnxruntime_create_supported_devices_json( ) -> VoicevoxResultCode { init_logger_once(); into_result_code_with_error((|| { - let supported_devices = - CString::new(onnxruntime.0.supported_devices()?.to_json().to_string()).unwrap(); + let supported_devices = CString::new(onnxruntime.0.supported_devices()?.to_json()).unwrap(); output_supported_devices_json.write_unaligned( C_STRING_DROP_CHECKER .whitelist(supported_devices) diff --git a/crates/voicevox_core_java_api/src/onnxruntime.rs b/crates/voicevox_core_java_api/src/onnxruntime.rs index 2297fcdb6..3f1dfb5bd 100644 --- a/crates/voicevox_core_java_api/src/onnxruntime.rs +++ b/crates/voicevox_core_java_api/src/onnxruntime.rs @@ -6,6 +6,7 @@ use jni::{ sys::jobject, JNIEnv, }; +use voicevox_core::__internal::interop::ToJsonValue as _; use crate::{common::throw_if_err, object}; @@ -56,7 +57,7 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_blocking_Onnxruntime_rs )?; let devices = this.supported_devices()?; - assert!(match devices.to_json() { + assert!(match devices.to_json_value() { serde_json::Value::Object(o) => o.len() == 3, // `cpu`, `cuda`, `dml` _ => false, }); diff --git a/crates/voicevox_core_python_api/src/convert.rs b/crates/voicevox_core_python_api/src/convert.rs index b71fc7468..ecbaf6a54 100644 --- a/crates/voicevox_core_python_api/src/convert.rs +++ b/crates/voicevox_core_python_api/src/convert.rs @@ -13,6 +13,7 @@ use serde_json::json; use uuid::Uuid; use voicevox_core::{ AccelerationMode, AccentPhrase, AudioQuery, StyleId, SupportedDevices, VoiceModelMeta, + __internal::interop::ToJsonValue as _, }; use crate::{ @@ -280,7 +281,7 @@ pub(crate) impl voicevox_core::Result { #[ext(SupportedDevicesExt)] impl SupportedDevices { pub(crate) fn to_py(self, py: Python<'_>) -> PyResult<&PyAny> { - assert!(match self.to_json() { + assert!(match self.to_json_value() { serde_json::Value::Object(o) => o.len() == 3, // `cpu`, `cuda`, `dml` _ => false, });