Skip to content

Commit

Permalink
Remove rand/strum dependencies (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjardoo committed Jun 18, 2024
1 parent de63f30 commit 40fd18e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 95 deletions.
70 changes: 0 additions & 70 deletions Cargo.lock

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

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@ tokio-util = { version = "0.7", optional = true, features = ["codec"] }
tokio-stream = { version = "0.1", optional = true }
serde = { version = "1", features = ["derive"] }
serde_json = "1.0"
rand = { version = "0.8", optional = true }
base64 = { version = "0.22", optional = true }
log = { version = "0.4", optional = true }
bytes = "1.5.0"
strum = "0.26.2"
strum_macros = "0.26.2"
derive_builder = "0.20.0"

[features]
default = ["reqwest", "tokio", "tokio-util", "reqwest/default-tls"]
download = ["dep:futures", "dep:rand", "dep:base64"]
download = ["dep:futures", "dep:base64"]
stream = ["dep:reqwest-eventsource", "dep:futures", "dep:tokio-stream"]
rustls-tls = ["reqwest/rustls-tls"]

Expand Down
26 changes: 18 additions & 8 deletions src/v1/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::v1::error::APIError;
use crate::v1::resources::audio::AudioTranscriptionBytes;
#[cfg(feature = "download")]
use rand::{distributions::Alphanumeric, Rng};
use reqwest::{multipart::Part, Response};
use serde::de::DeserializeOwned;
use serde_json::Value;
#[cfg(feature = "download")]
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::fs::File;
use tokio_util::codec::{BytesCodec, FramedRead};

Expand Down Expand Up @@ -71,11 +71,21 @@ pub async fn file_from_disk_to_form_part(path: String) -> Result<Part, APIError>

#[cfg(feature = "download")]
pub fn generate_file_name(path: &str, length: u32, file_type: &str) -> String {
let random_file_name: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(length as usize)
.map(char::from)
.collect();
const ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ";

let alphabet_len = ALPHABET.len();

let start = SystemTime::now();
let since_the_epoch = start.duration_since(UNIX_EPOCH).unwrap();
let mut seed = since_the_epoch.as_nanos();

let mut random_str = String::with_capacity(length as usize);

for _ in 0..length {
let index = (seed % alphabet_len as u128) as usize;
random_str.push(ALPHABET[index] as char);
seed /= alphabet_len as u128;
}

format!("{}/{}.{}", path, random_file_name, file_type)
format!("{}/{}.{}", path, random_str, file_type)
}
9 changes: 4 additions & 5 deletions src/v1/resources/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use bytes::Bytes;
use derive_builder::Builder;
use serde::{Deserialize, Serialize};
use std::{fmt::Display, path::Path};
use strum_macros::EnumString;

#[derive(Serialize, Deserialize, Debug, Default, Builder, Clone, PartialEq)]
#[builder(name = "AudioSpeechParametersBuilder")]
Expand Down Expand Up @@ -102,7 +101,7 @@ pub struct AudioSpeechResponseChunkResponse {
pub bytes: Bytes,
}

#[derive(Serialize, Deserialize, Debug, Clone, EnumString, PartialEq)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum AudioOutputFormat {
Json,
Expand All @@ -112,7 +111,7 @@ pub enum AudioOutputFormat {
Vtt,
}

#[derive(Serialize, Deserialize, Debug, Clone, EnumString, PartialEq)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum AudioSpeechResponseFormat {
Mp3,
Expand All @@ -135,7 +134,7 @@ pub enum AudioTranscriptionFile {
File(String),
}

#[derive(Serialize, Deserialize, Debug, Default, Clone, EnumString, PartialEq)]
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum AudioVoice {
#[default]
Expand All @@ -147,7 +146,7 @@ pub enum AudioVoice {
Shimmer,
}

#[derive(Serialize, Deserialize, Debug, Clone, EnumString, PartialEq)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum TimestampGranularity {
Word,
Expand Down
15 changes: 7 additions & 8 deletions src/v1/resources/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use derive_builder::Builder;
use futures::future;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use strum_macros::EnumString;

#[derive(Serialize, Debug, Default, Builder, Clone, PartialEq)]
#[builder(name = "CreateImageParametersBuilder")]
Expand Down Expand Up @@ -106,7 +105,7 @@ pub struct ImageResponse {
pub data: Vec<ImageData>,
}

#[derive(Serialize, Deserialize, Debug, Clone, EnumString, PartialEq)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum ImageSize {
#[serde(rename = "256x256")]
Size256X256,
Expand All @@ -120,28 +119,28 @@ pub enum ImageSize {
Size1024X1792,
}

#[derive(Serialize, Deserialize, Debug, Clone, EnumString, PartialEq)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum ImageQuality {
Standard,
Hd,
}

#[derive(Serialize, Deserialize, Debug, Clone, EnumString, PartialEq)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum ImageStyle {
Vivid,
Natural,
}

#[derive(Serialize, Deserialize, Debug, Clone, EnumString, PartialEq)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ResponseFormat {
Url,
B64Json,
}

#[derive(Serialize, Deserialize, Debug, Clone, EnumString, PartialEq)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(untagged)]
pub enum ImageData {
Url {
Expand Down Expand Up @@ -201,7 +200,7 @@ impl ImageData {
.await
.map_err(|error| APIError::FileError(error.to_string()))?;

let full_path = generate_file_name(path, 12, "png");
let full_path = generate_file_name(path, 16, "png");

tokio::fs::write(
&full_path,
Expand All @@ -222,7 +221,7 @@ impl ImageData {
b64_json: &str,
path: &str,
) -> Result<String, APIError> {
let full_path = generate_file_name(path, 12, "png");
let full_path = generate_file_name(path, 16, "png");

let bytes = general_purpose::STANDARD.decode(b64_json).unwrap();

Expand Down

0 comments on commit 40fd18e

Please sign in to comment.