tomcat/src/main.rs

71 lines
2.1 KiB
Rust

// Copyright (c) 2022 Emma Tebibyte
// SPDX-License-Identifier: AGPL-3.0-or-later
/* Tomcat 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.
*
* Tomcat 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;
use std::fs::File;
use std::io;
use std::io::Read;
use std::path::Path;
use std::process;
use toml::Value;
fn main() {
let mut arguments: Vec<String> = env::args().collect();
let argv0 = arguments.remove(0);
let input = &arguments[1];
let mut content = String::new();
let mut file = Path::new(&input);
if file.is_file() {
File::open(file).unwrap().read_to_string(&mut content).unwrap();
} else { content = input.to_string(); }
let keyval: Vec<&str> = arguments[0].split(".").collect();
let toml = content.parse::<Value>().unwrap();
let mut table = &toml;
if ! keyval[0].is_empty() {
match toml.get(keyval[0]) {
Some(table) => {
if keyval[1].is_empty() {
println!("{}", table.as_str().unwrap());
} else {
match table.get(keyval[1]) {
Some(value) => {
let out = value
.as_str()
.unwrap()
.trim_start_matches('"')
.trim_end_matches('"');
println!("{}", out);
},
None => {
eprintln!("{}: {}: No such key.", argv0, keyval[1]);
process::exit(1);
},
};
}
},
None => {
eprintln!("{}: {}: No such table.", argv0, keyval[0]);
process::exit(1);
},
};
}
}