refine 3.0.0

Refine your file collections using Rust!
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use anyhow::{Result, anyhow};
use regex::Regex;
use std::cmp::Ordering;
use std::convert::Into;
use std::env;
use std::fmt::{self, Display};
use std::fs::Metadata;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::path::{Component, Path, PathBuf};
use std::sync::LazyLock;
use yansi::{Paint, Style};

/// A file or directory entry that is guaranteed to have a valid UTF-8 representation.
#[derive(Debug, Clone, Eq)] // Hash, PartialEq, Ord, and PartialOrd are below.
pub struct Entry {
    path: PathBuf,
    is_dir: bool,
}

/// Create a new entry from a path, checking that it has a valid UTF-8 representation.
///
/// The path must exist.
impl TryFrom<PathBuf> for Entry {
    type Error = (PathBuf, anyhow::Error);

    fn try_from(path: PathBuf) -> Result<Self, Self::Error> {
        let path_err = |err: anyhow::Error| (path.clone(), err);
        let is_dir = path
            .metadata()
            .map_err(Into::into)
            .map_err(path_err)?
            .is_dir(); // verify that the path exists and is a directory.
        if is_dir {
            path.file_name()
                .unwrap_or_default() // the root dir has no name.
                .to_str()
                .ok_or_else(|| anyhow!("no UTF-8 dir name: {path:?}"))
                .map_err(path_err)?;
        } else {
            path.file_stem()
                .ok_or_else(|| anyhow!("no file stem: {path:?}"))
                .map_err(path_err)?
                .to_str()
                .ok_or_else(|| anyhow!("no UTF-8 file stem: {path:?}"))
                .map_err(path_err)?;
            path.extension()
                .unwrap_or_default()
                .to_str()
                .ok_or_else(|| anyhow!("no UTF-8 file extension: {path:?}"))
                .map_err(path_err)?;
        }
        // I could just check that the entire path is valid UTF-8, but I want to give better error messages.
        if let Some(pp) = path.parent() {
            // the root dir has no parent.
            pp.to_str()
                .ok_or_else(|| anyhow!("no UTF-8 parent: {pp:?}"))
                .map_err(path_err)?;
        }
        Ok(Entry { path, is_dir })
    }
}

pub static ROOT: LazyLock<Entry> = LazyLock::new(|| Entry::try_new("/", true).unwrap());

impl Entry {
    /// Create a new entry that, in case the path does not exist, will assume the given directory flag.
    /// If it does exist, check that it has the correct directory flag or panic.
    pub fn try_new(path: impl Into<PathBuf>, is_dir: bool) -> Result<Self> {
        let path = path.into();
        if path.to_str().is_none() {
            return Err(anyhow!("invalid UTF-8 path: {path:?}"));
        }

        // panic if the entry exists and the directory flag doesn't match.
        // it should never happen in normal program logic, so if it does it's a bug.
        match path.try_exists() {
            Ok(true) => assert_eq!(path.is_dir(), is_dir, "is_dir error in {path:?}: {is_dir}"),
            Ok(false) => {} // the path was verified to not exist, cool.
            Err(err) => println!("warning: couldn't verify {path:?}: {err}"),
        }

        Ok(Entry { path, is_dir })
    }

    /// Create a new entry with the given name adjoined without checking UTF-8 again.
    pub fn join(&self, name: impl AsRef<str>) -> Entry {
        let path = self.path.join(name.as_ref());
        let is_dir = path.is_dir();
        Entry { path, is_dir }
    }

    /// Create a new entry with the given name without checking UTF-8 again.
    pub fn with_file_name(&self, name: impl AsRef<str>) -> Entry {
        let path = self.path.with_file_name(name.as_ref());
        let is_dir = path.is_dir();
        Entry { path, is_dir }
    }

    /// Get the stem and extension from files, or name from directories.
    pub fn filename_parts(&self) -> (&str, &str) {
        match self.is_dir {
            true => (self.file_name(), ""),
            false => (
                self.path.file_stem().unwrap().to_str().unwrap(),
                self.path.extension().unwrap_or_default().to_str().unwrap(),
            ),
        }
    }

    /// Get the canonical name, source alias, sequence, comment, and extension from collections.
    pub fn collection_parts(&self) -> (&str, Option<&str>, Option<usize>, &str, &str) {
        // regex: name~24 or name+alias~24.
        static RE: LazyLock<Regex> =
            LazyLock::new(|| Regex::new(r"^(\w+)(?:\+(\w+))?~(\d+)(.*)$").unwrap());

        let (stem, ext) = self.filename_parts();
        let Some(caps) = RE.captures(stem) else {
            return (stem, None, None, "", ext);
        };
        let canonical = caps.get(1).unwrap().as_str(); // regex guarantees name is present.
        let alias = caps.get(2).map(|m| m.as_str());
        let seq = caps.get(3).and_then(|m| m.as_str().parse().ok());
        let comment = caps.get(4).map_or("", |m| m.as_str());
        (canonical, alias, seq, comment, ext)
    }

    /// Return a cached directory flag, which does not touch the filesystem again.
    pub fn is_dir(&self) -> bool {
        self.is_dir
    }

    /// Get the filename from entries directly as a &str.
    pub fn file_name(&self) -> &str {
        self.path
            .file_name()
            .map(|n| n.to_str().unwrap())
            .unwrap_or_default()
    }

    pub fn to_str(&self) -> &str {
        self.path.to_str().unwrap()
    }

    /// Get the parent directory as an entry, without checking UTF-8 again.
    pub fn parent(&self) -> Option<Entry> {
        self.path.parent().map(|p| Entry {
            path: p.to_owned(),
            is_dir: true,
        })
    }

    pub fn metadata(&self) -> Result<Metadata> {
        self.path.metadata().map_err(Into::into)
    }

    pub fn display_path(&self) -> impl Display {
        DisplayPath(self)
    }

    pub fn display_filename(&self) -> impl Display {
        DisplayFilename(self)
    }

    pub fn resolve(&self) -> Result<Entry> {
        let mut it = self.path.components();
        let mut res = match it.next().unwrap() {
            Component::Normal(x) if x == "~" => {
                dirs::home_dir().ok_or_else(|| anyhow!("no home dir"))?
            }
            Component::Normal(x) => {
                let mut dir = env::current_dir()?;
                dir.push(x);
                dir
            }
            Component::CurDir => env::current_dir()?,
            Component::ParentDir => {
                let mut dir = env::current_dir()?;
                dir.pop();
                dir
            }
            x => PathBuf::from(x.as_os_str()),
        };
        for comp in it {
            match comp {
                Component::RootDir => res.push(comp), // windows might have returned Prefix above, so RootDir comes here.
                Component::Normal(_) => res.push(comp),
                Component::ParentDir => {
                    if !res.pop() {
                        return Err(anyhow!("invalid path: {self}"));
                    }
                }
                _ => unreachable!(),
            }
        }
        Entry::try_new(res, self.is_dir) // the paths prepended above are NOT guaranteed to be valid UTF-8.
    }
}

/// A [Display] implementation for [Entry] that print its full path.
#[derive(Debug)]
pub struct DisplayPath<'a>(&'a Entry);

/// A [Display] implementation for [Entry] that print only its file name.
#[derive(Debug)]
pub struct DisplayFilename<'a>(&'a Entry);

const DIR_STYLE: (Style, Style) = {
    let parent_dir: Style = Style::new().yellow();
    (parent_dir, parent_dir.bold())
};
const FILE_STYLE: (Style, Style) = {
    let parent_file = Style::new().cyan();
    (parent_file, parent_file.bold())
};

impl Display for DisplayPath<'_> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let entry = self.0;
        let (parent, name, symbol) = display_parts(entry);
        let (p_style, n_style) = if entry.is_dir { DIR_STYLE } else { FILE_STYLE };
        write!(
            f,
            "{}{}{}",
            parent.paint(p_style),
            name.paint(n_style),
            symbol.paint(n_style)
        )
    }
}

impl Display for DisplayFilename<'_> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let entry = self.0;
        let (_, name, symbol) = display_parts(entry);
        let (_, style) = if entry.is_dir { DIR_STYLE } else { FILE_STYLE };
        write!(f, "{}{}", name.paint(style), symbol.paint(style))
    }
}

/// Get the parent directory, name, and directory symbol for an entry.
/// They are used by [DisplayPath] and [DisplayFilename] implementations, which style them.
fn display_parts(entry: &Entry) -> (&str, &str, &str) {
    let full = entry.to_str();
    let (parent, name) = match entry.path.file_name().map(|s| s.to_str().unwrap()) {
        Some(name) => {
            let pos = full.rfind(name).unwrap();
            (&full[..pos], name)
        }
        None => ("", full),
    };
    let dir_id = match entry.is_dir && !name.ends_with('/') {
        true => "/",
        false => "",
    };
    (parent, name, dir_id)
}

impl Display for Entry {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.display_path().fmt(f)
    }
}

impl Deref for Entry {
    type Target = Path;

    fn deref(&self) -> &Self::Target {
        &self.path
    }
}

impl AsRef<Path> for Entry {
    fn as_ref(&self) -> &Path {
        &self.path
    }
}

impl Hash for Entry {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.path.hash(state)
    }
}

impl Ord for Entry {
    fn cmp(&self, other: &Self) -> Ordering {
        self.path.cmp(&other.path)
    }
}

impl PartialOrd for Entry {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl PartialEq for Entry {
    fn eq(&self, other: &Self) -> bool {
        self.path == other.path
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn filename_parts() {
        #[track_caller]
        fn case(p: impl Into<PathBuf>, is_dir: bool, out: (&str, &str)) {
            let entry = Entry {
                path: p.into(),
                is_dir,
            };
            assert_eq!(out, entry.filename_parts())
        }

        case("foo", false, ("foo", ""));
        case("foo.bar", false, ("foo", "bar"));
        case("foo.bar.baz", false, ("foo.bar", "baz"));

        case(".foo", false, (".foo", ""));
        case(".foo.bar", false, (".foo", "bar"));
        case(".foo.bar.baz", false, (".foo.bar", "baz"));

        case("foo", true, ("foo", ""));
        case("foo.bar", true, ("foo.bar", ""));
        case("foo.bar.baz", true, ("foo.bar.baz", ""));

        case(".foo", true, (".foo", ""));
        case(".foo.bar", true, (".foo.bar", ""));
        case(".foo.bar.baz", true, (".foo.bar.baz", ""));
    }

    #[test]
    fn collection_parts() {
        #[track_caller]
        fn case(base: &str, out: (&str, Option<&str>, Option<usize>, &str)) {
            let (name, alias, seq, comment) = out;
            let entry = Entry::try_new(format!("{base}.ext"), false).unwrap();
            let out = (name, alias, seq, comment, "ext");
            assert_eq!(out, entry.collection_parts());
        }

        // stem only.
        case("foo", ("foo", None, None, ""));
        case("foo bar", ("foo bar", None, None, ""));
        case("foo bar - baz", ("foo bar - baz", None, None, ""));
        case("foo - 2025 - 24", ("foo - 2025 - 24", None, None, ""));
        case("_foo_-24", ("_foo_-24", None, None, ""));
        case("foo ~ 24", ("foo ~ 24", None, None, ""));
        case("foo~ 24", ("foo~ 24", None, None, ""));
        case("foo+bar", ("foo+bar", None, None, ""));
        case("foo+bar,baz", ("foo+bar,baz", None, None, ""));
        case("foo+bar ~ 24", ("foo+bar ~ 24", None, None, ""));
        case("foo ~24", ("foo ~24", None, None, ""));
        case("foo bar~24", ("foo bar~24", None, None, ""));
        case("foo bar ~24", ("foo bar ~24", None, None, ""));
        case("_foo_ ~24", ("_foo_ ~24", None, None, ""));
        case("foo - 33~24", ("foo - 33~24", None, None, ""));
        case("foo+ ~24", ("foo+ ~24", None, None, ""));
        case("foo+ asd~24", ("foo+ asd~24", None, None, ""));
        case("foo+asd ~24", ("foo+asd ~24", None, None, ""));
        case("foo+~24", ("foo+~24", None, None, ""));
        case(",~24", (",~24", None, None, ""));
        case("foo+,~24", ("foo+,~24", None, None, ""));
        case("foo+bar,~24", ("foo+bar,~24", None, None, ""));
        case("foo+bar,~24 cool", ("foo+bar,~24 cool", None, None, ""));

        // name and seq.
        case("foo~24", ("foo", None, Some(24), ""));
        case("foo_~24", ("foo_", None, Some(24), ""));
        case("__foo~24", ("__foo", None, Some(24), ""));
        case("_foo__~24", ("_foo__", None, Some(24), ""));

        // name, aliases and seq.
        case("foo+bar~24", ("foo", Some("bar"), Some(24), ""));
        case(
            "foo_bar__+_baz__~24",
            ("foo_bar__", Some("_baz__"), Some(24), ""),
        );

        // name, seq, and comment.
        case("foo~24cool", ("foo", None, Some(24), "cool"));
        case("foo~24 cool", ("foo", None, Some(24), " cool"));
        case("foo_~24-nice!", ("foo_", None, Some(24), "-nice!"));
        case("__foo~24 ?why?", ("__foo", None, Some(24), " ?why?"));
        case("_foo__~24 - cut", ("_foo__", None, Some(24), " - cut"));

        // name, aliases, seq, and comment.
        case(
            "foo+bar~24 seen 3 times",
            ("foo", Some("bar"), Some(24), " seen 3 times"),
        );
        case(
            "_foo+__bar_~24 with comment!",
            ("_foo", Some("__bar_"), Some(24), " with comment!"),
        );
    }

    #[test]
    fn fn_display_parts() {
        #[track_caller]
        fn case(p: impl Into<PathBuf>, is_dir: bool, out: (&str, &str, &str)) {
            let entry = Entry {
                path: p.into(),
                is_dir,
            };
            assert_eq!(out, display_parts(&entry));
        }

        // Directory cases (fixed)
        case(".", true, ("", ".", "/"));
        case("..", true, ("", "..", "/"));
        case("/", true, ("", "/", ""));
        case("./", true, ("", "./", ""));
        case("../", true, ("", "../", ""));
        case("dir", true, ("", "dir", "/"));
        case("dir/", true, ("", "dir", "/"));
        case("dir/.", true, ("", "dir", "/"));
        case("./dir", true, ("./", "dir", "/"));
        case("./dir/", true, ("./", "dir", "/"));
        case("./dir/.", true, ("./", "dir", "/"));

        // File cases
        case("file.txt", false, ("", "file.txt", ""));
        case("./file.txt", false, ("./", "file.txt", ""));
        case("dir/file.txt", false, ("dir/", "file.txt", ""));
        case("./dir/file.txt", false, ("./dir/", "file.txt", ""));
        case(".hidden", false, ("", ".hidden", ""));
        case("./dir/.hidden", false, ("./dir/", ".hidden", ""));
    }
}