todotxt_tui/config/
traits.rs1use anyhow::{Context, Result};
2use clap::{builder::Styles, ArgMatches};
3use std::{
4 ffi::OsString,
5 fs::File,
6 io::{Read, Write},
7 path::{Path, PathBuf},
8};
9
10pub trait Conf: Sized + Default {
11 fn from_file(path: impl AsRef<Path>) -> Result<Self> {
12 Self::from_reader(
13 File::open(path.as_ref()).with_context(|| format!("{:?}", path.as_ref()))?,
14 )
15 }
16
17 fn from_reader<R>(reader: R) -> Result<Self>
18 where
19 R: Read;
20
21 fn parse<Iter, T, R>(iter: Iter, reader: R) -> Result<Self>
22 where
23 Iter: IntoIterator<Item = T>,
24 T: Into<OsString> + Clone,
25 R: Read;
26}
27
28pub trait ConfMerge: Sized + ConfigDefaults + Conf {
29 fn from_args<Iter, T>(iter: Iter) -> Result<Self>
30 where
31 Iter: IntoIterator<Item = T>,
32 T: Into<OsString> + Clone;
33
34 fn export_default(path: impl AsRef<Path>) -> Result<()> {
35 let mut output =
36 std::fs::File::create(path.as_ref()).with_context(|| format!("{:?}", path.as_ref()))?;
37 write!(output, "{}", Self::default_toml()?)?;
38 Ok(())
39 }
40
41 fn configured_toml(path: impl AsRef<Path>, matches: &ArgMatches) -> Result<String>;
42
43 fn default_toml() -> Result<String>;
44
45 fn autocomplete(writer: &mut impl std::io::Write) -> Result<()>;
46}
47
48pub trait ConfigDefaults {
49 fn config_path() -> PathBuf;
50
51 fn help_colors() -> Styles;
52}
53
54pub trait ExportConf {
55 fn export(&self, config_path: impl AsRef<Path>, matches: &ArgMatches) -> Result<()>;
56}