#![allow(dead_code)]
use base64::{engine, Engine};
use serde::{Deserialize, Serialize};
use std::convert::{TryFrom, TryInto};
#[allow(non_snake_case)]
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub struct AudioQuery {
pub accent_phrases: Vec<AccentPhrase>,
pub speedScale: f64,
pub pitchScale: f64,
pub intonationScale: f64,
pub volumeScale: f64,
pub prePhonemeLength: f64,
pub postPhonemeLength: f64,
pub outputSamplingRate: i32,
pub outputStereo: bool,
pub kana: Option<String>,
}
impl Default for AudioQuery {
fn default() -> Self {
Self {
accent_phrases: Vec::new(),
speedScale: 1.0,
pitchScale: 0.0,
intonationScale: 1.0,
volumeScale: 1.0,
prePhonemeLength: 0.1,
postPhonemeLength: 0.1,
outputSamplingRate: 24000,
outputStereo: Default::default(),
kana: None,
}
}
}
#[allow(non_snake_case)]
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub struct AudioQueryInProject {
pub accentPhrases: Vec<AccentPhraseInProject>,
pub speedScale: f64,
pub pitchScale: f64,
pub intonationScale: f64,
pub volumeScale: f64,
pub prePhonemeLength: f64,
pub postPhonemeLength: f64,
pub outputSamplingRate: i32,
pub outputStereo: bool,
pub kana: Option<String>,
}
impl From<AudioQuery> for AudioQueryInProject {
fn from(aq: AudioQuery) -> Self {
Self {
accentPhrases: aq
.accent_phrases
.iter()
.map(|ap| ap.clone().into())
.collect(),
speedScale: aq.speedScale,
pitchScale: aq.pitchScale,
intonationScale: aq.intonationScale,
volumeScale: aq.volumeScale,
prePhonemeLength: aq.prePhonemeLength,
postPhonemeLength: aq.postPhonemeLength,
outputSamplingRate: aq.outputSamplingRate,
outputStereo: aq.outputStereo,
kana: aq.kana,
}
}
}
impl From<AudioQueryInProject> for AudioQuery {
fn from(aq: AudioQueryInProject) -> Self {
Self {
accent_phrases: aq
.accentPhrases
.iter()
.map(|ap| ap.clone().into())
.collect(),
speedScale: aq.speedScale,
pitchScale: aq.pitchScale,
intonationScale: aq.intonationScale,
volumeScale: aq.volumeScale,
prePhonemeLength: aq.prePhonemeLength,
postPhonemeLength: aq.postPhonemeLength,
outputSamplingRate: aq.outputSamplingRate,
outputStereo: aq.outputStereo,
kana: aq.kana,
}
}
}
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub struct AccentPhrase {
pub moras: Vec<Mora>,
pub accent: i32,
pub pause_mora: Option<Mora>,
pub is_interrogative: Option<bool>,
}
#[allow(non_snake_case)]
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub struct AccentPhraseInProject {
pub moras: Vec<MoraInProject>,
pub accent: i32,
#[serde(skip_serializing_if = "Option::is_none")]
pub pauseMora: Option<MoraInProject>,
#[serde(skip_serializing_if = "Option::is_none")]
pub isInterrogative: Option<bool>,
}
impl From<AccentPhrase> for AccentPhraseInProject {
fn from(ap: AccentPhrase) -> Self {
Self {
moras: ap.moras.iter().map(|mora| mora.clone().into()).collect(),
accent: ap.accent,
pauseMora: ap.pause_mora.map(|mora| mora.clone().into()),
isInterrogative: ap.is_interrogative,
}
}
}
impl From<AccentPhraseInProject> for AccentPhrase {
fn from(ap: AccentPhraseInProject) -> Self {
Self {
moras: ap.moras.iter().map(|mora| mora.clone().into()).collect(),
accent: ap.accent,
pause_mora: ap.pauseMora.map(|mora| mora.clone().into()),
is_interrogative: ap.isInterrogative,
}
}
}
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub struct Mora {
pub text: String,
pub consonant: Option<String>,
pub consonant_length: Option<f64>,
pub vowel: String,
pub vowel_length: f64,
pub pitch: f64,
}
#[allow(non_snake_case)]
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub struct MoraInProject {
pub text: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub consonant: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub consonantLength: Option<f64>,
pub vowel: String,
pub vowelLength: f64,
pub pitch: f64,
}
impl From<Mora> for MoraInProject {
fn from(mora: Mora) -> Self {
Self {
text: mora.text,
consonant: mora.consonant,
consonantLength: mora.consonant_length,
vowel: mora.vowel,
vowelLength: mora.vowel_length,
pitch: mora.pitch,
}
}
}
impl From<MoraInProject> for Mora {
fn from(mora: MoraInProject) -> Self {
Self {
text: mora.text.clone(),
consonant: mora.consonant.clone(),
consonant_length: mora.consonantLength,
vowel: mora.vowel.clone(),
vowel_length: mora.vowelLength,
pitch: mora.pitch,
}
}
}
#[allow(non_snake_case)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct HttpValidationError {
pub detail: Vec<ValidationError>,
}
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct ValidationError {
pub loc: Vec<String>,
pub msg: String,
#[serde(rename = "type")]
pub _type: String,
}
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct AccentPhrasesResponse {
pub accent_phrases: Vec<AccentPhrase>,
}
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct ParseKanaBadRequest {
pub text: String,
pub error_name: String,
pub error_args: String,
}
#[derive(Debug, Clone)]
pub enum ErrorName {
UnknownText,
AccentTop,
AccentTwice,
AccentNotFound,
EmptyPhrase,
InterrogationMarkNotAtEnd,
InfiniteLoop,
}
#[allow(non_snake_case, unused_variables)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Preset {
pub id: i32,
pub name: String,
pub speaker_uuid: String,
pub style_id: i32,
pub speedScale: f64,
pub pitchScale: f64,
pub intonationScale: f64,
pub volumeScale: f64,
pub prePhonemeLength: f64,
pub postPhonemeLength: f64,
}
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Speaker {
pub name: String,
pub speaker_uuid: String,
pub styles: Vec<SpeakerStyle>,
pub version: Option<String>,
}
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct SpeakerStyle {
pub name: String,
pub id: i32,
}
#[derive(Deserialize, Debug, Serialize, Clone)]
pub struct StyleInfoRaw {
pub(crate) id: i32,
pub(crate) icon: String,
pub(crate) voice_samples: Vec<String>,
}
impl TryFrom<StyleInfoRaw> for StyleInfo {
type Error = TryFromRawError;
fn try_from(raw: StyleInfoRaw) -> Result<Self, <Self as TryFrom<StyleInfoRaw>>::Error> {
Ok(Self {
id: raw.id,
icon: engine::general_purpose::STANDARD
.decode(raw.icon)
.map_err(|_| TryFromRawError::Base64Decode)?,
voice_samples: raw
.voice_samples
.iter()
.filter_map(|b64| engine::general_purpose::STANDARD.decode(b64).ok())
.collect(),
})
}
}
impl From<StyleInfo> for StyleInfoRaw {
fn from(mut si: StyleInfo) -> Self {
Self {
id: si.id,
icon: engine::general_purpose::STANDARD.encode(si.icon),
voice_samples: si
.voice_samples
.drain(..)
.map(|bytes| engine::general_purpose::STANDARD.encode(bytes))
.collect(),
}
}
}
#[derive(Debug, Clone)]
pub struct SpeakerInfo {
pub policy: String,
pub portrait: Vec<u8>,
pub style_infos: Vec<StyleInfo>,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct SpeakerInfoRaw {
pub(crate) policy: String,
pub(crate) portrait: String,
pub(crate) style_infos: Vec<StyleInfoRaw>,
}
pub enum TryFromRawError {
Base64Decode,
}
impl TryFrom<SpeakerInfoRaw> for SpeakerInfo {
type Error = TryFromRawError;
fn try_from(mut raw: SpeakerInfoRaw) -> Result<Self, Self::Error> {
Ok(Self {
policy: raw.policy,
portrait: engine::general_purpose::STANDARD
.decode(raw.portrait)
.map_err(|_| TryFromRawError::Base64Decode)?,
style_infos: raw
.style_infos
.drain(..)
.filter_map(|si_raw| si_raw.try_into().ok())
.collect(),
})
}
}
impl From<SpeakerInfo> for SpeakerInfoRaw {
fn from(mut si: SpeakerInfo) -> Self {
Self {
policy: si.policy,
portrait: engine::general_purpose::STANDARD.encode(&si.portrait),
style_infos: si.style_infos.drain(..).map(|si| si.into()).collect(),
}
}
}
#[derive(Debug, Clone)]
pub struct StyleInfo {
pub id: i32,
pub icon: Vec<u8>,
pub voice_samples: Vec<Vec<u8>>,
}
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct SupportedDevices {
pub cpu: bool,
pub cuda: bool,
pub dml: Option<bool>,
}
#[derive(Debug, Clone)]
pub struct DownloadableLibraries {
pub download_url: String,
pub bytes: usize,
pub speaker: Speaker,
pub speaker_info: SpeakerInfo,
}
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct DownloadableLibrariesRaw {
download_url: String,
bytes: usize,
speaker: Speaker,
speaker_info: SpeakerInfoRaw,
}
impl TryFrom<DownloadableLibrariesRaw> for DownloadableLibraries {
type Error = TryFromRawError;
fn try_from(value: DownloadableLibrariesRaw) -> Result<Self, Self::Error> {
let speaker_info = value.speaker_info.try_into()?;
Ok(Self {
download_url: value.download_url,
bytes: value.bytes,
speaker: value.speaker,
speaker_info,
})
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct EngineManifestRaw {
manifest_version: String,
name: String,
uuid: String,
url: String,
icon: String,
default_sampling_rate: i32,
term_of_service: String,
update_infos: Vec<UpdateInfo>,
dependency_licenses: Vec<LicenseInfo>,
downloadable_libraries_path: String,
downloadable_libraries_url: String,
}
#[derive(Debug, Clone)]
pub struct EngineManifest {
pub manifest_version: String,
pub name: String,
pub uuid: String,
pub url: String,
pub icon: Vec<u8>,
pub default_sampling_rate: i32,
pub term_of_service: String,
pub update_infos: Vec<UpdateInfo>,
pub dependency_licenses: Vec<LicenseInfo>,
pub downloadable_libraries_path: String,
pub downloadable_libraries_url: String,
}
impl TryFrom<EngineManifestRaw> for EngineManifest {
type Error = TryFromRawError;
fn try_from(raw: EngineManifestRaw) -> Result<Self, Self::Error> {
Ok(Self {
manifest_version: raw.manifest_version,
name: raw.name,
uuid: raw.uuid,
url: raw.url,
icon: engine::general_purpose::STANDARD
.decode(raw.icon)
.map_err(|_| TryFromRawError::Base64Decode)?,
default_sampling_rate: raw.default_sampling_rate,
term_of_service: raw.term_of_service,
update_infos: raw.update_infos,
dependency_licenses: raw.dependency_licenses,
downloadable_libraries_path: raw.downloadable_libraries_path,
downloadable_libraries_url: raw.downloadable_libraries_url,
})
}
}
impl From<EngineManifest> for EngineManifestRaw {
fn from(raw: EngineManifest) -> Self {
Self {
manifest_version: raw.manifest_version,
name: raw.name,
uuid: raw.uuid,
url: raw.url,
icon: engine::general_purpose::STANDARD.encode(raw.icon),
default_sampling_rate: raw.default_sampling_rate,
term_of_service: raw.term_of_service,
update_infos: raw.update_infos,
dependency_licenses: raw.dependency_licenses,
downloadable_libraries_path: raw.downloadable_libraries_path,
downloadable_libraries_url: raw.downloadable_libraries_url,
}
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct UpdateInfo {
version: String,
descriptions: Vec<String>,
contributers: Vec<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct LicenseInfo {
name: String,
version: String,
license: String,
text: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct UserDictWord {
surface: String,
priority: i32,
context_id: Option<i32>,
part_of_speech: String,
part_of_speech_detail_1: String,
part_of_speech_detail_2: String,
part_of_speech_detail_3: String,
inflectional_type: String,
inflectional_form: String,
stem: String,
yomi: String,
pronunciation: String,
accent_type: i32,
mora_count: Option<i32>,
accent_associative_rule: String,
}
#[derive(Clone, Copy, Debug)]
pub enum WordType {
ProperNoun,
CommonNoun,
Verb,
Adjective,
Suffix,
}
impl ToString for WordType {
fn to_string(&self) -> String {
match self {
WordType::ProperNoun => "PROPER_NOUN",
WordType::CommonNoun => "COMMON_NOUN",
WordType::Verb => "VERB",
WordType::Adjective => "ADJECTIVE",
WordType::Suffix => "SUFFIX",
}
.to_owned()
}
}
impl Serialize for WordType {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.to_string())
}
}