voirs-cli 0.1.0-rc.1

Command-line interface 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
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
//! Configuration profile management.
//!
//! Provides support for multiple configuration profiles, allowing users to
//! quickly switch between different settings for different use cases.

use crate::config::{CliConfig, ConfigManager};
use crate::error::{CliError, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

/// Profile metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProfileInfo {
    /// Profile name
    pub name: String,
    /// Profile description
    pub description: Option<String>,
    /// Creation timestamp
    pub created_at: chrono::DateTime<chrono::Utc>,
    /// Last modified timestamp
    pub modified_at: chrono::DateTime<chrono::Utc>,
    /// Tags for categorization
    pub tags: Vec<String>,
    /// Whether this is a system/built-in profile
    pub system: bool,
}

/// Profile configuration container
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfigProfile {
    /// Profile metadata
    pub info: ProfileInfo,
    /// The actual configuration
    pub config: CliConfig,
}

/// Profile manager for handling multiple configurations
pub struct ProfileManager {
    profiles_dir: PathBuf,
    current_profile: Option<String>,
    profiles_cache: HashMap<String, ConfigProfile>,
}

impl ProfileManager {
    /// Create a new profile manager
    pub fn new() -> Result<Self> {
        let profiles_dir = Self::get_profiles_directory()
            .ok_or_else(|| CliError::config("Cannot determine profiles directory"))?;

        // Create profiles directory if it doesn't exist
        fs::create_dir_all(&profiles_dir).map_err(|e| {
            CliError::file_operation("create directory", &profiles_dir.display().to_string(), e)
        })?;

        let mut manager = Self {
            profiles_dir,
            current_profile: None,
            profiles_cache: HashMap::new(),
        };

        // Load current profile selection
        manager.load_current_profile()?;

        // Initialize with default profiles if none exist
        if manager.list_profiles()?.is_empty() {
            manager.create_default_profiles()?;
        }

        Ok(manager)
    }

    /// Create a new profile
    pub fn create_profile(
        &mut self,
        name: &str,
        description: Option<String>,
        config: CliConfig,
    ) -> Result<()> {
        if self.profile_exists(name) {
            return Err(CliError::config(format!(
                "Profile '{}' already exists",
                name
            )));
        }

        if !Self::is_valid_profile_name(name) {
            return Err(CliError::config(
                "Profile name must contain only alphanumeric characters, hyphens, and underscores",
            ));
        }

        let profile = ConfigProfile {
            info: ProfileInfo {
                name: name.to_string(),
                description,
                created_at: chrono::Utc::now(),
                modified_at: chrono::Utc::now(),
                tags: Vec::new(),
                system: false,
            },
            config,
        };

        self.save_profile(&profile)?;
        self.profiles_cache.insert(name.to_string(), profile);

        Ok(())
    }

    /// Update an existing profile
    pub fn update_profile(&mut self, name: &str, config: CliConfig) -> Result<()> {
        let mut profile = self.load_profile(name)?;
        profile.config = config;
        profile.info.modified_at = chrono::Utc::now();

        self.save_profile(&profile)?;
        self.profiles_cache.insert(name.to_string(), profile);

        Ok(())
    }

    /// Delete a profile
    pub fn delete_profile(&mut self, name: &str) -> Result<()> {
        if !self.profile_exists(name) {
            return Err(CliError::config(format!(
                "Profile '{}' does not exist",
                name
            )));
        }

        // Check if it's a system profile
        if let Ok(profile) = self.load_profile(name) {
            if profile.info.system {
                return Err(CliError::config(format!(
                    "Cannot delete system profile '{}'",
                    name
                )));
            }
        }

        // Don't allow deleting the current profile without switching first
        if self.current_profile.as_ref() == Some(&name.to_string()) {
            return Err(CliError::config(
                "Cannot delete the currently active profile. Switch to another profile first.",
            ));
        }

        let profile_path = self.get_profile_path(name);
        fs::remove_file(&profile_path).map_err(|e| {
            CliError::file_operation("delete", &profile_path.display().to_string(), e)
        })?;

        self.profiles_cache.remove(name);

        Ok(())
    }

    /// Switch to a different profile
    pub fn switch_profile(&mut self, name: &str) -> Result<()> {
        if !self.profile_exists(name) {
            return Err(CliError::config(format!(
                "Profile '{}' does not exist",
                name
            )));
        }

        self.current_profile = Some(name.to_string());
        self.save_current_profile()?;

        Ok(())
    }

    /// Get the current profile
    pub fn get_current_profile(&self) -> Result<Option<ConfigProfile>> {
        if let Some(ref name) = self.current_profile {
            Ok(Some(self.load_profile(name)?))
        } else {
            Ok(None)
        }
    }

    /// Get current profile name
    pub fn get_current_profile_name(&self) -> Option<&str> {
        self.current_profile.as_deref()
    }

    /// Load a specific profile
    pub fn load_profile(&self, name: &str) -> Result<ConfigProfile> {
        if let Some(profile) = self.profiles_cache.get(name) {
            return Ok(profile.clone());
        }

        let profile_path = self.get_profile_path(name);
        if !profile_path.exists() {
            return Err(CliError::config(format!(
                "Profile '{}' does not exist",
                name
            )));
        }

        let content = fs::read_to_string(&profile_path).map_err(|e| {
            CliError::file_operation("read", &profile_path.display().to_string(), e)
        })?;

        let profile: ConfigProfile = toml::from_str(&content).map_err(|e| {
            CliError::config(format!("Invalid profile format for '{}': {}", name, e))
        })?;

        Ok(profile)
    }

    /// List all available profiles
    pub fn list_profiles(&self) -> Result<Vec<ProfileInfo>> {
        let mut profiles = Vec::new();

        let entries = fs::read_dir(&self.profiles_dir).map_err(|e| {
            CliError::file_operation(
                "read directory",
                &self.profiles_dir.display().to_string(),
                e,
            )
        })?;

        for entry in entries {
            let entry =
                entry.map_err(|e| CliError::file_operation("read directory entry", "", e))?;
            let path = entry.path();

            if path.extension().and_then(|s| s.to_str()) == Some("toml") {
                if let Some(name) = path.file_stem().and_then(|s| s.to_str()) {
                    if let Ok(profile) = self.load_profile(name) {
                        profiles.push(profile.info);
                    }
                }
            }
        }

        // Sort by name
        profiles.sort_by(|a, b| a.name.cmp(&b.name));

        Ok(profiles)
    }

    /// Copy an existing profile with a new name
    pub fn copy_profile(
        &mut self,
        source: &str,
        target: &str,
        description: Option<String>,
    ) -> Result<()> {
        if !self.profile_exists(source) {
            return Err(CliError::config(format!(
                "Source profile '{}' does not exist",
                source
            )));
        }

        if self.profile_exists(target) {
            return Err(CliError::config(format!(
                "Target profile '{}' already exists",
                target
            )));
        }

        if !Self::is_valid_profile_name(target) {
            return Err(CliError::config(
                "Profile name must contain only alphanumeric characters, hyphens, and underscores",
            ));
        }

        let source_profile = self.load_profile(source)?;
        let mut new_profile = source_profile.clone();
        new_profile.info.name = target.to_string();
        new_profile.info.description = description;
        new_profile.info.created_at = chrono::Utc::now();
        new_profile.info.modified_at = chrono::Utc::now();
        new_profile.info.system = false;

        self.save_profile(&new_profile)?;
        self.profiles_cache.insert(target.to_string(), new_profile);

        Ok(())
    }

    /// Export a profile to a file
    pub fn export_profile(&self, name: &str, export_path: &Path) -> Result<()> {
        let profile = self.load_profile(name)?;

        let content = toml::to_string_pretty(&profile)
            .map_err(|e| CliError::config(format!("Failed to serialize profile: {}", e)))?;

        fs::write(export_path, content).map_err(|e| {
            CliError::file_operation("write", &export_path.display().to_string(), e)
        })?;

        Ok(())
    }

    /// Import a profile from a file
    pub fn import_profile(&mut self, import_path: &Path, name: Option<&str>) -> Result<()> {
        let content = fs::read_to_string(import_path)
            .map_err(|e| CliError::file_operation("read", &import_path.display().to_string(), e))?;

        let mut profile: ConfigProfile = toml::from_str(&content)
            .map_err(|e| CliError::config(format!("Invalid profile format: {}", e)))?;

        // Use provided name or the one in the file
        let final_name = name
            .map(|s| s.to_string())
            .unwrap_or_else(|| profile.info.name.clone());

        if self.profile_exists(&final_name) {
            return Err(CliError::config(format!(
                "Profile '{}' already exists",
                final_name
            )));
        }

        profile.info.name = final_name.clone();
        profile.info.created_at = chrono::Utc::now();
        profile.info.modified_at = chrono::Utc::now();
        profile.info.system = false;

        self.save_profile(&profile)?;
        self.profiles_cache.insert(final_name.clone(), profile);

        Ok(())
    }

    /// Add tags to a profile
    pub fn add_tags(&mut self, name: &str, tags: Vec<String>) -> Result<()> {
        let mut profile = self.load_profile(name)?;
        for tag in tags {
            if !profile.info.tags.contains(&tag) {
                profile.info.tags.push(tag);
            }
        }
        profile.info.modified_at = chrono::Utc::now();

        self.save_profile(&profile)?;
        self.profiles_cache.insert(name.to_string(), profile);

        Ok(())
    }

    /// Remove tags from a profile
    pub fn remove_tags(&mut self, name: &str, tags: Vec<String>) -> Result<()> {
        let mut profile = self.load_profile(name)?;
        profile.info.tags.retain(|tag| !tags.contains(tag));
        profile.info.modified_at = chrono::Utc::now();

        self.save_profile(&profile)?;
        self.profiles_cache.insert(name.to_string(), profile);

        Ok(())
    }

    /// Search profiles by tags
    pub fn find_profiles_by_tags(&self, tags: &[String]) -> Result<Vec<ProfileInfo>> {
        let all_profiles = self.list_profiles()?;
        let matching_profiles = all_profiles
            .into_iter()
            .filter(|profile| tags.iter().any(|tag| profile.tags.contains(tag)))
            .collect();

        Ok(matching_profiles)
    }

    // Private helper methods

    fn profile_exists(&self, name: &str) -> bool {
        self.get_profile_path(name).exists()
    }

    fn get_profile_path(&self, name: &str) -> PathBuf {
        self.profiles_dir.join(format!("{}.toml", name))
    }

    fn save_profile(&self, profile: &ConfigProfile) -> Result<()> {
        let profile_path = self.get_profile_path(&profile.info.name);

        let content = toml::to_string_pretty(profile)
            .map_err(|e| CliError::config(format!("Failed to serialize profile: {}", e)))?;

        fs::write(&profile_path, content).map_err(|e| {
            CliError::file_operation("write", &profile_path.display().to_string(), e)
        })?;

        Ok(())
    }

    fn load_current_profile(&mut self) -> Result<()> {
        let current_file = self.profiles_dir.join("current");
        if current_file.exists() {
            let content = fs::read_to_string(&current_file).map_err(|e| {
                CliError::file_operation("read", &current_file.display().to_string(), e)
            })?;
            let name = content.trim();
            if self.profile_exists(name) {
                self.current_profile = Some(name.to_string());
            }
        }
        Ok(())
    }

    fn save_current_profile(&self) -> Result<()> {
        let current_file = self.profiles_dir.join("current");
        if let Some(ref current) = self.current_profile {
            fs::write(&current_file, current).map_err(|e| {
                CliError::file_operation("write", &current_file.display().to_string(), e)
            })?;
        } else {
            // Remove current file if no profile is selected
            if current_file.exists() {
                fs::remove_file(&current_file).map_err(|e| {
                    CliError::file_operation("delete", &current_file.display().to_string(), e)
                })?;
            }
        }
        Ok(())
    }

    fn create_default_profiles(&mut self) -> Result<()> {
        // Default profile
        let default_profile = ConfigProfile {
            info: ProfileInfo {
                name: "default".to_string(),
                description: Some("Default VoiRS configuration".to_string()),
                created_at: chrono::Utc::now(),
                modified_at: chrono::Utc::now(),
                tags: vec!["system".to_string()],
                system: true,
            },
            config: CliConfig::default(),
        };
        self.save_profile(&default_profile)?;

        // High-quality profile
        let mut hq_config = CliConfig::default();
        hq_config.cli.default_quality = "ultra".to_string();
        hq_config.cli.default_output_format = "flac".to_string();
        let hq_profile = ConfigProfile {
            info: ProfileInfo {
                name: "high-quality".to_string(),
                description: Some("High-quality synthesis with FLAC output".to_string()),
                created_at: chrono::Utc::now(),
                modified_at: chrono::Utc::now(),
                tags: vec!["system".to_string(), "quality".to_string()],
                system: true,
            },
            config: hq_config,
        };
        self.save_profile(&hq_profile)?;

        // Fast profile
        let mut fast_config = CliConfig::default();
        fast_config.cli.default_quality = "low".to_string();
        fast_config.cli.show_progress = false;
        let fast_profile = ConfigProfile {
            info: ProfileInfo {
                name: "fast".to_string(),
                description: Some("Fast synthesis for quick testing".to_string()),
                created_at: chrono::Utc::now(),
                modified_at: chrono::Utc::now(),
                tags: vec!["system".to_string(), "speed".to_string()],
                system: true,
            },
            config: fast_config,
        };
        self.save_profile(&fast_profile)?;

        // Set default as current if no current profile is set
        if self.current_profile.is_none() {
            self.current_profile = Some("default".to_string());
            self.save_current_profile()?;
        }

        Ok(())
    }

    fn is_valid_profile_name(name: &str) -> bool {
        !name.is_empty()
            && name.len() <= 50
            && name
                .chars()
                .all(|c| c.is_alphanumeric() || c == '-' || c == '_')
    }

    fn get_profiles_directory() -> Option<PathBuf> {
        if let Ok(xdg_config) = std::env::var("XDG_CONFIG_HOME") {
            Some(PathBuf::from(xdg_config).join("voirs").join("profiles"))
        } else if let Ok(home) = std::env::var("HOME") {
            Some(
                PathBuf::from(home)
                    .join(".config")
                    .join("voirs")
                    .join("profiles"),
            )
        } else if let Ok(appdata) = std::env::var("APPDATA") {
            Some(PathBuf::from(appdata).join("voirs").join("profiles"))
        } else {
            None
        }
    }
}

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

    #[test]
    fn test_profile_name_validation() {
        assert!(ProfileManager::is_valid_profile_name("default"));
        assert!(ProfileManager::is_valid_profile_name("test-profile"));
        assert!(ProfileManager::is_valid_profile_name("test_profile"));
        assert!(ProfileManager::is_valid_profile_name("profile123"));

        assert!(!ProfileManager::is_valid_profile_name(""));
        assert!(!ProfileManager::is_valid_profile_name(
            "profile with spaces"
        ));
        assert!(!ProfileManager::is_valid_profile_name("profile.dot"));
        assert!(!ProfileManager::is_valid_profile_name("profile/slash"));
    }

    #[test]
    fn test_profile_info_creation() {
        let info = ProfileInfo {
            name: "test".to_string(),
            description: Some("Test profile".to_string()),
            created_at: chrono::Utc::now(),
            modified_at: chrono::Utc::now(),
            tags: vec!["test".to_string()],
            system: false,
        };

        assert_eq!(info.name, "test");
        assert_eq!(info.description, Some("Test profile".to_string()));
        assert!(!info.system);
        assert_eq!(info.tags.len(), 1);
    }
}