elevenlabs_rs 0.3.2

A lib crate for ElevenLabs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//! The pronunciation dictionaries endpoints.
//!
//! See examples [here](https://www.github.com/rwxbytes/elevenlabs_rs/tree/main/examples/pronunciation_dictionaries).
#[allow(dead_code)]
use super::*;

const PRONUNCIATION_PATH: &str = "/v1/pronunciation-dictionaries";
const ADD_FROM_FILE_PATH: &str = "/add-from-file";
const ADD_RULES_PATH: &str = "/add-rules";
const REMOVE_RULES_PATH: &str = "/remove-rules";

const CURSOR_QUERY: &str = "cursor";
const PAGE_SIZE_QUERY: &str = "page_size";

/// Creates a new pronunciation dictionary from a lexicon .PLS file
///
/// # Example
/// ```no_run
/// use elevenlabs_rs::*;
/// use elevenlabs_rs::endpoints::pronunciation::*;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///    let c = ElevenLabsClient::default()?;
///    let body = AddFromFileBody::new("acronyms.pls", "acronyms");
///    let resp = c.hit(AddFromFile::new(body)).await?;
///    println!("{:?}", resp);
///   Ok(())
/// }
/// ```
#[derive(Clone, Debug)]
pub struct AddFromFile(AddFromFileBody);

impl AddFromFile {
    pub fn new(body: AddFromFileBody) -> Self {
        Self(body)
    }
}

/// Add from file body
///
/// # Example
/// ```no_run
/// use elevenlabs_rs::endpoints::pronunciation::*;
///
/// let mut body = AddFromFileBody::new("acronyms.pls", "acronyms")
///     .with_description("A list of acronyms")
///     .with_workspace_access("editor");
/// ```
#[derive(Clone, Debug)]
pub struct AddFromFileBody {
    file: String,
    name: String,
    description: Option<String>,
    workspace_access: Option<String>,
}

impl AddFromFileBody {
    pub fn new(file: &str, name: &str) -> Self {
        Self {
            file: file.to_string(),
            name: name.to_string(),
            description: None,
            workspace_access: None,
        }
    }

    pub fn with_description(mut self, description: &str) -> Self {
        self.description = Some(description.to_string());
        self
    }

    pub fn with_workspace_access(mut self, workspace_access: &str) -> Self {
        self.workspace_access = Some(workspace_access.to_string());
        self
    }

    fn to_form(&self) -> Result<Form> {
        let mut form = Form::new();
        let file = std::fs::read(self.file.clone())?;
        form = form.part("file", Part::bytes(file).file_name("file"));
        form = form.text("name", self.name.clone());
        if let Some(description) = &self.description {
            form = form.text("description", description.clone());
        }
        if let Some(workspace_access) = &self.workspace_access {
            form = form.text("workspace_access", workspace_access.clone());
        }
        Ok(form)
    }
}

impl Endpoint for AddFromFile {
    type ResponseBody = AddFromFileResponse;

    fn method(&self) -> Method {
        Method::POST
    }

    fn request_body(&self) -> Result<RequestBody> {
        Ok(RequestBody::Multipart(self.0.to_form()?))
    }

    async fn response_body(self, resp: Response) -> Result<Self::ResponseBody> {
        Ok(resp.json().await?)
    }

    fn url(&self) -> Url {
        let mut url = BASE_URL.parse::<Url>().unwrap();
        url.set_path(&format!("{}{}", PRONUNCIATION_PATH, ADD_FROM_FILE_PATH));
        url
    }
}

/// Add from file response
#[derive(Clone, Debug, Deserialize)]
pub struct AddFromFileResponse {
    id: String,
    name: String,
    created_by: String,
    creation_time_unix: i64,
    version_id: String,
    description: Option<String>,
}

impl AddFromFileResponse {
    pub fn id(&self) -> &str {
        &self.id
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn created_by(&self) -> &str {
        &self.created_by
    }

    pub fn creation_time_unix(&self) -> i64 {
        self.creation_time_unix
    }

    pub fn version_id(&self) -> &str {
        &self.version_id
    }

    pub fn description(&self) -> Option<&str> {
        self.description.as_deref()
    }
}

/// Add pronunciation rules to a pronunciation dictionary
///
/// # Example
/// ```no_run
/// use elevenlabs_rs::*;
/// use elevenlabs_rs::endpoints::pronunciation::*;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///   let c = ElevenLabsClient::default()?;
///   let rules = vec![
///      Rule::new_alias("TTS", "text to speech"),
///      Rule::new_alias("API", "application programming interface"),
///
///   ];
///   let resp = c.hit(AddRules::new("dictionary_id", rules)).await?;
///   println!("{:?}", resp);
///   Ok(())
/// }
/// ```
#[derive(Clone, Debug)]
pub struct AddRules {
    param: DictionaryID,
    body: AddRulesBody,
}

impl AddRules {
    pub fn new(dictionary_id: &str, rules: Vec<Rule>) -> Self {
        let body = AddRulesBody::new(rules);
        Self {
            param: DictionaryID(dictionary_id.to_string()),
            body,
        }
    }
}

/// This struct is used to build the request body for the AddRules endpoint.
#[derive(Clone, Debug, Serialize)]
struct AddRulesBody {
    rules: Vec<Rule>,
}

impl AddRulesBody {
    fn new(rules: Vec<Rule>) -> Self {
        Self { rules }
    }
}

/// This enum represents the rules that can be added to a pronunciation dictionary.
#[derive(Clone, Debug, Serialize)]
#[serde(untagged)]
pub enum Rule {
    Alias {
        r#type: String,
        string_to_replace: String,
        alias: String,
    },
    Phoneme {
        r#type: String,
        string_to_replace: String,
        phoneme: String,
        alphabet: String,
    },
}

impl Rule {
    pub fn new_alias(string_to_replace: &str, alias: &str) -> Self {
        Self::Alias {
            r#type: "alias".to_string(),
            string_to_replace: string_to_replace.to_string(),
            alias: alias.to_string(),
        }
    }
    pub fn new_phoneme(string_to_replace: &str, phoneme: &str, alphabet: &str) -> Self {
        Self::Phoneme {
            r#type: "phoneme".to_string(),
            string_to_replace: string_to_replace.to_string(),
            phoneme: phoneme.to_string(),
            alphabet: alphabet.to_string(),
        }
    }
}

impl Endpoint for AddRules {
    type ResponseBody = RulesResponse;

    fn method(&self) -> Method {
        Method::POST
    }

    fn request_body(&self) -> Result<RequestBody> {
        Ok(RequestBody::Json(serde_json::to_value(&self.body)?))
    }

    async fn response_body(self, resp: Response) -> Result<Self::ResponseBody> {
        Ok(resp.json().await?)
    }

    fn url(&self) -> Url {
        let mut url = BASE_URL.parse::<Url>().unwrap();
        url.set_path(&format!(
            "{}/{}{}",
            PRONUNCIATION_PATH, self.param.0, ADD_RULES_PATH
        ));
        url
    }
}

/// Add rules response
#[derive(Clone, Debug, Deserialize)]
pub struct RulesResponse {
    id: String,
    version_id: String,
}

impl RulesResponse {
    pub fn id(&self) -> &str {
        &self.id
    }

    pub fn version_id(&self) -> &str {
        &self.version_id
    }
}

/// Get the list of pronunciation dictionaries
///
/// This endpoint returns the list of pronunciation dictionaries.
///
/// # Example
/// ```no_run
/// use elevenlabs_rs::*;
/// use elevenlabs_rs::endpoints::pronunciation::*;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///    let c = ElevenLabsClient::default()?;
///    let resp = c.hit(GetDictionaries::new()).await?;
///    println!("{:?}", resp);
///   Ok(())
/// }
/// ```
#[derive(Clone, Debug)]
pub struct GetDictionaries(pub GetDictionariesQuery);

impl GetDictionaries {
    pub fn new() -> Self {
        Self(GetDictionariesQuery::default())
    }

    pub fn with_query(query: GetDictionariesQuery) -> Self {
        Self(query)
    }
}

/// Get dictionaries query
///
/// This struct is used to build the query parameters for the GetDictionaries endpoint.
#[derive(Clone, Debug, Default)]
pub struct GetDictionariesQuery {
    pub page_size: Option<String>,
    pub cursor: Option<String>,
}

impl GetDictionariesQuery {
    pub fn with_page_size(mut self, page_size: i32) -> Self {
        self.page_size = Some(format!("{}={}", PAGE_SIZE_QUERY, page_size));
        self
    }

    pub fn with_cursor(mut self, cursor: &str) -> Self {
        self.cursor = Some(format!("{}={}", CURSOR_QUERY, cursor));
        self
    }

    fn to_string(&self) -> String {
        let mut query = String::new();

        if let Some(page_size) = &self.page_size {
            query.push_str(page_size);
        }
        if let Some(cursor) = &self.cursor {
            if !query.is_empty() {
                query.push('&');
            }
            query.push_str(cursor);
        }
        query
    }
}

impl Endpoint for GetDictionaries {
    type ResponseBody = GetDictionariesResponse;

    fn method(&self) -> Method {
        Method::GET
    }

    async fn response_body(self, resp: Response) -> Result<Self::ResponseBody> {
        Ok(resp.json().await?)
    }

    fn url(&self) -> Url {
        let mut url = BASE_URL.parse::<Url>().unwrap();
        url.set_path(PRONUNCIATION_PATH);
        if !self.0.to_string().is_empty() {
            url.set_query(Some(&self.0.to_string()));
        }
        url
    }
}

/// Get dictionaries response
#[derive(Clone, Debug, Deserialize)]
pub struct GetDictionariesResponse {
    pronunciation_dictionaries: Vec<PronunciationDictionary>,
    next_cursor: Option<String>,
    has_more: bool,
}

impl GetDictionariesResponse {
    pub fn pronunciation_dictionaries(&self) -> &Vec<PronunciationDictionary> {
        &self.pronunciation_dictionaries
    }

    pub fn next_cursor(&self) -> Option<&str> {
        self.next_cursor.as_deref()
    }

    pub fn has_more(&self) -> bool {
        self.has_more
    }
}

/// Pronunciation dictionary
#[derive(Clone, Debug, Deserialize)]
pub struct PronunciationDictionary {
    id: String,
    latest_version_id: String,
    name: String,
    created_by: String,
    creation_time_unix: i64,
    description: Option<String>,
}

impl PronunciationDictionary {
    pub fn id(&self) -> &str {
        &self.id
    }

    pub fn latest_version_id(&self) -> &str {
        &self.latest_version_id
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn created_by(&self) -> &str {
        &self.created_by
    }

    pub fn creation_time_unix(&self) -> i64 {
        self.creation_time_unix
    }

    pub fn description(&self) -> Option<&str> {
        self.description.as_deref()
    }
}

/// This endpoint returns the pronunciation dictionary with the specified ID.
///
/// # Example
/// ```no_run
/// use elevenlabs_rs::*;
/// use elevenlabs_rs::endpoints::pronunciation::*;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///   let c = ElevenLabsClient::default()?;
///   let resp = c.hit(GetDictionary::new("dictionary_id")).await?;
///   println!("{:?}", resp);
///  Ok(())
/// }
/// ```
#[derive(Clone, Debug)]
pub struct GetDictionary(DictionaryID);

impl GetDictionary {
    pub fn new(id: &str) -> Self {
        Self(DictionaryID::from(id.to_string()))
    }
}

#[derive(Clone, Debug)]
struct DictionaryID(String);

impl From<String> for DictionaryID {
    fn from(id: String) -> Self {
        Self(id)
    }

}

impl Endpoint for GetDictionary {
    type ResponseBody = PronunciationDictionary;

    fn method(&self) -> Method {
        Method::GET
    }

    async fn response_body(self, resp: Response) -> Result<Self::ResponseBody> {
        Ok(resp.json().await?)
    }

    fn url(&self) -> Url {
        let mut url = BASE_URL.parse::<Url>().unwrap();
        url.set_path(&format!("{}/{}", PRONUNCIATION_PATH, self.0 .0));
        url
    }
}

/// This endpoint returns the PLS file with the pronunciation dictionary version rules.
///
/// # Example
/// ```no_run
/// use elevenlabs_rs::*;
/// use elevenlabs_rs::endpoints::pronunciation::*;
/// use elevenlabs_rs::utils::save;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///   let c = ElevenLabsClient::default()?;
///   let resp_bytes = c.hit(DownloadVersionByID::new("dictionary_id", "version_id")).await?;
///   save("dictionary_rules.pls", resp_bytes)?;
///   Ok(())
/// }
/// ```
#[derive(Clone, Debug)]
pub struct DownloadVersionByID {
    dictionary_id: DictionaryID,
    version_id: VersionID,
}

impl DownloadVersionByID {
    pub fn new(dictionary_id: &str, version_id: &str) -> Self {
        Self {
            dictionary_id: DictionaryID::from(dictionary_id.to_string()),
            version_id: VersionID::from(version_id.to_string()),
        }
    }
}

/// This struct represents the version ID of a pronunciation dictionary.
#[derive(Clone, Debug)]
struct VersionID(String);

impl From<String> for VersionID {
    fn from(id: String) -> Self {
        Self(id)
    }
}

impl Endpoint for DownloadVersionByID {
    type ResponseBody = Bytes;

    fn method(&self) -> Method {
        Method::GET
    }

    async fn response_body(self, resp: Response) -> Result<Self::ResponseBody> {
        Ok(resp.bytes().await?)
    }

    fn url(&self) -> Url {
        let mut url = BASE_URL.parse::<Url>().unwrap();
        url.set_path(&format!(
            "{}/{}/{}{}",
            PRONUNCIATION_PATH, self.dictionary_id.0, self.version_id.0, DOWNLOAD_PATH
        ));
        url
    }
}

/// This endpoint removes the specified rules from the pronunciation dictionary.
///
/// # Example
/// ```no_run
/// use elevenlabs_rs::*;
/// use elevenlabs_rs::endpoints::pronunciation::*;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///    let c = ElevenLabsClient::default()?;
///    let remove_rules = vec!["rule_string_1", "rule_string_2"];
///    let resp = c.hit(RemoveRules::new("dictionary_id", remove_rules)).await?;
///    println!("{:?}", resp);
///    Ok(())
/// }
/// ```
#[derive(Clone, Debug)]
pub struct RemoveRules {
    param: DictionaryID,
    body: RemoveRulesBody,
}

impl RemoveRules {
    pub fn new(dictionary_id: &str, rules: Vec<&str>) -> Self {
        let body = RemoveRulesBody::new(rules);
        Self {
            param: DictionaryID::from(dictionary_id.to_string()),
            body,
        }
    }
}

/// This struct is used to build the request body for the RemoveRules endpoint.
/// The rules are removed from the latest version of the pronunciation dictionary.
#[derive(Clone, Debug, Serialize)]
struct RemoveRulesBody {
    rule_strings: Vec<String>,
}

impl RemoveRulesBody {
    fn new(rules: Vec<&str>) -> Self {
        Self {
            rule_strings: rules.iter().map(|r| r.to_string()).collect(),
        }
    }
}

impl Endpoint for RemoveRules {
    type ResponseBody = RulesResponse;

    fn method(&self) -> Method {
        Method::POST
    }

    fn request_body(&self) -> Result<RequestBody> {
        Ok(RequestBody::Json(serde_json::to_value(&self.body)?))
    }

    async fn response_body(self, resp: Response) -> Result<Self::ResponseBody> {
        Ok(resp.json().await?)
    }

    fn url(&self) -> Url {
        let mut url = BASE_URL.parse::<Url>().unwrap();
        url.set_path(&format!(
            "{}/{}{}",
            PRONUNCIATION_PATH, self.param.0, REMOVE_RULES_PATH
        ));
        url
    }
}