voirs-sdk 0.1.0-rc.1

Unified SDK and public API for VoiRS speech synthesis
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
use regex::Regex;
use std::fs;
use std::path::Path;
use voirs_sdk::prelude::*;

pub struct DocumentationTester {
    pub base_path: String,
    pub results: Vec<TestResult>,
}

#[derive(Debug, Clone)]
pub struct TestResult {
    pub file_path: String,
    pub test_name: String,
    pub passed: bool,
    pub error: Option<String>,
}

impl DocumentationTester {
    pub fn new(base_path: &str) -> Self {
        Self {
            base_path: base_path.to_string(),
            results: Vec::new(),
        }
    }

    pub async fn run_all_tests(&mut self) -> Result<()> {
        self.test_readme_examples().await?;
        self.test_lib_rs_examples().await?;
        self.test_module_documentation().await?;
        self.test_example_files().await?;
        self.test_api_documentation().await?;

        Ok(())
    }

    pub async fn test_readme_examples(&mut self) -> Result<()> {
        let readme_path = Path::new(&self.base_path).join("README.md");

        if !readme_path.exists() {
            self.results.push(TestResult {
                file_path: readme_path.to_string_lossy().to_string(),
                test_name: "README existence".to_string(),
                passed: false,
                error: Some("README.md not found".to_string()),
            });
            return Ok(());
        }

        let content = fs::read_to_string(&readme_path)
            .map_err(|e| VoirsError::config_error(format!("Failed to read README.md: {e}")))?;

        let code_blocks = self.extract_rust_code_blocks(&content);

        for (index, code_block) in code_blocks.iter().enumerate() {
            let test_name = format!("README code block {}", index + 1);

            match self.validate_code_block(code_block).await {
                Ok(()) => {
                    self.results.push(TestResult {
                        file_path: readme_path.to_string_lossy().to_string(),
                        test_name,
                        passed: true,
                        error: None,
                    });
                }
                Err(e) => {
                    self.results.push(TestResult {
                        file_path: readme_path.to_string_lossy().to_string(),
                        test_name,
                        passed: false,
                        error: Some(e.to_string()),
                    });
                }
            }
        }

        Ok(())
    }

    pub async fn test_lib_rs_examples(&mut self) -> Result<()> {
        let lib_path = Path::new(&self.base_path).join("src/lib.rs");

        let content = fs::read_to_string(&lib_path)
            .map_err(|e| VoirsError::config_error(format!("Failed to read lib.rs: {e}")))?;

        let code_blocks = self.extract_rust_code_blocks(&content);

        for (index, code_block) in code_blocks.iter().enumerate() {
            let test_name = format!("lib.rs code block {}", index + 1);

            match self.validate_code_block(code_block).await {
                Ok(()) => {
                    self.results.push(TestResult {
                        file_path: lib_path.to_string_lossy().to_string(),
                        test_name,
                        passed: true,
                        error: None,
                    });
                }
                Err(e) => {
                    self.results.push(TestResult {
                        file_path: lib_path.to_string_lossy().to_string(),
                        test_name,
                        passed: false,
                        error: Some(e.to_string()),
                    });
                }
            }
        }

        Ok(())
    }

    pub async fn test_module_documentation(&mut self) -> Result<()> {
        let src_path = Path::new(&self.base_path).join("src");
        let rust_files = Self::find_rust_files(&src_path)?;

        for file_path in rust_files {
            let content = fs::read_to_string(&file_path).map_err(|e| {
                VoirsError::config_error(format!("Failed to read {}: {}", file_path.display(), e))
            })?;

            let code_blocks = self.extract_rust_code_blocks(&content);

            for (index, code_block) in code_blocks.iter().enumerate() {
                let test_name = format!("{} code block {}", file_path.display(), index + 1);

                match self.validate_code_block(code_block).await {
                    Ok(()) => {
                        self.results.push(TestResult {
                            file_path: file_path.to_string_lossy().to_string(),
                            test_name,
                            passed: true,
                            error: None,
                        });
                    }
                    Err(e) => {
                        self.results.push(TestResult {
                            file_path: file_path.to_string_lossy().to_string(),
                            test_name,
                            passed: false,
                            error: Some(e.to_string()),
                        });
                    }
                }
            }
        }

        Ok(())
    }

    pub async fn test_example_files(&mut self) -> Result<()> {
        let examples_path = Path::new(&self.base_path).join("examples");

        if !examples_path.exists() {
            self.results.push(TestResult {
                file_path: examples_path.to_string_lossy().to_string(),
                test_name: "Examples directory existence".to_string(),
                passed: false,
                error: Some("Examples directory not found".to_string()),
            });
            return Ok(());
        }

        let example_files = Self::find_rust_files(&examples_path)?;

        for file_path in example_files {
            let test_name = format!("Example file: {}", file_path.display());

            match self.validate_example_file(&file_path).await {
                Ok(()) => {
                    self.results.push(TestResult {
                        file_path: file_path.to_string_lossy().to_string(),
                        test_name,
                        passed: true,
                        error: None,
                    });
                }
                Err(e) => {
                    self.results.push(TestResult {
                        file_path: file_path.to_string_lossy().to_string(),
                        test_name,
                        passed: false,
                        error: Some(e.to_string()),
                    });
                }
            }
        }

        Ok(())
    }

    pub async fn test_api_documentation(&mut self) -> Result<()> {
        let test_name = "API documentation completeness".to_string();

        match self.validate_api_documentation().await {
            Ok(()) => {
                self.results.push(TestResult {
                    file_path: "API documentation".to_string(),
                    test_name,
                    passed: true,
                    error: None,
                });
            }
            Err(e) => {
                self.results.push(TestResult {
                    file_path: "API documentation".to_string(),
                    test_name,
                    passed: false,
                    error: Some(e.to_string()),
                });
            }
        }

        Ok(())
    }

    pub fn extract_rust_code_blocks(&self, content: &str) -> Vec<String> {
        let mut blocks = Vec::new();

        // Pattern for all rust code blocks (with DOTALL flag for multiline)
        // This matches ```rust, ```rust,no_run, etc.
        let rust_code_regex = Regex::new(r"(?s)```rust(?:,\s*no_run)?\s*\n(.*?)\n```").unwrap();

        // Pattern for documentation comment code blocks (//! ```)
        let doc_code_block_regex =
            Regex::new(r"//!\s*```(?:rust,)?(?:\s*no_run)?\n((?://!.*\n)*?)//!\s*```").unwrap();

        // Extract regular markdown code blocks
        for cap in rust_code_regex.captures_iter(content) {
            if let Some(code) = cap.get(1) {
                blocks.push(code.as_str().to_string());
            }
        }

        // Extract documentation comment code blocks and clean them up
        for cap in doc_code_block_regex.captures_iter(content) {
            if let Some(code) = cap.get(1) {
                // Remove the "//! " prefix from each line
                let cleaned_code = code
                    .as_str()
                    .lines()
                    .map(|line| {
                        if let Some(stripped) = line.strip_prefix("//! ") {
                            stripped
                        } else if let Some(stripped) = line.strip_prefix("//!") {
                            stripped
                        } else {
                            line
                        }
                    })
                    .collect::<Vec<_>>()
                    .join("\n");
                blocks.push(cleaned_code);
            }
        }

        blocks
    }

    pub async fn validate_code_block(&self, code: &str) -> Result<()> {
        // Basic syntax validation
        if code.trim().is_empty() {
            return Err(VoirsError::config_error("Empty code block".to_string()));
        }

        // Check for basic Rust syntax patterns
        if !code.contains("fn ") && !code.contains("let ") && !code.contains("use ") {
            return Err(VoirsError::config_error(
                "Code block doesn't appear to be valid Rust".to_string(),
            ));
        }

        // Check for proper VoiRS SDK usage
        if (code.contains("voirs_sdk") || code.contains("VoirsPipeline"))
            && !code.contains("use voirs_sdk")
            && !code.contains("use crate::")
        {
            return Err(VoirsError::config_error(
                "Missing proper imports for VoiRS SDK".to_string(),
            ));
        }

        // Check for async/await patterns
        if (code.contains("async fn") || code.contains(".await"))
            && !code.contains("tokio::main")
            && !code.contains("async fn")
        {
            return Err(VoirsError::config_error(
                "Async code should have proper runtime setup".to_string(),
            ));
        }

        Ok(())
    }

    async fn validate_example_file(&self, file_path: &Path) -> Result<()> {
        let content = fs::read_to_string(file_path)
            .map_err(|e| VoirsError::config_error(format!("Failed to read example file: {e}")))?;

        // Check for basic structure
        if !content.contains("fn main") {
            return Err(VoirsError::config_error(
                "Example file missing main function".to_string(),
            ));
        }

        // Check for proper imports
        if !content.contains("use voirs_sdk") {
            return Err(VoirsError::config_error(
                "Example file missing VoiRS SDK imports".to_string(),
            ));
        }

        // Check for error handling
        if !content.contains("Result") && !content.contains("?") {
            return Err(VoirsError::config_error(
                "Example file should demonstrate error handling".to_string(),
            ));
        }

        Ok(())
    }

    async fn validate_api_documentation(&self) -> Result<()> {
        // Check that all public APIs are documented
        let lib_path = Path::new(&self.base_path).join("src/lib.rs");
        let content = fs::read_to_string(&lib_path)
            .map_err(|e| VoirsError::config_error(format!("Failed to read lib.rs: {e}")))?;

        let public_items = self.find_public_items(&content);
        let documented_items = self.find_documented_items(&content);

        for item in &public_items {
            if !documented_items.contains(item) {
                return Err(VoirsError::config_error(format!(
                    "Public item '{item}' is not documented"
                )));
            }
        }

        Ok(())
    }

    fn find_public_items(&self, content: &str) -> Vec<String> {
        let mut items = Vec::new();
        let pub_regex =
            Regex::new(r"pub\s+(?:fn|struct|enum|trait|mod|type|const|static)\s+(\w+)").unwrap();

        for cap in pub_regex.captures_iter(content) {
            if let Some(item) = cap.get(1) {
                items.push(item.as_str().to_string());
            }
        }

        items
    }

    fn find_documented_items(&self, content: &str) -> Vec<String> {
        let mut items = Vec::new();
        let doc_regex = Regex::new(r"///.*\n(?:.*\n)*?\s*(?:pub\s+)?(?:fn|struct|enum|trait|mod|type|const|static)\s+(\w+)").unwrap();

        for cap in doc_regex.captures_iter(content) {
            if let Some(item) = cap.get(1) {
                items.push(item.as_str().to_string());
            }
        }

        items
    }

    fn find_rust_files(dir: &Path) -> Result<Vec<std::path::PathBuf>> {
        let mut files = Vec::new();

        if dir.is_dir() {
            for entry in fs::read_dir(dir)
                .map_err(|e| VoirsError::config_error(format!("Failed to read directory: {e}")))?
            {
                let entry = entry.map_err(|e| {
                    VoirsError::config_error(format!("Failed to read directory entry: {e}"))
                })?;
                let path = entry.path();

                if path.is_dir() {
                    files.extend(Self::find_rust_files(&path)?);
                } else if path.extension().is_some_and(|ext| ext == "rs") {
                    files.push(path);
                }
            }
        }

        Ok(files)
    }

    pub fn get_results(&self) -> &[TestResult] {
        &self.results
    }

    pub fn get_summary(&self) -> TestSummary {
        let total = self.results.len();
        let passed = self.results.iter().filter(|r| r.passed).count();
        let failed = total - passed;

        TestSummary {
            total,
            passed,
            failed,
            pass_rate: if total > 0 {
                passed as f64 / total as f64
            } else {
                0.0
            },
        }
    }
}

#[derive(Debug, Clone)]
pub struct TestSummary {
    pub total: usize,
    pub passed: usize,
    pub failed: usize,
    pub pass_rate: f64,
}

impl std::fmt::Display for TestSummary {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Documentation Tests: {}/{} passed ({:.1}%)",
            self.passed,
            self.total,
            self.pass_rate * 100.0
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_documentation_tester_creation() {
        let tester = DocumentationTester::new(".");
        assert_eq!(tester.base_path, ".");
        assert!(tester.results.is_empty());
    }

    #[tokio::test]
    async fn test_code_block_extraction() {
        let content = r#"
Some text here.

```rust
fn main() {
    println!("Hello, world!");
}
```

More text.

```rust,no_run
use voirs_sdk::prelude::*;

async fn example() -> Result<(), VoirsError> {
    let pipeline = VoirsPipelineBuilder::new().build().await?;
    Ok(())
}
```
"#;

        let tester = DocumentationTester::new(".");
        let blocks = tester.extract_rust_code_blocks(content);

        assert_eq!(blocks.len(), 2);
        assert!(blocks[0].contains("fn main"));
        assert!(blocks[1].contains("VoirsPipelineBuilder"));
    }

    #[tokio::test]
    async fn test_code_block_validation() {
        let tester = DocumentationTester::new(".");

        // Valid code block
        let valid_code = r#"
use voirs_sdk::prelude::*;

fn main() {
    println!("Hello, world!");
}
"#;

        assert!(tester.validate_code_block(valid_code).await.is_ok());

        // Invalid code block (empty)
        let invalid_code = "";
        assert!(tester.validate_code_block(invalid_code).await.is_err());
    }

    #[tokio::test]
    async fn test_summary_display() {
        let summary = TestSummary {
            total: 10,
            passed: 8,
            failed: 2,
            pass_rate: 0.8,
        };

        let display = format!("{summary}");
        assert!(display.contains("8/10 passed"));
        assert!(display.contains("80.0%"));
    }
}