summaryrefslogtreecommitdiff
path: root/src/configuration/load.rs
blob: a0065fc22c2c03865f831eb695c20c85ca8cc69d (plain) (blame)
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
/*
	Copyright 2021-2023 Gabriel Jensen.

	This file is part of Luma.

	Luma is free software: you can redistribute it
	and/or modify it under the terms of the GNU
	Affero General Public License as published by
	the Free Software Foundation, either version 3
	of the License, or (at your option) any later
	version.

	Luma is distributed in the hope that it will be
	useful, but WITHOUT ANY WARRANTY; without even
	the implied warranty of MERCHANTABILITY or
	FITNESS FOR A PARTICULAR PURPOSE. See the GNU
	Affero General Public License for more details.

	You should have received a copy of the GNU
	Affero General Public License along with Luma.
	If not, see <https://www.gnu.org/licenses/>.
*/

use crate::configuration::Configuration;

extern crate toml;

use std::fs::read_to_string;
use std::str::FromStr;
use toml::{Table, Value};

impl Configuration {
	pub fn load(path: &str) -> Result<Self, String> {
		eprintln!("loading configuration at \"{path}\"");

		let configuration_text = match read_to_string(path) {
			Ok( content) => content,
			_            => return Err("unable to read file".to_string()),
		};

		let base_table = match Table::from_str(&configuration_text) {
			Ok( table) => table,
			_          => return Err("unable to parse configuration".to_string()),
		};

		let luma_table   = get_table(&base_table, "luma")?;
		let device_table = get_table(&base_table, "device")?;
		let video_table  = get_table(&base_table, "video")?;

		let version = get_integer(&luma_table, "version")?;

		if version < Self::VERSION { return Err(format!("ancient version - got {}, expected {}", version, Self::VERSION)) }
		if version > Self::VERSION { return Err(format!("out-of-date version - got {}, expected {}", version, Self::VERSION)) }

		let bootloader = get_string(&device_table, "bootloader")?;
		let image      = get_string(&device_table, "image")?;

		let scale = get_integer(&video_table, "scale")?;

		let configuration = Configuration {
			bootloader: bootloader.clone(),
			image:      image.clone(),

			scale: scale,
		};

		configuration.validate()?;
		return Ok(configuration);
	}
}

fn get_value<'a>(table: &'a Table, name: &str) -> Option<&'a Value> {
	if !table.contains_key(name) { return None };

	return Some(&table[name]);
}

fn get_table<'a>(table: &'a Table, name: &str) -> Result<&'a Table, String> {
	return match get_value(table, name) {
		Some(Value::Table(table)) => Ok(table),
		Some(_)                   => Err(format!("\"{name}\" should be a section")),
		_                         => Err("section \"{name}\" is required".to_string()),
	};
}

fn get_integer(table: &Table, name: &str) -> Result<u32, String> {
	return match get_value(table, name) {
		Some(Value::Integer(value)) => Ok(*value as u32),
		Some(_)                     => Err(format!("\"{name}\" should be an integer")),
		_                           => Err("missing integer \"{name}\"".to_string()),
	};
}

fn get_string<'a>(table: &'a Table, name: &str) -> Result<&'a String, String> {
	return match get_value(table, name) {
		Some(Value::String(string)) => Ok(string),
		Some(_)                     => Err(format!("\"{name}\" should be a string")),
		_                           => Err("missing string \"{name}\"".to_string()),
	};
}