elevenlabs_api/apis/
mod.rs

1use std::fmt::{self, Display, Formatter};
2
3mod requests;
4
5pub mod tts;
6
7const TEXT_TO_SPEECH: &str = "text-to-speech";
8
9pub type Json = serde_json::Value;
10pub type ApiResult<T> = Result<T, Error>;
11
12#[derive(Debug)]
13pub enum Error {
14    /// An Error returned by the API
15    ApiError(u16, String),
16    /// An Error not related to the API
17    RequestError(String),
18}
19
20impl Display for Error {
21    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
22        match self {
23            Error::ApiError(status, msg) => {
24                write!(f, "API responded with status {} error: {}", status, msg)
25            }
26            Error::RequestError(msg) => write!(f, "Request error: {}", msg),
27        }
28    }
29}
30
31impl From<ureq::Error> for Error {
32    fn from(value: ureq::Error) -> Self {
33        match value {
34            ureq::Error::Status(status, response) => {
35                let error_msg = response.into_json::<Json>().unwrap();
36                Error::ApiError(status, format!("{error_msg}"))
37            }
38            ureq::Error::Transport(e) => Error::RequestError(e.to_string()),
39        }
40    }
41}