framefinery_api/
settings.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub enum SettingValue {
4 Boolean,
6 Choice(&'static [&'static str]),
8 IntegerRange {
10 min: u16,
12 max: u16,
14 },
15 SignedIntegerRange {
17 min: i32,
19 max: i32,
21 },
22}
23
24#[derive(Debug, Clone, Copy)]
26pub struct SettingSpecForm {
27 pub syntax: &'static str,
29 pub summary: &'static str,
31}
32
33#[derive(Debug, Clone, Copy)]
35pub struct SettingSpecExample {
36 pub spec: &'static str,
38 pub summary: &'static str,
40}
41
42#[derive(Debug, Clone, Copy)]
44pub struct SettingSpecManifest {
45 pub forms: &'static [SettingSpecForm],
47 pub examples: &'static [SettingSpecExample],
49 pub notes: &'static [&'static str],
51}
52
53#[derive(Debug, Clone, Copy)]
55pub struct SettingManifest {
56 pub name: &'static str,
58 pub value: SettingValue,
60 pub default_value: Option<&'static str>,
62 pub spec: &'static SettingSpecManifest,
64 pub summary: &'static str,
66}
67
68impl SettingValue {
69 pub fn accepts(self, value: &str) -> bool {
71 match self {
72 SettingValue::Boolean => matches!(
73 value,
74 "true" | "false" | "1" | "0" | "yes" | "no" | "on" | "off"
75 ),
76 SettingValue::Choice(values) => values.contains(&value),
77 SettingValue::IntegerRange { min, max } => value
78 .parse::<u16>()
79 .is_ok_and(|parsed| (min..=max).contains(&parsed)),
80 SettingValue::SignedIntegerRange { min, max } => value
81 .parse::<i32>()
82 .is_ok_and(|parsed| (min..=max).contains(&parsed)),
83 }
84 }
85}
86
87pub fn setting_name(spec: &str) -> &str {
89 spec.split_once('=').map_or(spec, |(name, _)| name)
90}
91
92pub fn setting_value(spec: &str) -> Option<&str> {
94 spec.split_once('=').map(|(_, value)| value)
95}
96
97pub fn boolean_setting_enabled(
101 settings: &[String],
102 name: &str,
103) -> std::result::Result<bool, String> {
104 for spec in settings {
105 if setting_name(spec) != name {
106 continue;
107 }
108 let value = setting_value(spec).unwrap_or("true");
109 match value {
110 "true" | "1" | "yes" | "on" => return Ok(true),
111 "false" | "0" | "no" | "off" => return Ok(false),
112 _ => return Err(format!("{name} expects true or false, got '{value}'")),
113 }
114 }
115 Ok(false)
116}
117
118pub fn u8_setting(settings: &[String], name: &str) -> std::result::Result<Option<u8>, String> {
122 for spec in settings {
123 if setting_name(spec) != name {
124 continue;
125 }
126 let value = setting_value(spec).unwrap_or("true");
127 let parsed = value
128 .parse::<u16>()
129 .map_err(|_| format!("{name} expects an integer from 1 through 255, got '{value}'"))?;
130 if parsed == 0 || parsed > u16::from(u8::MAX) {
131 return Err(format!(
132 "{name} expects an integer from 1 through 255, got '{value}'"
133 ));
134 }
135 return Ok(Some(parsed as u8));
136 }
137 Ok(None)
138}
139
140pub fn i32_setting(settings: &[String], name: &str) -> std::result::Result<Option<i32>, String> {
144 for spec in settings {
145 if setting_name(spec) != name {
146 continue;
147 }
148 let value = setting_value(spec).unwrap_or("true");
149 let parsed = value
150 .parse::<i32>()
151 .map_err(|_| format!("{name} expects a signed integer, got '{value}'"))?;
152 return Ok(Some(parsed));
153 }
154 Ok(None)
155}
156
157pub fn setting_values_label(setting: SettingManifest) -> String {
159 match setting.value {
160 SettingValue::Boolean => "true|false".to_string(),
161 SettingValue::Choice(values) => values.join("|"),
162 SettingValue::IntegerRange { min, max } => format!("{min}..{max}"),
163 SettingValue::SignedIntegerRange { min, max } => format!("{min}..{max}"),
164 }
165}
166
167const LOSSLESS_SPEC_FORMS: &[SettingSpecForm] = &[
168 SettingSpecForm {
169 syntax: "lossless",
170 summary: "enable lossless coding",
171 },
172 SettingSpecForm {
173 syntax: "lossless=<bool>",
174 summary: "explicitly enable or disable lossless coding",
175 },
176];
177
178const LOSSLESS_SPEC_EXAMPLES: &[SettingSpecExample] = &[
179 SettingSpecExample {
180 spec: "lossless",
181 summary: "request lossless output",
182 },
183 SettingSpecExample {
184 spec: "lossless=false",
185 summary: "explicitly leave lossy coding enabled",
186 },
187];
188
189const LOSSLESS_SPEC_NOTES: &[&str] = &[
190 "bare boolean settings imply true",
191 "mutually exclusive with codec quantization settings",
192];
193
194pub const LOSSLESS_SETTING_SPEC: SettingSpecManifest = SettingSpecManifest {
196 forms: LOSSLESS_SPEC_FORMS,
197 examples: LOSSLESS_SPEC_EXAMPLES,
198 notes: LOSSLESS_SPEC_NOTES,
199};
200
201pub const LOSSLESS_SETTING: SettingManifest = SettingManifest {
203 name: "lossless",
204 value: SettingValue::Boolean,
205 default_value: Some("false"),
206 spec: &LOSSLESS_SETTING_SPEC,
207 summary: "request lossless coding when supported",
208};
209
210pub const GLOBAL_SETTINGS: &[SettingManifest] = &[LOSSLESS_SETTING];