pub struct App { /* private fields */ }
Expand description
Main application structure with dependency injection support.
The App
struct provides a programmatic interface to SubX functionality,
designed for embedding SubX in other Rust applications or for advanced
use cases requiring fine-grained control over configuration and execution.
§Use Cases
- Embedding: Use SubX as a library component in larger applications
- Testing: Programmatic testing of SubX functionality with custom configurations
- Automation: Scripted execution of SubX operations without shell commands
- Custom Workflows: Building complex workflows that combine multiple SubX operations
§vs CLI Interface
Feature | CLI (subx command) | App (Library API) |
---|---|---|
Usage | Command line tool | Embedded in Rust code |
Config | Files + Environment | Programmatic injection |
Output | Terminal/stdout | Programmatic control |
Error Handling | Exit codes | Result types |
§Examples
§Basic Usage
use subx_cli::{App, config::ProductionConfigService};
use std::sync::Arc;
let config_service = Arc::new(ProductionConfigService::new()?);
let app = App::new(config_service);
// Execute operations programmatically
app.match_files("/movies", true).await?; // dry run
app.convert_files("/subs", "srt", Some("/output")).await?;
§With Custom Configuration
use subx_cli::{App, config::{TestConfigService, Config}};
use std::sync::Arc;
let mut config_service = TestConfigService::with_ai_settings("openai", "gpt-4");
let app = App::new(Arc::new(config_service));
app.match_files("/path", false).await?;
Implementations§
Source§impl App
impl App
Sourcepub fn new(config_service: Arc<dyn ConfigService>) -> Self
pub fn new(config_service: Arc<dyn ConfigService>) -> Self
Create a new application instance with the provided configuration service.
§Arguments
config_service
- The configuration service to use
§Examples
use subx_cli::{App, config::TestConfigService};
use std::sync::Arc;
let config_service = Arc::new(TestConfigService::with_defaults());
let app = App::new(config_service);
Sourcepub fn new_with_production_config() -> Result<Self>
pub fn new_with_production_config() -> Result<Self>
Create a new application instance with the production configuration service.
This is the default way to create an application instance for production use.
§Examples
use subx_cli::App;
let app = App::new_with_production_config()?;
// Ready to use with production configuration
§Errors
Returns an error if the production configuration service cannot be created.
Sourcepub async fn run(&self) -> Result<()>
pub async fn run(&self) -> Result<()>
Run the application with command-line argument parsing.
This method provides a programmatic way to run SubX with CLI-style arguments, useful for embedding SubX in other Rust applications.
§Examples
use subx_cli::{App, config::ProductionConfigService};
use std::sync::Arc;
let config_service = Arc::new(ProductionConfigService::new()?);
let app = App::new(config_service);
// This parses std::env::args() just like the CLI
app.run().await?;
§Errors
Returns an error if command execution fails.
Sourcepub async fn handle_command(&self, command: Commands) -> Result<()>
pub async fn handle_command(&self, command: Commands) -> Result<()>
Handle a specific command with the current configuration.
This method allows programmatic execution of specific SubX commands without parsing command-line arguments.
§Examples
use subx_cli::{App, cli::{Commands, MatchArgs}, config::TestConfigService};
use std::sync::Arc;
let config_service = Arc::new(TestConfigService::with_defaults());
let app = App::new(config_service);
let match_args = MatchArgs {
path: Some("/path/to/files".into()),
input_paths: vec![],
dry_run: true,
confidence: 80,
recursive: false,
backup: false,
copy: false,
move_files: false,
};
app.handle_command(Commands::Match(match_args)).await?;
§Arguments
command
- The command to execute
§Errors
Returns an error if command execution fails.
Sourcepub async fn match_files(&self, input_path: &str, dry_run: bool) -> Result<()>
pub async fn match_files(&self, input_path: &str, dry_run: bool) -> Result<()>
Execute a match operation programmatically.
This is a convenience method for programmatic usage without needing to construct the Commands enum manually.
§Examples
use subx_cli::{App, config::TestConfigService};
use std::sync::Arc;
let config_service = Arc::new(TestConfigService::with_defaults());
let app = App::new(config_service);
// Match files programmatically
app.match_files("/path/to/files", true).await?; // dry_run = true
§Arguments
input_path
- Path to the directory or file to processdry_run
- Whether to perform a dry run (no actual changes)
§Errors
Returns an error if the match operation fails.
Sourcepub async fn convert_files(
&self,
input_path: &str,
output_format: &str,
output_path: Option<&str>,
) -> Result<()>
pub async fn convert_files( &self, input_path: &str, output_format: &str, output_path: Option<&str>, ) -> Result<()>
Convert subtitle files programmatically.
§Examples
use subx_cli::{App, config::TestConfigService};
use std::sync::Arc;
let config_service = Arc::new(TestConfigService::with_defaults());
let app = App::new(config_service);
// Convert to SRT format
app.convert_files("/path/to/subtitles", "srt", Some("/output/path")).await?;
§Arguments
input_path
- Path to subtitle files to convertoutput_format
- Target format (“srt”, “ass”, “vtt”, etc.)output_path
- Optional output directory path
§Errors
Returns an error if the conversion fails.
Sourcepub async fn sync_files(
&self,
video_path: &str,
subtitle_path: &str,
method: &str,
) -> Result<()>
pub async fn sync_files( &self, video_path: &str, subtitle_path: &str, method: &str, ) -> Result<()>
Synchronize subtitle files programmatically.
§Examples
use subx_cli::{App, config::TestConfigService};
use std::sync::Arc;
let config_service = Arc::new(TestConfigService::with_defaults());
let app = App::new(config_service);
// Synchronize using VAD method
app.sync_files("/path/to/video.mp4", "/path/to/subtitle.srt", "vad").await?;
§Arguments
video_path
- Path to video file for audio analysissubtitle_path
- Path to subtitle file to synchronizemethod
- Synchronization method (“vad”, “manual”)
§Errors
Returns an error if synchronization fails.
Sourcepub async fn sync_files_with_offset(
&self,
subtitle_path: &str,
offset: f32,
) -> Result<()>
pub async fn sync_files_with_offset( &self, subtitle_path: &str, offset: f32, ) -> Result<()>
Synchronize subtitle files with manual offset.
§Examples
use subx_cli::{App, config::TestConfigService};
use std::sync::Arc;
let config_service = Arc::new(TestConfigService::with_defaults());
let app = App::new(config_service);
// Apply +2.5 second offset to subtitles
app.sync_files_with_offset("/path/to/subtitle.srt", 2.5).await?;
§Arguments
subtitle_path
- Path to subtitle file to synchronizeoffset
- Time offset in seconds (positive delays, negative advances)
§Errors
Returns an error if synchronization fails.
Sourcepub fn config_service(&self) -> &Arc<dyn ConfigService>
pub fn config_service(&self) -> &Arc<dyn ConfigService>
Get a reference to the configuration service.
This allows access to the configuration service for testing or advanced use cases.
Sourcepub fn get_config(&self) -> Result<Config>
pub fn get_config(&self) -> Result<Config>
Get the current configuration.
This is a convenience method that retrieves the configuration from the configured service.
§Errors
Returns an error if configuration loading fails.