mithril_client_cli/utils/
mod.rs

1//! Utilities module
2//! This module contains tools needed for the commands layer.
3
4mod archive_unpacker;
5mod cardano_db;
6mod cardano_db_download_checker;
7mod expander;
8mod feedback_receiver;
9mod forced_era_fetcher;
10mod fs;
11mod github_release_retriever;
12mod http_downloader;
13mod multi_download_progress_reporter;
14mod progress_reporter;
15
16pub use archive_unpacker::*;
17pub use cardano_db::*;
18pub use cardano_db_download_checker::*;
19pub use expander::*;
20pub use feedback_receiver::*;
21pub use forced_era_fetcher::*;
22pub use fs::*;
23pub use github_release_retriever::*;
24pub use http_downloader::*;
25pub use multi_download_progress_reporter::*;
26pub use progress_reporter::*;
27
28use anyhow::Context;
29use mithril_client::MithrilResult;
30use std::path::Path;
31
32/// The key used to store the caution message when printing a JSON directly to stderr
33pub(crate) const JSON_CAUTION_KEY: &str = "caution";
34
35/// Prints a simple warning message to stderr
36///
37/// The message should be no more than one or two lines, else the json output would be too large,
38/// and it would be best to break it down in a structured object. (See `warn_fast_bootstrap_not_available`
39/// in cardano db download command for an example.)
40pub(crate) fn print_simple_warning(message: &str, is_json_output_enabled: bool) {
41    if is_json_output_enabled {
42        eprintln!(r#"{{"{JSON_CAUTION_KEY}":"{message}"}}"#);
43    } else {
44        eprintln!("Warning: {message}");
45    }
46}
47
48/// Converts a [Path] to a [String], returning an error if the path is not valid UTF-8.
49pub(crate) fn path_to_string(path: &Path) -> MithrilResult<String> {
50    let path = path
51        .to_str()
52        .with_context(|| {
53            format!(
54                "Path '{}' contains invalid UTF-8 characters.",
55                path.display()
56            )
57        })?
58        .to_string();
59
60    Ok(path)
61}