fennec_common/
util.rs

1// Copyright 2023 Gregory Petrosyan <pgregory@pgregory.net>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
7use std::{
8    ffi::OsStr,
9    path::{Component, Path, PathBuf},
10};
11
12use crate::{import_path::PACKAGE_RE, SOURCE_EXTENSION};
13
14// https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
15const RESERVED_WINDOWS_NAMES: &[&str] = &[
16    "CON", "PRN", "AUX", "NUL", //
17    "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", //
18    "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
19];
20
21#[must_use]
22pub fn is_reserved_windows_filename(file_name: &str) -> bool {
23    RESERVED_WINDOWS_NAMES
24        .iter()
25        .any(|r| file_name.eq_ignore_ascii_case(r))
26}
27
28#[must_use]
29pub fn is_valid_utf8_visible(file_name: &OsStr) -> bool {
30    file_name // windows: WTF-8, unix: byte slice, usually UTF-8
31        .to_str() // maybe UTF-8
32        .map_or(false, |s| !s.starts_with('.'))
33}
34
35#[must_use]
36pub fn valid_package_name(file_name: &str) -> bool {
37    PACKAGE_RE.is_match(file_name) && !is_reserved_windows_filename(file_name)
38}
39
40#[must_use]
41pub fn valid_source_file_name(file_name: &str) -> bool {
42    match file_name.rsplit_once('.') {
43        Some((name, SOURCE_EXTENSION)) => valid_package_name(name),
44        _ => false,
45    }
46}
47
48// Adapted from the normalize-path crate, MIT license.
49#[must_use]
50pub fn normalize_path(path: &Path) -> PathBuf {
51    let mut components = path.components().peekable();
52    let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek() {
53        let buf = PathBuf::from(c.as_os_str());
54        components.next();
55        buf
56    } else {
57        PathBuf::new()
58    };
59
60    for component in components {
61        match component {
62            Component::Prefix(..) => unreachable!(),
63            Component::RootDir => {
64                ret.push(component.as_os_str());
65            }
66            Component::CurDir => {}
67            Component::ParentDir => {
68                ret.pop();
69            }
70            Component::Normal(c) => {
71                ret.push(c);
72            }
73        }
74    }
75
76    ret
77}