• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

voiceapiai / ralertsinua / 9159157501

20 May 2024 01:04PM UTC coverage: 28.724% (-0.5%) from 29.214%
9159157501

Pull #12

github

web-flow
Merge 488d7d60e into 863e03c21
Pull Request #12: fix(deps): cross-compile needs musl target, introduce dockerfile & ttyd

0 of 61 new or added lines in 5 files covered. (0.0%)

15 existing lines in 1 file now uncovered.

565 of 1967 relevant lines covered (28.72%)

4.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/utils.rs
1
use directories::ProjectDirs;
2
use lazy_static::lazy_static;
3
// use serde::de::DeserializeOwned;
4
// use serde::Serialize;
5
// use std::marker::PhantomData;
6
use std::path::PathBuf;
7
use std::{any::type_name, env};
8
use tracing_error::ErrorLayer;
9
use tracing_subscriber::{
10
    self, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt,
11
};
12

13
use crate::error::*;
14

15
type Result<T> = miette::Result<T, AppError>;
16

17
const VERSION_MESSAGE: &str = concat!(
18
    env!("CARGO_PKG_VERSION"),
19
    "-",
20
    env!("VERGEN_CARGO_DEBUG"),
21
    "-",
22
    env!("VERGEN_GIT_DESCRIBE"),
23
    "-",
24
    "(",
25
    env!("VERGEN_BUILD_DATE"),
26
    ")",
27
    "-",
28
    env!("VERGEN_CARGO_TARGET_TRIPLE"),
29
);
30

31
lazy_static! {
32
    pub static ref PROJECT_NAME: String =
33
        env!("CARGO_CRATE_NAME").to_uppercase().to_string();
34
    pub static ref DATA_FOLDER: Option<PathBuf> =
35
        env::var(format!("{}_DATA", PROJECT_NAME.clone()))
36
            .ok()
37
            .map(PathBuf::from);
38
    pub static ref CONFIG_FOLDER: Option<PathBuf> =
39
        env::var(format!("{}_CONFIG", PROJECT_NAME.clone()))
40
            .ok()
41
            .map(PathBuf::from);
42
    pub static ref LOG_ENV: String = format!("{}_LOGLEVEL", PROJECT_NAME.clone());
43
    pub static ref LOG_FILE: String = format!("{}.log", env!("CARGO_PKG_NAME"));
44
}
45

UNCOV
46
fn project_directory() -> Option<ProjectDirs> {
×
47
    ProjectDirs::from("com", "kdheepak", env!("CARGO_PKG_NAME"))
×
48
}
×
49

50
pub fn initialize_panic_handler() -> Result<()> {
×
51
    miette::set_hook(Box::new(|_| {
×
52
        Box::new(
×
53
            miette::MietteHandlerOpts::default()
×
54
                .terminal_links(true)
×
55
                .force_graphical(true)
×
56
                .context_lines(4)
×
57
                .tab_width(4)
×
58
                .break_words(false)
×
59
                .color(true)
×
60
                .build(),
×
UNCOV
61
        )
×
62
    }))
×
63
    .map_err(|e| AppError::Unknown)?;
×
64

65
    miette::set_panic_hook();
×
UNCOV
66

×
67
    Ok(())
×
68
}
×
69

UNCOV
70
pub fn get_local_data_dir() -> PathBuf {
×
71
    PathBuf::from(".").join(".data")
×
72
}
×
73

74
pub fn get_data_dir() -> PathBuf {
×
75
    let directory = if let Some(s) = DATA_FOLDER.clone() {
×
UNCOV
76
        s
×
77
    } else if let Some(proj_dirs) = project_directory() {
×
UNCOV
78
        proj_dirs.data_local_dir().to_path_buf()
×
79
    } else {
80
        PathBuf::from(".").join(".data")
×
81
    };
UNCOV
82
    directory
×
83
}
×
84

85
/// Returns the path to the project's local config directory
86
pub fn get_config_dir() -> PathBuf {
×
87
    let directory = if let Some(s) = CONFIG_FOLDER.clone() {
×
UNCOV
88
        s
×
89
    } else if let Some(proj_dirs) = project_directory() {
×
UNCOV
90
        proj_dirs.config_local_dir().to_path_buf()
×
91
    } else {
92
        PathBuf::from(".").join(".config")
×
93
    };
94
    directory
×
95
}
×
96

NEW
97
pub fn initialize_logging(log_path: Option<impl Into<PathBuf>>) -> Result<()> {
×
NEW
98
    let disable_file_logging: bool = log_path.is_none();
×
NEW
99

×
NEW
100
    if log_path.is_some() {
×
NEW
101
        let log_path: PathBuf = log_path.unwrap().into();
×
102
        // let directory = get_data_dir();
103
        // std::fs::create_dir_all(directory.clone())?;
104
        // let log_path: PathBuf = directory.join(LOG_FILE.clone());
NEW
105
        let log_file = std::fs::File::create(log_path)?;
×
106

NEW
107
        let file_logger = tracing_subscriber::fmt::layer()
×
NEW
108
            .with_file(true)
×
NEW
109
            .with_line_number(true)
×
NEW
110
            .with_writer(log_file)
×
NEW
111
            .with_target(false)
×
NEW
112
            .with_ansi(false);
×
NEW
113

×
NEW
114
        tracing_subscriber::registry()
×
NEW
115
            .with(file_logger)
×
NEW
116
            .with(ErrorLayer::default())
×
NEW
117
            .with(tui_logger::tracing_subscriber_layer())
×
NEW
118
            .init();
×
NEW
119
    } else {
×
NEW
120
        tracing_subscriber::registry()
×
NEW
121
            .with(ErrorLayer::default())
×
NEW
122
            .with(tui_logger::tracing_subscriber_layer())
×
NEW
123
            .init();
×
NEW
124
    }
×
125

UNCOV
126
    Ok(())
×
UNCOV
127
}
×
128

129
/// Similar to the `std::dbg!` macro, but generates `tracing` events rather
130
/// than printing to stdout.
131
///
132
/// By default, the verbosity level for the generated events is `DEBUG`, but
133
/// this can be customized.
134
#[macro_export]
135
macro_rules! trace_dbg {
136
    (target: $target:expr, level: $level:expr, $ex:expr) => {{
137
        match $ex {
138
            value => {
139
                tracing::event!(target: $target, $level, ?value, stringify!($ex));
140
                value
141
            }
142
        }
143
    }};
144
    (level: $level:expr, $ex:expr) => {
145
        trace_dbg!(target: module_path!(), level: $level, $ex)
146
    };
147
    (target: $target:expr, $ex:expr) => {
148
        trace_dbg!(target: $target, level: tracing::Level::DEBUG, $ex)
149
    };
150
    ($ex:expr) => {
151
        trace_dbg!(level: tracing::Level::DEBUG, $ex)
152
    };
153
}
154

155
pub fn version() -> String {
×
156
    let author = clap::crate_authors!();
×
157

×
158
    // let current_exe_path = PathBuf::from(clap::crate_name!()).display().to_string();
×
159
    let config_dir_path = get_config_dir().display().to_string();
×
160
    let data_dir_path = get_data_dir().display().to_string();
×
161

×
162
    format!(
×
163
        "\
×
164
{VERSION_MESSAGE}
×
165

×
166
Authors: {author}
×
167

×
168
Config directory: {config_dir_path}
×
UNCOV
169
Data directory: {data_dir_path}"
×
170
    )
×
171
}
×
172

UNCOV
173
pub fn type_of<T>(_: T) -> &'static str {
×
174
    type_name::<T>().split("::").last().unwrap()
×
175
}
×
176

177
#[inline]
NEW
178
pub fn str_to_bool(s: impl Into<String>) -> bool {
×
NEW
179
    match s.into().to_lowercase().as_str() {
×
NEW
180
        "true" | "1" => true,
×
NEW
UNCOV
181
        "false" | "0" => false,
×
NEW
UNCOV
182
        _ => false,
×
183
    }
NEW
184
}
×
185

186
/* use std::{collections::HashMap, hash::Hash};
187

188
pub fn memoize<A, B, F>(f: F) -> impl FnMut(A) -> B
189
where
190
    A: Eq + Hash + Clone,
191
    B: Clone,
192
    F: Fn(A) -> B,
193
{
194
    let mut cache = HashMap::new();
195
    move |x| (*cache.entry(x.clone()).or_insert_with(|| f(x))).clone()
196
} */
197

198
/*
199
pub struct SerdeCacache<D, K>
200
where
201
    D: Serialize + DeserializeOwned,
202
    K: AsRef<str>,
203
{
204
    name: PathBuf,
205
    _phantom_data: PhantomData<D>,
206
    _phantom_key: PhantomData<K>,
207
}
208

209
impl<D, K> SerdeCacache<D, K>
210
where
211
    D: Serialize + DeserializeOwned,
212
    K: AsRef<str>,
213
{
214
    // Set an item in the cache
215
    pub async fn set(&self, key: K, data: &D) -> Result<cacache::Integrity> {
216
        let serialized = rmp_serde::to_vec(data)?;
217
        Ok(cacache::write(&self.name, key, serialized).await?)
218
    }
219

220
    // Get an item from the cache
221
    pub async fn get(&self, key: K) -> Result<D> {
222
        let read = cacache::read(&self.name, key).await?;
223
        Ok(rmp_serde::from_slice(&read)?)
224
    }
225
}
226
 */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc