orbiton/commands/
renderer.rs

1// Command for configuring the renderer
2
3use anyhow::{Context, Result};
4use clap::Args;
5use console::style;
6use std::path::PathBuf;
7
8#[derive(Args)]
9pub struct RendererArgs {
10    /// Renderer configuration (skia, wgpu, auto)
11    #[arg(short, long)]
12    config: String,
13
14    /// Project directory
15    #[arg(short, long)]
16    dir: Option<PathBuf>,
17}
18
19pub fn execute(args: RendererArgs) -> Result<()> {
20    // Determine the project directory
21    let project_dir = match args.dir {
22        Some(dir) => dir,
23        None => std::env::current_dir()?,
24    };
25
26    println!(
27        "{} renderer to {}",
28        style("Configuring").bold().green(),
29        style(&args.config).bold()
30    );
31
32    // Validate the renderer configuration
33    let renderer_type = match args.config.to_lowercase().as_str() {
34        "skia" => "skia",
35        "wgpu" => "wgpu",
36        "auto" => "auto",
37        _ => {
38            return Err(anyhow::anyhow!(
39                "Invalid renderer configuration: {}. Valid options are: skia, wgpu, auto",
40                args.config
41            ));
42        }
43    };
44
45    // Update the project configuration file
46    let config_file = project_dir.join("orbit.config.json");
47
48    // If the config file exists, read it; otherwise, create a new one
49    let mut config = if config_file.exists() {
50        let config_str = std::fs::read_to_string(&config_file)
51            .with_context(|| format!("Failed to read config file: {config_file:?}"))?;
52
53        serde_json::from_str(&config_str)
54            .with_context(|| format!("Failed to parse config file: {config_file:?}"))?
55    } else {
56        serde_json::json!({})
57    };
58
59    // Update the renderer configuration
60    if let Some(config_obj) = config.as_object_mut() {
61        config_obj.insert(
62            "renderer".to_string(),
63            serde_json::Value::String(renderer_type.to_string()),
64        );
65    }
66
67    // Write the updated configuration
68    let config_str =
69        serde_json::to_string_pretty(&config).with_context(|| "Failed to serialize config")?;
70
71    std::fs::write(&config_file, config_str)
72        .with_context(|| format!("Failed to write config file: {config_file:?}"))?;
73
74    println!(
75        "Renderer configured to {} in {config_file:?}",
76        style(renderer_type).bold()
77    );
78
79    Ok(())
80}