1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use std::{
    ffi::OsStr,
    fmt::Debug,
    fs, io,
    path::{Path, PathBuf},
};

#[cfg(feature = "azure_blob")]
use crate::cfg::AzureBlobCfg;
use crate::{
    cfg::{Cfg, PyHttpReaderCfg, SshCfg},
    rverr,
    tools_data::bbox_data,
};
use crate::{
    result::{to_rv, RvResult},
    tools_data::BboxExportData,
};
use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};
use std::str::FromStr;

lazy_static! {
    pub static ref DEFAULT_TMPDIR: PathBuf = std::env::temp_dir().join("rvimage");
}
lazy_static! {
    pub static ref DEFAULT_HOMEDIR: PathBuf = match dirs::home_dir() {
        Some(p) => p.join(".rvimage"),
        _ => std::env::temp_dir().join("rvimage"),
    };
}

pub const RVPRJ_PREFIX: &str = "rvprj_";

pub fn read_to_string<P>(p: P) -> RvResult<String>
where
    P: AsRef<Path> + Debug,
{
    fs::read_to_string(&p).map_err(|e| rverr!("could not read {:?} due to {:?}", p, e))
}
pub trait PixelEffect: FnMut(u32, u32) {}
impl<T: FnMut(u32, u32)> PixelEffect for T {}
pub fn filename_in_tmpdir(path: &str, tmpdir: &str) -> RvResult<String> {
    let path = PathBuf::from_str(path).unwrap();
    let fname = osstr_to_str(path.file_name()).map_err(to_rv)?;
    Path::new(tmpdir)
        .join(fname)
        .to_str()
        .map(|s| s.to_string())
        .ok_or_else(|| rverr!("could not transform {:?} to &str", fname))
}

pub fn path_to_str(p: &Path) -> RvResult<&str> {
    osstr_to_str(Some(p.as_os_str()))
        .map_err(|e| rverr!("could not transform '{:?}' due to '{:?}'", p, e))
}

pub fn osstr_to_str(p: Option<&OsStr>) -> io::Result<&str> {
    p.ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, format!("{p:?} not found")))?
        .to_str()
        .ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                format!("{p:?} not convertible to unicode"),
            )
        })
}

pub fn to_stem_str(p: &Path) -> RvResult<&str> {
    osstr_to_str(p.file_stem())
        .map_err(|e| rverr!("could not transform '{:?}' due to '{:?}'", p, e))
}

pub fn to_name_str(p: &Path) -> RvResult<&str> {
    osstr_to_str(p.file_name())
        .map_err(|e| rverr!("could not transform '{:?}' due to '{:?}'", p, e))
}
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, Eq)]
pub enum ConnectionData {
    Ssh(SshCfg),
    PyHttp(PyHttpReaderCfg),
    #[cfg(feature = "azure_blob")]
    AzureBlobCfg(AzureBlobCfg),
    #[default]
    None,
}
#[derive(Clone, Default, PartialEq, Eq)]
pub struct MetaData {
    pub file_path: Option<String>,
    pub connection_data: ConnectionData,
    pub ssh_cfg: Option<SshCfg>,
    pub opened_folder: Option<String>,
    pub export_folder: Option<String>,
    pub is_loading_screen_active: Option<bool>,
}
impl MetaData {
    pub fn from_filepath(file_path: String) -> Self {
        MetaData {
            file_path: Some(file_path),
            connection_data: ConnectionData::None,
            ssh_cfg: None,
            opened_folder: None,
            export_folder: None,
            is_loading_screen_active: None,
        }
    }
}

pub const DEFAULT_PRJ_NAME: &str = "default";
pub fn is_prjname_set(prj_name: &str) -> bool {
    prj_name != DEFAULT_PRJ_NAME
}

pub fn make_prjcfg_filename(prj_name: &str) -> String {
    if prj_name.starts_with(RVPRJ_PREFIX) {
        format!("{prj_name}.json")
    } else {
        format!("{RVPRJ_PREFIX}{prj_name}.json")
    }
}
pub fn make_prjcfg_path(export_folder: &Path, prj_name: &str) -> PathBuf {
    let prj_name = match get_last_part_of_path(prj_name) {
        Some(prj_name_) => prj_name_.last_folder,
        None => prj_name,
    };
    Path::new(export_folder).join(make_prjcfg_filename(prj_name))
}
pub fn filename_to_prjname(filename: &str) -> RvResult<&str> {
    let file_name = osstr_to_str(Path::new(filename).file_stem()).map_err(to_rv)?;
    if file_name.starts_with(RVPRJ_PREFIX) {
        file_name.strip_prefix(RVPRJ_PREFIX).ok_or_else(|| {
            rverr!(
                "could not strip prefix '{}' from '{}'",
                RVPRJ_PREFIX,
                file_name
            )
        })
    } else {
        Ok(file_name)
    }
}

#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
pub struct ExportData {
    pub opened_folder: Option<String>,
    pub bbox_data: Option<BboxExportData>,
    pub bbox_options: Option<bbox_data::Options>,
    pub cfg: Cfg,
}

pub struct Defer<F: FnMut()> {
    pub func: F,
}
impl<F: FnMut()> Drop for Defer<F> {
    fn drop(&mut self) {
        (self.func)();
    }
}
#[macro_export]
macro_rules! defer {
    ($f:expr) => {
        let _dfr = $crate::file_util::Defer { func: $f };
    };
}
pub fn checked_remove<'a, P: AsRef<Path> + Debug>(
    path: &'a P,
    func: fn(p: &'a P) -> io::Result<()>,
) {
    match func(path) {
        Ok(_) => println!("removed {path:?}"),
        Err(e) => println!("could not remove {path:?} due to {e:?}"),
    }
}
#[macro_export]
macro_rules! defer_folder_removal {
    ($path:expr) => {
        let func = || $crate::file_util::checked_remove($path, std::fs::remove_dir_all);
        $crate::defer!(func);
    };
}
#[macro_export]
macro_rules! defer_file_removal {
    ($path:expr) => {
        let func = || $crate::file_util::checked_remove($path, std::fs::remove_file);
        $crate::defer!(func);
    };
}

#[allow(clippy::needless_lifetimes)]
pub fn files_in_folder<'a>(
    folder: &'a str,
    prefix: &'a str,
    extension: &'a str,
) -> io::Result<impl Iterator<Item = PathBuf> + 'a> {
    Ok(fs::read_dir(folder)?
        .flatten()
        .map(|de| de.path())
        .filter(|p| {
            let prefix: &str = prefix; // Not sure why the borrow checker needs this.
            p.is_file()
                && if let Some(fname) = p.file_name() {
                    fname.to_str().unwrap().starts_with(prefix)
                } else {
                    false
                }
                && (p.extension() == Some(OsStr::new(extension)))
        }))
}

pub fn write<P, C>(path: P, contents: C) -> RvResult<()>
where
    P: AsRef<Path> + Debug,
    C: AsRef<[u8]>,
{
    fs::write(&path, contents).map_err(|e| rverr!("could not write to {:?} since {:?}", path, e))
}

#[macro_export]
macro_rules! p_to_rv {
    ($path:expr, $expr:expr) => {
        $expr.map_err(|e| format_rverr!("{:?}, failed on {e:?}", $path))
    };
}

pub struct LastPartOfPath<'a> {
    pub last_folder: &'a str,
    // will transform /a/b/c/ to /a/b/c
    pub path_wo_final_sep: &'a str,
    // offset is defined by " or ' that might by at the beginning and end of the path
    pub offset: usize,
    // ', ", or empty string depending on their existence
    pub mark: &'a str,
    // separators can be / on Linux or for http and \ on Windows
    pub n_removed_separators: usize,
}

impl<'a> LastPartOfPath<'a> {
    pub fn name(&self) -> String {
        format!(
            "{}{}{}",
            self.mark,
            self.last_folder.replace(':', "_"),
            self.mark
        )
    }
}

pub fn url_encode(url: &str) -> String {
    let mappings = [
        (" ", "%20"),
        ("+", "%2B"),
        (",", "%2C"),
        (";", "%3B"),
        ("*", "%2A"),
        ("(", "%28"),
        (")", "%29"),
    ];
    let mut url = url.replace(mappings[0].0, mappings[1].1);
    for m in mappings[1..].iter() {
        url = url
            .replace(m.0, m.1)
            .replace(m.1.to_lowercase().as_str(), m.1);
    }
    url
}

fn get_last_part_of_path_by_sep(path: &str, sep: char) -> Option<LastPartOfPath> {
    if path.contains(sep) {
        let mark = if path.starts_with('\'') && path.ends_with('\'') {
            "\'"
        } else if path.starts_with('"') && path.ends_with('"') {
            "\""
        } else {
            ""
        };
        let offset = mark.len();
        let mut path_wo_final_sep = &path[offset..(path.len() - offset)];
        let n_fp_slice_initial = path_wo_final_sep.len();
        let mut last_folder = path_wo_final_sep.split(sep).last().unwrap_or("");
        while last_folder.is_empty() && !path_wo_final_sep.is_empty() {
            path_wo_final_sep = &path_wo_final_sep[0..path_wo_final_sep.len() - 1];
            last_folder = path_wo_final_sep.split(sep).last().unwrap_or("");
        }
        Some(LastPartOfPath {
            last_folder,
            path_wo_final_sep,
            offset,
            mark,
            n_removed_separators: n_fp_slice_initial - path_wo_final_sep.len(),
        })
    } else {
        None
    }
}

pub fn get_last_part_of_path(path: &str) -> Option<LastPartOfPath> {
    let lp_fw = get_last_part_of_path_by_sep(path, '/');
    if let Some(lp) = &lp_fw {
        if let Some(lp_fwbw) = get_last_part_of_path_by_sep(lp.last_folder, '\\') {
            Some(lp_fwbw)
        } else {
            lp_fw
        }
    } else {
        get_last_part_of_path_by_sep(path, '\\')
    }
}

pub fn local_file_info<P>(p: P) -> String
where
    P: AsRef<Path>,
{
    fs::metadata(p)
        .map(|md| {
            let n_bytes = md.len();
            if n_bytes < 1024 {
                format!("{}b", md.len())
            } else if n_bytes < 1024u64.pow(2) {
                format!("{:.3}kb", md.len() as f64 / 1024f64)
            } else {
                format!("{:.3}mb", md.len() as f64 / 1024f64.powi(2))
            }
        })
        .unwrap_or_else(|_| "".to_string())
}

#[test]
fn get_last_part() {
    let path = "http://localhost:8000/a/21%20%20b/Beg.png";
    let lp = get_last_part_of_path(path).unwrap();
    assert_eq!(lp.last_folder, "Beg.png");
}

#[test]
fn last_folder_part() {
    assert_eq!(
        get_last_part_of_path("a/b/c").map(|lp| lp.name()),
        Some("c".to_string())
    );
    assert_eq!(
        get_last_part_of_path_by_sep("a/b/c", '\\').map(|lp| lp.name()),
        None
    );
    assert_eq!(
        get_last_part_of_path_by_sep("a\\b\\c", '/').map(|lp| lp.name()),
        None
    );
    assert_eq!(
        get_last_part_of_path("a\\b\\c").map(|lp| lp.name()),
        Some("c".to_string())
    );
    assert_eq!(get_last_part_of_path("").map(|lp| lp.name()), None);
    assert_eq!(
        get_last_part_of_path("a/b/c/").map(|lp| lp.name()),
        Some("c".to_string())
    );
    assert_eq!(
        get_last_part_of_path("aadfh//bdafl////aksjc/////").map(|lp| lp.name()),
        Some("aksjc".to_string())
    );
    assert_eq!(
        get_last_part_of_path("\"aa dfh//bdafl////aks jc/////\"").map(|lp| lp.name()),
        Some("\"aks jc\"".to_string())
    );
    assert_eq!(
        get_last_part_of_path("'aa dfh//bdafl////aks jc/////'").map(|lp| lp.name()),
        Some("'aks jc'".to_string())
    );
}