Skip to content

Commit

Permalink
Move custom_pages_dir business logic from RawConfig to Config
Browse files Browse the repository at this point in the history
This also introduces the path source information for that directory,
which was previously unknonw.
  • Loading branch information
dbrgn committed Jun 17, 2022
1 parent 2ae8588 commit 477eed0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
46 changes: 28 additions & 18 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
convert::TryFrom,
env, fs,
env, fmt, fs,
io::{Read, Write},
path::PathBuf,
time::Duration,
Expand Down Expand Up @@ -162,28 +162,14 @@ impl Default for RawUpdatesConfig {
}
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
struct RawDirectoriesConfig {
#[serde(default)]
pub cache_dir: Option<PathBuf>,
#[serde(default)]
pub custom_pages_dir: Option<PathBuf>,
}

impl Default for RawDirectoriesConfig {
fn default() -> Self {
Self {
cache_dir: None,
custom_pages_dir: get_app_root(AppDataType::UserData, &crate::APP_INFO)
.map(|path| {
// Note: The `join("")` call ensures that there's a trailing slash
path.join("pages").join("")
})
.ok(),
}
}
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(default)]
struct RawConfig {
Expand Down Expand Up @@ -246,10 +232,16 @@ pub struct PathWithSource {
pub source: PathSource,
}

impl fmt::Display for PathWithSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} ({})", self.path.display(), self.source)
}
}

#[derive(Clone, Debug, PartialEq)]
pub struct DirectoriesConfig {
pub cache_dir: PathWithSource,
pub custom_pages_dir: Option<PathBuf>,
pub custom_pages_dir: Option<PathWithSource>,
}

#[derive(Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -318,9 +310,27 @@ impl TryFrom<RawConfig> for Config {
// If everything fails, give up
bail!("Could not determine user cache directory");
};
let custom_pages_dir = raw_config
.directories
.custom_pages_dir
.map(|path| PathWithSource {
path,
source: PathSource::OsConvention,
})
.or_else(|| {
get_app_root(AppDataType::UserData, &crate::APP_INFO)
.map(|path| {
// Note: The `join("")` call ensures that there's a trailing slash
PathWithSource {
path: path.join("pages").join(""),
source: PathSource::ConfigFile,
}
})
.ok()
});
let directories = DirectoriesConfig {
cache_dir,
custom_pages_dir: raw_config.directories.custom_pages_dir,
custom_pages_dir,
};

Ok(Self {
Expand Down
23 changes: 10 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,24 +161,17 @@ fn show_paths(config: &Config) {
|e| format!("[Error: {}]", e),
|(path, _)| path.display().to_string(),
);
let cache_dir = format!(
"{} ({})",
config.directories.cache_dir.path.display(),
config.directories.cache_dir.source
);
let cache_dir = config.directories.cache_dir.to_string();
let pages_dir = {
let mut path = config.directories.cache_dir.path.clone();
path.push(TLDR_PAGES_DIR);
path.push(""); // Trailing path separator
path.display().to_string()
};
let custom_pages_dir = config.directories.custom_pages_dir.as_deref().map_or_else(
|| "[None]".to_string(),
|path| {
path.to_str()
.map_or_else(|| "[Invalid]".to_string(), ToString::to_string)
},
);
let custom_pages_dir = match config.directories.custom_pages_dir {
None => "[None]".to_string(),
Some(ref path_with_source) => path_with_source.to_string(),
};
println!("Config dir: {}", config_dir);
println!("Config path: {}", config_path);
println!("Cache dir: {}", cache_dir);
Expand Down Expand Up @@ -381,7 +374,11 @@ fn main() {
if let Some(lookup_result) = cache.find_page(
&command,
&languages,
config.directories.custom_pages_dir.as_deref(),
config
.directories
.custom_pages_dir
.as_ref()
.map(|path_with_source| path_with_source.path.as_ref()),
) {
if let Err(ref e) =
print_page(&lookup_result, args.raw, enable_styles, args.pager, &config)
Expand Down

0 comments on commit 477eed0

Please sign in to comment.