Skip to content

Commit

Permalink
feat!: #[non_exhaustive] (VOICEVOX#941)
Browse files Browse the repository at this point in the history
目的としては将来の後方互換性の確保のため。

いくつかのenumだけこのようにする。

```rs
#[expect(
    clippy::manual_non_exhaustive,
    reason = "バインディングを作るときはexhaustiveとして扱いたい"
)]
pub enum … {
    …,

    #[doc(hidden)]
    __NonExhaustive,
}
```

PythonとJavaの列挙型も同様の後方互換性を抱えているはずなので、要対応。

See-also: VOICEVOX#941 (comment)
  • Loading branch information
qryxip authored Jan 25, 2025
1 parent ee985ff commit 0cfbf94
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/voicevox_core/src/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ fn test_gpu(
/// # }
/// ```
#[derive(Clone, Copy, PartialEq, Eq, Debug, BitAnd, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SupportedDevices {
/// CPUが利用可能。
///
Expand Down
3 changes: 3 additions & 0 deletions crates/voicevox_core/src/engine/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use serde::{de, Deserialize, Deserializer, Serialize, Serializer};

/// モーラ(子音+母音)ごとの情報。
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[non_exhaustive]
pub struct Mora {
/// 文字。
pub text: String,
Expand All @@ -24,6 +25,7 @@ pub struct Mora {

/// AccentPhrase (アクセント句ごとの情報)。
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[non_exhaustive]
pub struct AccentPhrase {
/// モーラの配列。
pub moras: Vec<Mora>,
Expand All @@ -48,6 +50,7 @@ impl AccentPhrase {

/// AudioQuery (音声合成用のクエリ)。
#[derive(Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub struct AudioQuery {
/// アクセント句の配列。
pub accent_phrases: Vec<AccentPhrase>,
Expand Down
6 changes: 6 additions & 0 deletions crates/voicevox_core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ pub(crate) enum ErrorRepr {
}

/// エラーの種類。
#[expect(
clippy::manual_non_exhaustive,
reason = "バインディングを作るときはexhaustiveとして扱いたい"
)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum ErrorKind {
/// open_jtalk辞書ファイルが読み込まれていない。
Expand Down Expand Up @@ -165,6 +169,8 @@ pub enum ErrorKind {
UseUserDict,
/// ユーザー辞書の単語のバリデーションに失敗した。
InvalidWord,
#[doc(hidden)]
__NonExhaustive,
}

pub(crate) type LoadModelResult<T> = std::result::Result<T, LoadModelError>;
Expand Down
3 changes: 3 additions & 0 deletions crates/voicevox_core/src/metas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub type VoiceModelMeta = Vec<SpeakerMeta>;

/// **話者**(_speaker_)のメタ情報。
#[derive(Deserialize, Serialize, Clone)]
#[non_exhaustive]
pub struct SpeakerMeta {
/// 話者名。
pub name: String,
Expand Down Expand Up @@ -141,6 +142,7 @@ impl SpeakerMeta {

/// **スタイル**(_style_)のメタ情報。
#[derive(Deserialize, Serialize, Clone)]
#[non_exhaustive]
pub struct StyleMeta {
/// スタイルID。
pub id: StyleId,
Expand Down Expand Up @@ -172,6 +174,7 @@ pub struct StyleMeta {
)]
#[strum(serialize_all = "snake_case")]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum StyleType {
/// 音声合成クエリの作成と音声合成が可能。
#[default]
Expand Down
7 changes: 7 additions & 0 deletions crates/voicevox_core/src/synthesizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ impl Default for TtsOptions {
}

/// ハードウェアアクセラレーションモードを設定する設定値。
#[expect(
clippy::manual_non_exhaustive,
reason = "バインディングを作るときはexhaustiveとして扱いたい"
)]
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq)]
pub enum AccelerationMode {
/// 実行環境に合った適切なハードウェアアクセラレーションモードを選択する。
Expand All @@ -90,6 +94,8 @@ pub enum AccelerationMode {
Cpu,
/// ハードウェアアクセラレーションモードを"GPU"に設定する。
Gpu,
#[doc(hidden)]
__NonExhaustive,
}

struct InitializeOptions {
Expand Down Expand Up @@ -251,6 +257,7 @@ impl<T, A: AsyncExt> Inner<T, A> {
[gpu, ..] => DeviceSpec::Gpu(gpu),
}
}
AccelerationMode::__NonExhaustive => unreachable!(),
};

info!("{device_for_heavy}を利用します");
Expand Down
2 changes: 2 additions & 0 deletions crates/voicevox_core/src/user_dict/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ pub enum UserDictWordType {
Adjective,
/// 接尾辞。
Suffix,
#[doc(hidden)]
__NonExhaustive,
}

impl UserDictWord {
Expand Down
3 changes: 3 additions & 0 deletions crates/voicevox_core_c_api/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub(crate) fn into_result_code_with_error(result: CApiResult<()>) -> VoicevoxRes
WordNotFound => VOICEVOX_RESULT_USER_DICT_WORD_NOT_FOUND_ERROR,
UseUserDict => VOICEVOX_RESULT_USE_USER_DICT_ERROR,
InvalidWord => VOICEVOX_RESULT_INVALID_USER_DICT_WORD_ERROR,
__NonExhaustive => unreachable!(),
},
Err(InvalidUtf8Input) => VOICEVOX_RESULT_INVALID_UTF8_INPUT_ERROR,
Err(InvalidAudioQuery(_)) => VOICEVOX_RESULT_INVALID_AUDIO_QUERY_ERROR,
Expand Down Expand Up @@ -99,6 +100,7 @@ impl From<voicevox_core::AccelerationMode> for VoicevoxAccelerationMode {
Auto => Self::VOICEVOX_ACCELERATION_MODE_AUTO,
Cpu => Self::VOICEVOX_ACCELERATION_MODE_CPU,
Gpu => Self::VOICEVOX_ACCELERATION_MODE_GPU,
__NonExhaustive => unreachable!(),
}
}
}
Expand Down Expand Up @@ -186,6 +188,7 @@ impl From<voicevox_core::UserDictWordType> for VoicevoxUserDictWordType {
Self::VOICEVOX_USER_DICT_WORD_TYPE_ADJECTIVE
}
voicevox_core::UserDictWordType::Suffix => Self::VOICEVOX_USER_DICT_WORD_TYPE_SUFFIX,
voicevox_core::UserDictWordType::__NonExhaustive => unreachable!(),
}
}
}
1 change: 1 addition & 0 deletions crates/voicevox_core_java_api/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ where
"Exception",
),
)*
voicevox_core::ErrorKind::__NonExhaustive => unreachable!(),
}
};
}
Expand Down
1 change: 1 addition & 0 deletions crates/voicevox_core_python_api/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ pub(crate) impl<T> voicevox_core::Result<T> {
ErrorKind::WordNotFound => WordNotFoundError::new_err(msg),
ErrorKind::UseUserDict => UseUserDictError::new_err(msg),
ErrorKind::InvalidWord => InvalidWordError::new_err(msg),
ErrorKind::__NonExhaustive => unreachable!(),
};

[top]
Expand Down

0 comments on commit 0cfbf94

Please sign in to comment.