artifactize-provider 0.27.1

Managed credentials, model discovery and direct HTTP turns for Artifactize.
Documentation
//! Platform entry points used at credential, transport and diagnostic edges.

use std::{
    ffi::{OsStr, OsString},
    io,
    path::Path,
    sync::OnceLock,
};

pub(crate) mod credential_files;
pub(crate) mod net;
pub(crate) use artifactize_foundation::platform::{
    files::{open_no_follow, open_regular, path_from_text, path_text},
    paths::{is_within, paths_equal, resolve_target},
    private::{
        PRIVATE_DIRECTORY, PRIVATE_FILE, create_private_dir_all, create_private_file,
        is_private_dir, is_private_file, remove_tree, sync_dir,
    },
};

pub(crate) fn absolute(path: &Path) -> io::Result<std::path::PathBuf> {
    std::path::absolute(path)
}

pub(crate) fn var(name: &str) -> Option<OsString> {
    std::env::var_os(name)
}
pub(crate) fn var_text(name: &str) -> Option<String> {
    std::env::var(name).ok()
}

pub(crate) fn as_text(value: &OsStr) -> Result<&str, &'static str> {
    value.to_str().ok_or("it is not valid Unicode text")
}

pub(crate) fn env_path(value: OsString) -> std::path::PathBuf {
    // Preserve a native path (including non-Unicode and Windows verbatim spelling),
    // while also accepting the path spelling that the CLI reports.
    if let Ok(text) = as_text(&value)
        && (text.starts_with("native-path:/") || cfg!(windows) && text.starts_with("//?/"))
        && let Ok(path) = path_from_text(text)
    {
        return path;
    }
    value.into()
}

pub(crate) fn entry_missing(path: &Path) -> bool {
    path.symlink_metadata()
        .is_err_and(|error| error.kind() == io::ErrorKind::NotFound)
}

pub(crate) fn label() -> &'static str {
    static LABEL: OnceLock<String> = OnceLock::new();
    LABEL.get_or_init(|| format!("{} {}", std::env::consts::OS, std::env::consts::ARCH))
}

/// ECMA-48 control sequence bytes: after `ESC [`, parameter bytes, then intermediate bytes,
/// then one final byte end the sequence.
const CSI_PARAMETER: std::ops::RangeInclusive<u8> = 0x30..=0x3f;
const CSI_INTERMEDIATE: std::ops::RangeInclusive<u8> = 0x20..=0x2f;
const CSI_FINAL: std::ops::RangeInclusive<u8> = 0x40..=0x7e;

/// The two-byte prefix introducing an ECMA-48 control sequence.
const CSI_PREFIX: &[u8] = b"\x1b[";
/// C0 controls except tab, line feed and carriage return are not printable tool output.
const C0_CONTROLS: &[std::ops::RangeInclusive<u8>] = &[0..=8, 11..=12, 14..=31];

pub(crate) fn clean_text(bytes: &[u8]) -> Vec<u8> {
    let text = String::from_utf8_lossy(bytes);
    let mut bytes = text.as_bytes();
    let mut clean = Vec::with_capacity(bytes.len());
    while let Some((&byte, tail)) = bytes.split_first() {
        if bytes.starts_with(CSI_PREFIX) {
            let parameters = bytes[CSI_PREFIX.len()..]
                .iter()
                .take_while(|byte| CSI_PARAMETER.contains(byte))
                .count();
            let intermediate_start = CSI_PREFIX.len() + parameters;
            let intermediates = bytes[intermediate_start..]
                .iter()
                .take_while(|byte| CSI_INTERMEDIATE.contains(byte))
                .count();
            let end = intermediate_start + intermediates;
            if bytes.get(end).is_some_and(|byte| CSI_FINAL.contains(byte)) {
                bytes = &bytes[end + 1..];
                continue;
            }
        }
        if !C0_CONTROLS.iter().any(|range| range.contains(&byte)) {
            clean.push(byte);
        }
        bytes = tail;
    }
    clean
}