aboutsummaryrefslogtreecommitdiff
path: root/config.ha
blob: ba454f08160efaa0d7179cb22c4039ee1e7eae66 (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
use time::date;
use fmt;
use dirs;
use format::ini;
use time::chrono;
use path;
use io;
use fs;
use os;
use errors;
use strings;

type config = struct {
	tz: date::locality,
};

type noexist = !str;

type conferror = !(path::error | noexist | fs::error | date::tzdberror | nomem);

fn loadconfig() (config | conferror) = {
	const confdir = dirs::config("distamp");
	const buf = path::init()?;
	const confpath = path::push(&buf, confdir, "config.ini")?;
	const f = match (os::open(confpath)) {
	case let e: errors::noentry =>
		fmt::errorln("warn: No config file found")!;
		return config {
			tz = date::LOCAL,
		};
	case let e: fs::error =>
		return e;
	case let f: io::file =>
		yield f;
	};
	defer io::close(f)!;
	const sc = ini::scan(f);

	let tz: date::locality= date::LOCAL;
	for (const e => ini::next(&sc)!) {
		const key = strings::trim(e.1);
		const val = strings::trim(e.2);
		if (key == "tz") {
			tz = date::tzdb(val)?;
		};
	};
	return config {
		tz = tz,
	};
};

fn strconferror(e: conferror) str = {
	match (e) {
	case let e: path::error =>
		return path::strerror(e);
	case let e: fs::error =>
		return fs::strerror(e);
	case let e: date::tzdberror=>
		return date::strerror(e);
	};
};