Skip to content

Commit

Permalink
Use FixedString in command arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Mar 10, 2024
1 parent 9b20fab commit a365e8a
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 28 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ default-features = false

[workspace.dependencies.serenity]
git = "https://github.com/serenity-rs/serenity"
features = ["unstable_discord_api", "typesize"]
features = ["typesize"]
default-features = false
branch = "next"

Expand Down
6 changes: 3 additions & 3 deletions src/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ pub async fn fetch_translation_languages(
reqwest: &reqwest::Client,
mut tts_service: reqwest::Url,
auth_key: Option<&str>,
) -> Result<BTreeMap<FixedString, FixedString>> {
) -> Result<BTreeMap<FixedString<u8>, FixedString<u8>>> {
tts_service.set_path("translation_languages");

let raw_langs: Vec<(String, FixedString)> =
let raw_langs: Vec<(String, FixedString<u8>)> =
fetch_json(reqwest, tts_service, auth_key.unwrap_or("")).await?;

let lang_map = raw_langs.into_iter().map(|(mut lang, name)| {
Expand All @@ -88,7 +88,7 @@ pub async fn fetch_translation_languages(

pub fn prepare_gcloud_voices(
raw_map: Vec<GoogleVoice>,
) -> BTreeMap<FixedString, BTreeMap<FixedString, GoogleGender>> {
) -> BTreeMap<FixedString<u8>, BTreeMap<FixedString<u8>, GoogleGender>> {
// {lang_accent: {variant: gender}}
let mut cleaned_map = BTreeMap::new();
for gvoice in raw_map {
Expand Down
6 changes: 4 additions & 2 deletions tts_commands/src/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use anyhow::Error;
use num_format::{Locale, ToFormattedString};

use poise::{
serenity_prelude::{self as serenity, builder::*, Mentionable as _},
serenity_prelude::{
self as serenity, builder::*, small_fixed_array::FixedString, Mentionable as _,
},
CreateReply,
};

Expand Down Expand Up @@ -79,7 +81,7 @@ pub async fn tts(
ctx: Context<'_>,
#[description = "The text to TTS"]
#[rest]
message: String,
message: FixedString<u16>,
) -> CommandResult {
let is_unnecessary_command_invoke = async {
if !matches!(ctx, poise::Context::Prefix(_)) {
Expand Down
4 changes: 2 additions & 2 deletions tts_commands/src/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use std::{borrow::Cow, hash::Hash};

use self::serenity::builder::*;
use self::serenity::{builder::*, small_fixed_array::FixedString};
use num_format::{Locale, ToFormattedString};
use poise::{serenity_prelude as serenity, CreateReply};
use typesize::TypeSize;
Expand All @@ -38,7 +38,7 @@ pub async fn register(ctx: Context<'_>) -> CommandResult {
pub async fn dm(
ctx: PrefixContext<'_>,
todm: serenity::User,
#[rest] message: String,
#[rest] message: FixedString<u16>,
) -> CommandResult {
let attachment_url = ctx.msg.attachments.first().map(|a| a.url.as_str());
let (content, embed) = dm_generic(
Expand Down
16 changes: 8 additions & 8 deletions tts_commands/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ async fn change_voice<'a, T, RowT1, RowT2>(
author_id: serenity::UserId,
guild_id: serenity::GuildId,
key: T,
voice: Option<FixedString>,
voice: Option<FixedString<u8>>,
target: Target,
) -> Result<Cow<'a, str>, Error>
where
Expand Down Expand Up @@ -425,7 +425,7 @@ where
})
}

fn format_languages<'a>(mut iter: impl Iterator<Item = &'a FixedString>) -> String {
fn format_languages<'a>(mut iter: impl Iterator<Item = &'a FixedString<u8>>) -> String {
let mut buf = String::with_capacity(iter.size_hint().0 * 2);
if let Some(first_elt) = iter.next() {
buf.push('`');
Expand All @@ -441,15 +441,15 @@ fn format_languages<'a>(mut iter: impl Iterator<Item = &'a FixedString>) -> Stri
buf
}

fn get_voice_name<'a>(data: &'a Data, code: &str, mode: TTSMode) -> Option<&'a FixedString> {
fn get_voice_name<'a>(data: &'a Data, code: &str, mode: TTSMode) -> Option<&'a FixedString<u8>> {
match mode {
TTSMode::gTTS => data.gtts_voices.get(code),
TTSMode::Polly => data.polly_voices.get(code).map(|n| &n.name),
TTSMode::eSpeak | TTSMode::gCloud => None,
}
}

fn check_valid_voice(data: &Data, code: &FixedString, mode: TTSMode) -> bool {
fn check_valid_voice(data: &Data, code: &FixedString<u8>, mode: TTSMode) -> bool {
match mode {
TTSMode::gTTS | TTSMode::Polly => get_voice_name(data, code, mode).is_some(),
TTSMode::eSpeak => data.espeak_voices.contains(code),
Expand Down Expand Up @@ -829,7 +829,7 @@ pub async fn server_voice(
#[description = "The default voice to read messages in"]
#[autocomplete = "voice_autocomplete"]
#[rest]
voice: String,
voice: FixedString<u8>,
) -> CommandResult {
let data = ctx.data();
let guild_id = ctx.guild_id().unwrap();
Expand All @@ -841,7 +841,7 @@ pub async fn server_voice(
ctx.author().id,
guild_id,
guild_id.into(),
Some(voice.trunc_into()),
Some(voice),
Target::Guild,
)
.await?;
Expand Down Expand Up @@ -911,14 +911,14 @@ pub async fn command_prefix(
ctx: Context<'_>,
#[description = "The prefix to be used before commands"]
#[rest]
prefix: String,
prefix: FixedString<u8>,
) -> CommandResult {
let to_send = if let Err(err) = check_prefix(&ctx, &prefix) {
Cow::Borrowed(err)
} else {
ctx.data()
.guilds_db
.set_one(ctx.guild_id().unwrap().into(), "prefix", &prefix)
.set_one(ctx.guild_id().unwrap().into(), "prefix", prefix.as_str())
.await?;
Cow::Owned(
ctx.gettext("Command prefix for this server is now: {prefix}")
Expand Down
18 changes: 9 additions & 9 deletions tts_core/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ pub struct Data {
pub fully_started: std::sync::atomic::AtomicBool,
pub update_startup_lock: tokio::sync::Mutex<()>,

pub espeak_voices: FixedArray<FixedString>,
pub gtts_voices: BTreeMap<FixedString, FixedString>,
pub polly_voices: BTreeMap<FixedString, PollyVoice>,
pub gcloud_voices: BTreeMap<FixedString, BTreeMap<FixedString, GoogleGender>>,
pub espeak_voices: FixedArray<FixedString<u8>>,
pub gtts_voices: BTreeMap<FixedString<u8>, FixedString<u8>>,
pub polly_voices: BTreeMap<FixedString<u8>, PollyVoice>,
pub gcloud_voices: BTreeMap<FixedString<u8>, BTreeMap<FixedString<u8>, GoogleGender>>,

pub translation_languages: BTreeMap<FixedString, FixedString>,
pub translation_languages: BTreeMap<FixedString<u8>, FixedString<u8>>,
}

impl std::fmt::Debug for Data {
Expand Down Expand Up @@ -452,10 +452,10 @@ impl From<TTSModeChoice> for TTSMode {
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GoogleVoice {
pub name: FixedString,
pub name: FixedString<u8>,
#[serde(default)]
pub ssml_gender: GoogleGender,
pub language_codes: [FixedString; 1],
pub language_codes: [FixedString<u8>; 1],
}

#[derive(serde::Deserialize)]
Expand All @@ -464,8 +464,8 @@ pub struct PollyVoice {
pub language_code: FixedString,
pub language_name: FixedString,
pub gender: PollyGender,
pub name: FixedString,
pub id: FixedString,
pub name: FixedString<u8>,
pub id: FixedString<u8>,
}

#[derive(serde::Deserialize, IntoStaticStr, Copy, Clone, Default)]
Expand Down

0 comments on commit a365e8a

Please sign in to comment.