wick_xdg/
lib.rs

1//! Cross-platform normalization of directories and other configuration related to Wick.
2
3// !!START_LINTS
4// Wick lints
5// Do not change anything between the START_LINTS and END_LINTS line.
6// This is automatically generated. Add exceptions after this section.
7#![allow(unknown_lints)]
8#![deny(
9  clippy::await_holding_lock,
10  clippy::borrow_as_ptr,
11  clippy::branches_sharing_code,
12  clippy::cast_lossless,
13  clippy::clippy::collection_is_never_read,
14  clippy::cloned_instead_of_copied,
15  clippy::cognitive_complexity,
16  clippy::create_dir,
17  clippy::deref_by_slicing,
18  clippy::derivable_impls,
19  clippy::derive_partial_eq_without_eq,
20  clippy::equatable_if_let,
21  clippy::exhaustive_structs,
22  clippy::expect_used,
23  clippy::expl_impl_clone_on_copy,
24  clippy::explicit_deref_methods,
25  clippy::explicit_into_iter_loop,
26  clippy::explicit_iter_loop,
27  clippy::filetype_is_file,
28  clippy::flat_map_option,
29  clippy::format_push_string,
30  clippy::fn_params_excessive_bools,
31  clippy::future_not_send,
32  clippy::get_unwrap,
33  clippy::implicit_clone,
34  clippy::if_then_some_else_none,
35  clippy::impl_trait_in_params,
36  clippy::implicit_clone,
37  clippy::inefficient_to_string,
38  clippy::inherent_to_string,
39  clippy::iter_not_returning_iterator,
40  clippy::large_types_passed_by_value,
41  clippy::large_include_file,
42  clippy::let_and_return,
43  clippy::manual_assert,
44  clippy::manual_ok_or,
45  clippy::manual_split_once,
46  clippy::manual_let_else,
47  clippy::manual_string_new,
48  clippy::map_flatten,
49  clippy::map_unwrap_or,
50  clippy::missing_enforced_import_renames,
51  clippy::missing_assert_message,
52  clippy::missing_const_for_fn,
53  clippy::must_use_candidate,
54  clippy::mut_mut,
55  clippy::needless_for_each,
56  clippy::needless_option_as_deref,
57  clippy::needless_pass_by_value,
58  clippy::needless_collect,
59  clippy::needless_continue,
60  clippy::non_send_fields_in_send_ty,
61  clippy::nonstandard_macro_braces,
62  clippy::option_if_let_else,
63  clippy::option_option,
64  clippy::rc_mutex,
65  clippy::redundant_else,
66  clippy::same_name_method,
67  clippy::semicolon_if_nothing_returned,
68  clippy::str_to_string,
69  clippy::string_to_string,
70  clippy::too_many_lines,
71  clippy::trivially_copy_pass_by_ref,
72  clippy::trivial_regex,
73  clippy::try_err,
74  clippy::unnested_or_patterns,
75  clippy::unused_async,
76  clippy::unwrap_or_else_default,
77  clippy::useless_let_if_seq,
78  bad_style,
79  clashing_extern_declarations,
80  dead_code,
81  deprecated,
82  explicit_outlives_requirements,
83  improper_ctypes,
84  invalid_value,
85  missing_copy_implementations,
86  missing_debug_implementations,
87  mutable_transmutes,
88  no_mangle_generic_items,
89  non_shorthand_field_patterns,
90  overflowing_literals,
91  path_statements,
92  patterns_in_fns_without_body,
93  private_in_public,
94  trivial_bounds,
95  trivial_casts,
96  trivial_numeric_casts,
97  type_alias_bounds,
98  unconditional_recursion,
99  unreachable_pub,
100  unsafe_code,
101  unstable_features,
102  unused,
103  unused_allocation,
104  unused_comparisons,
105  unused_import_braces,
106  unused_parens,
107  unused_qualifications,
108  while_true,
109  missing_docs
110)]
111#![warn(clippy::exhaustive_enums)]
112#![allow(unused_attributes, clippy::derive_partial_eq_without_eq, clippy::box_default)]
113// !!END_LINTS
114// Add exceptions here
115#![allow()]
116
117mod directories;
118mod directory;
119mod error;
120mod file;
121
122use std::path::PathBuf;
123
124pub use directories::Directories;
125pub use error::Error;
126
127#[derive(Debug, Clone, getset::Getters)]
128#[cfg_attr(feature = "serde", derive(serde::Serialize))]
129/// A one-stop shop for all the environment-specific configuration for the Wick project.
130pub struct Settings {
131  /// Local directories for wick to store and retrieve files.
132  #[getset(get = "pub")]
133  local: Directories,
134  /// Global directories for wick to store and retrieve files.
135  #[getset(get = "pub")]
136  global: Directories,
137  /// The global state directory wick uses to store data while running.
138  #[cfg_attr(feature = "serde", serde(serialize_with = "display_path"))]
139  #[getset(get = "pub")]
140  data: PathBuf,
141  /// The location to look for user configuration.
142  #[cfg_attr(feature = "serde", serde(serialize_with = "display_path"))]
143  #[getset(get = "pub")]
144  config_dir: PathBuf,
145  /// The basename of the user configuration file.
146  #[getset(get = "pub")]
147  configfile_basename: String,
148}
149#[cfg(feature = "serde")]
150fn display_path<S>(value: impl AsRef<std::path::Path>, serializer: S) -> Result<S::Ok, S::Error>
151where
152  S: serde::Serializer,
153{
154  serializer.collect_str(value.as_ref().to_string_lossy().as_ref())
155}
156
157impl Default for Settings {
158  fn default() -> Self {
159    Self::new()
160  }
161}
162
163impl Settings {
164  #[must_use]
165  ///
166  pub fn new() -> Self {
167    Self {
168      local: Directories::new(&directory::relative_root()),
169      global: Directories::new(&directory::global_root()),
170      data: directory::global_data_dir(),
171      config_dir: directory::user_config_dir(),
172      configfile_basename: file::CONFIG_FILE_NAME.to_owned(),
173    }
174  }
175
176  #[must_use]
177  /// Returns the local directories if condition is true, otherwise returns the global directories.
178  pub const fn local_if(&self, condition: bool) -> &Directories {
179    if condition {
180      &self.local
181    } else {
182      &self.global
183    }
184  }
185}