This repository has been archived on 2023-12-14. You can view files and clone it, but cannot push or open issues or pull requests.
plaque/src/config.rs

131 lines
3.2 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
* SPDX-License-Identifier: AGPL-3.0-or-later
*
* This file is part of Plaque.
*
* Plaque 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.
*
* Plaque 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 this program. If not, see https://www.gnu.org/licenses/.
*/
use std::{
env,
fs::File,
io::Read,
};
use charsets::Charset;
use isolang::Language;
use serde::Deserialize;
use serde_with::{ serde_as, DisplayFromStr };
use toml::{ de::ValueDeserializer, Value };
use yacexits::*;
#[derive(Deserialize)]
pub struct Config {
pub content_dir: String,
pub output_dir: String,
pub template_dir: String,
}
#[serde_as]
#[derive(Deserialize)]
pub struct PageMeta {
pub author: String,
pub date: String,
#[serde_as(as = "DisplayFromStr")]
pub encoding: Charset,
pub lang: Language,
pub template: String,
}
pub fn get_path(xdg: &str) -> Result<String, (String, u32)> {
let dir: String;
if unsafe { libc::geteuid() } == 0 {
dir = match xdg {
"XDG_CACHE_HOME" => "/var/cms".to_owned(),
"XDG_CONFIG_HOME" => "/etc/cms".to_owned(),
_ => panic!("nya"),
};
} else {
dir = match env::var(xdg) {
Ok(val) => val,
Err(_) => {
match env::var("HOME") {
Ok(home) => format!("{}/.config/", home),
Err(_) => {
return Err((
"Unable to determine path to current users home directory."
.to_string(),
EX_UNAVAILABLE
));
},
}
},
};
}
match xdg {
"XDG_CACHE_HOME" => Ok(format!("{}/cms", dir)),
"XDG_CONFIG_HOME" => Ok(format!("{}/cms", dir)),
_ => panic!("nya"),
}
}
impl Config {
pub fn read_config(config_path: String) -> Result<Self, (String, u32)> {
let mut buf: Vec<u8> = Vec::new();
let mut config_file = match File::open(
format!("{}/config.toml", config_path)
) {
Ok(file) => file,
Err(err) => return Err((format!("{:?}", err), 1)),
};
match config_file.read_to_end(&mut buf) {
Ok(_) => {},
Err(err) => return Err((format!("{:?}", err), 1)),
};
let toml = String::from_utf8(buf).unwrap();
match Config::deserialize(ValueDeserializer::new(&toml)) {
Ok(val) => Ok(val),
Err(err) => Err((format!("{:?}", err), 1)),
}
}
}
impl PageMeta {
pub fn read_meta(toml: Value) -> Result<Self, (String, u32)> {
let input: String = match toml.try_into() {
Ok(val) => val,
Err(_) => {
return Err((format!("Failed to read page metadata."), EX_DATAERR));
},
};
match Self::deserialize(ValueDeserializer::new(&input)) {
Ok(val) => Ok(val),
Err(_) => {
Err(("Could not deserialized TOML.".to_owned(), EX_DATAERR))
},
}
}
}