load config

pull/14/head
dvkt 4 years ago
parent 2b473719ce
commit 409b9218a5

@ -1,7 +1,7 @@
use crate::phetchdir;
use std::io::{Read, Result};
// Bookmarks only work if you've created a ~/.config/phetch/ manually.
/// Bookmarks only work if you've created a ~/.config/phetch/ manually.
const BOOKMARKS_FILE: &str = "bookmarks.gph";
macro_rules! dir_missing_fmt {

@ -1,5 +1,13 @@
use std::{collections::HashMap, io::Result};
use crate::phetchdir;
use std::{
collections::HashMap,
io::{Read, Result},
};
/// phetch will look for this file on load.
const CONFIG_FILE: &str = "phetch.conf";
/// Default config. Currently only used internally.
const DEFAULT_CONFIG: &str = "
## config file for the phetch gopher client
## gopher://phkt.io/1/phetch
@ -23,10 +31,29 @@ pub struct Config {
impl Default for Config {
fn default() -> Self {
parse(DEFAULT_CONFIG).expect("Syntax error in internal default config")
parse(DEFAULT_CONFIG).expect("Syntax error in config file")
}
}
/// Returns the config phetch uses when launched with no flags or
/// config file modification.
pub fn default() -> Config {
Default::default()
}
/// Attempt to load config from disk, specifically `CONFIG_FILE`.
pub fn load() -> Result<Config> {
let mut reader = phetchdir::load(CONFIG_FILE)?;
let mut file = String::new();
reader.read_to_string(&mut file)?;
parse(&file)
}
/// Does the config file exist?
pub fn exists() -> bool {
phetchdir::exists(CONFIG_FILE)
}
/// Parses a phetch config file into a Config struct.
pub fn parse(text: &str) -> Result<Config> {
let mut linenum = 0;

@ -8,6 +8,15 @@ use std::{
/// If you want the full, expanded path, use `path()`.
pub const DIR: &str = "~/.config/phetch/";
/// Check if a file exists within the phetchdir.
pub fn exists(filename: &str) -> bool {
if let Ok(path) = path() {
path.join(filename).exists()
} else {
false
}
}
/// Loads a file from the phetchdir for reading.
pub fn load(filename: &str) -> Result<BufReader<File>> {
path().and_then(|dotdir| {

Loading…
Cancel
Save