comfy_print/config/
env_vars.rs

1//! Functions for loading [comfy_print](crate::config)'s global config variables from the environment.
2//! The key for each config is on a const string named `ENV_NAME` in its respective module. Example: [MAX_RETRIES](max_retries::ENV_NAME).
3//! Configs are not automatically loaded, you must call [load_all](load_all) to load them. But they do have default values.
4
5use std::env::VarError;
6use std::str::FromStr;
7use super::*;
8use crate::config::on_max_retries_reached::On_MaxRetriesReached;
9use crate::config::on_queue_full::On_QueueFull;
10use crate::config::on_queue_printing_fail::On_QueuePrintingFail;
11
12/// Errors that can occur when loading a global config variable from the environment.
13#[derive(Debug)]
14pub enum LoadVarError<T: FromStr> {
15	/// Errors returned from [std::env::var](std::env::var).
16	VarError(VarError),
17	/// Environment variable had a value but it could not be [parsed](FromStr).
18	ParseError(T::Err),
19	/// Errors returned from [std::fs::File::open](std::fs::File::open).
20	IOError(std::io::Error),
21}
22
23/// Results for attempting to load each of [comfy_print](crate::config)'s configs from the environment.
24pub struct LoadVarsResult {
25	/// See [MAX_RETRIES](max_retries).
26	pub max_retries: Result<usize, LoadVarError<usize>>,
27	/// See [MAX_QUEUE_LENGTH](max_queue_length).
28	pub max_queue_length: Result<usize, LoadVarError<usize>>,
29	/// See [ALLOW_LOGGING_PRINT_FAILURES](allow_logging_print_failures).
30	pub allow_logging_print_failures: Result<bool, LoadVarError<bool>>,
31	/// See [ON_RETRY_PRINTING_FAIL](on_queue_printing_fail).
32	pub on_retry_printing_fail: Result<On_QueuePrintingFail, LoadVarError<On_QueuePrintingFail>>,
33	/// See [ON_MAX_RETRIES_REACHED](on_max_retries_reached).
34	pub on_max_retries_reached: Result<On_MaxRetriesReached, LoadVarError<On_MaxRetriesReached>>,
35	/// See [LOG_IO_PATH](log_io_path).
36	pub log_io_path: Result<String, LoadVarError<String>>,
37	/// See [ON_QUEUE_FULL](on_queue_full).
38	pub on_push_queue_full: Result<On_QueueFull, LoadVarError<On_QueueFull>>,
39}
40
41
42/// Attempts to load all global config variables from the [environment](std::env). [comfy_print](crate::config)'s global config variables will be replaced by any values found in the environment.
43pub fn load_all() -> LoadVarsResult {
44	let max_retries = get_var::<usize>(max_retries::ENV_NAME)
45			.inspect(|new_value| max_retries::set(*new_value));
46
47	let max_queue_length = get_var::<usize>(max_queue_length::ENV_NAME)
48			.inspect(|new_value| max_queue_length::set(*new_value));
49
50	let allow_logging_print_failures = get_var::<bool>(allow_logging_print_failures::ENV_NAME)
51			.inspect(|new_value| allow_logging_print_failures::set(*new_value));
52
53	let on_retry_printing_fail = get_var::<On_QueuePrintingFail>(on_queue_printing_fail::ENV_NAME)
54			.inspect(|new_value| on_queue_printing_fail::set(*new_value));
55
56	let on_max_retries_reached = get_var::<On_MaxRetriesReached>(on_max_retries_reached::ENV_NAME)
57			.inspect(|new_value| on_max_retries_reached::set(*new_value));
58
59	let mut log_io_path: Result<String, LoadVarError<String>> = get_var::<String>(log_io_path::ENV_NAME);
60
61	if let Ok(path) = &mut log_io_path {
62		match log_io_path::set(path.as_str()) {
63			Ok(_) => {},
64			Err(err) => {
65				log_io_path = Err(LoadVarError::<String>::IOError(err));
66			},
67		}
68	}
69
70	let on_push_queue_full = get_var::<On_QueueFull>(on_queue_full::ENV_NAME)
71			.inspect(|new_value| on_queue_full::set(*new_value));
72
73	return LoadVarsResult {
74		max_retries,
75		max_queue_length,
76		allow_logging_print_failures,
77		on_retry_printing_fail,
78		on_max_retries_reached,
79		log_io_path,
80		on_push_queue_full,
81	};
82
83	fn get_var<T: FromStr>(var_name: &'static str) -> Result<T, LoadVarError<T>> {
84		return match std::env::var(var_name) {
85			Ok(value) =>
86				match value.parse::<T>() {
87					Ok(value) => Ok(value),
88					Err(err) => Err(LoadVarError::ParseError(err)),
89				},
90			Err(err) => Err(LoadVarError::VarError(err)),
91		}
92	}
93}