summaryrefslogtreecommitdiff
path: root/racer-tracer/src/config.rs
blob: 4cb088020ce6871ff3da113f25091e2b81088ea6 (plain)
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
use std::{path::PathBuf, str::FromStr};

use config::File;
use serde::Deserialize;
use structopt::StructOpt;

use crate::error::TracerError;

#[derive(Default, Debug, Deserialize)]
pub struct Screen {
    pub height: usize,
    pub width: usize,
}

#[derive(Default, Debug, Deserialize)]
pub struct RenderData {
    pub samples: usize,
    pub max_depth: usize,
    pub num_threads_width: usize,
    pub num_threads_height: usize,
    pub scale: usize,
}

#[derive(StructOpt, Debug)]
#[structopt(name = "racer-tracer")]
pub struct Args {
    #[structopt(
        short = "c",
        long = "config",
        default_value = "./config.yml",
        env = "CONFIG"
    )]
    pub config: String,

    #[structopt(short = "s", long = "scene")]
    pub scene: Option<String>,

    #[structopt(long = "image-action")]
    pub image_action: Option<ImageAction>,
}

impl TryFrom<Args> for Config {
    type Error = TracerError;
    fn try_from(args: Args) -> Result<Self, TracerError> {
        Config::from_file(args.config).and_then(|mut cfg| {
            if let Some(image_action) = args.image_action {
                cfg.image_action = image_action;
            }

            if let Some(scene) = args.scene {
                if scene == "random" {
                    cfg.loader = SceneLoader::Random;
                } else {
                    let path = PathBuf::from(scene);
                    cfg.loader = path
                        .extension()
                        .map(|s| s.to_string_lossy())
                        .ok_or_else(|| {
                            TracerError::ArgumentParsingError(format!(
                                "Could not get extension from scene file: {}",
                                path.display()
                            ))
                        })
                        .and_then(|p| match p.as_ref() {
                            "yml" => Ok(SceneLoader::Yml { path: path.clone() }),
                            _ => Err(TracerError::ArgumentParsingError(format!(
                                "Could not find a suitable scene loader for file: {}",
                                path.display()
                            ))),
                        })?;
                };
            }

            Ok(cfg)
        })
    }
}

#[derive(StructOpt, Debug, Clone, Deserialize, Default)]
pub enum SceneLoader {
    #[default]
    None,
    Yml {
        path: PathBuf,
    },
    Random,
}

#[derive(StructOpt, Debug, Clone, Deserialize, Default)]
pub enum ImageAction {
    #[default]
    WaitForSignal,
    SavePng,
}

impl FromStr for ImageAction {
    type Err = TracerError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "png" => Ok(ImageAction::SavePng),
            "show" => Ok(ImageAction::WaitForSignal),
            _ => Ok(ImageAction::WaitForSignal),
        }
    }
}

#[derive(Default, Debug, Deserialize)]
pub struct Config {
    #[serde(default)]
    pub preview: RenderData,

    #[serde(default)]
    pub render: RenderData,

    #[serde(default)]
    pub screen: Screen,

    #[serde(default)]
    pub loader: SceneLoader,

    #[serde(default)]
    pub image_action: ImageAction,

    #[serde(default)]
    pub image_output_dir: Option<PathBuf>,
}

impl Config {
    pub fn from_file(file: String) -> Result<Self, TracerError> {
        config::Config::builder()
            .add_source(File::from(file.as_ref()))
            .build()
            .map_err(|e| TracerError::Configuration(file.clone(), e.to_string()))?
            .try_deserialize()
            .map_err(|e| TracerError::Configuration(file, e.to_string()))
    }
}