Skip to content

liboverdrop-utf8 - handle configuration fragments using UTF-8 paths

[Home | sourcehut | crates.io | ReadTheDocs]

Overview

The liboverdrop-utf8 crate extends the liboverdrop Rust crate by providing the scan_utf8 function that both accepts and returns valid UTF-8 fragment and path names.

Examples

A trivial demonstration of the parameters passed in:

use camino::Utf8PathBuf;
use eyre::{Result, bail};

fn main() -> Result<()> {
    let cwd = Utf8PathBuf::from(".").canonicalize_utf8()?;
    let res = liboverdrop_utf8::scan_utf8(
        &[
            Utf8PathBuf::from(".").canonicalize_utf8()?,
        ],
        "src",
        &["rs"],
        true,
    )?;
    if !res.contains_key("lib.rs") {
        bail!("No lib.rs file found in {cwd}/src");
    }
    Ok(())
}

Examine some actual configuration files:

use camino::{Utf8Path, Utf8PathBuf};
use eyre::{Result, WrapErr as _, bail};
use itertools::Itertools as _;
use xdg::BaseDirectories;

fn main() -> Result<()> {
    let base_dirs = BaseDirectories::with_prefix("kak");
    // Kakoune's config file lives in ~/.config/kak/ or /usr/share/kak/
    let dirs: Vec<Utf8PathBuf> = base_dirs
        .get_data_dirs()
        .into_iter()
        .chain(base_dirs.get_config_home())
        .map(|path| {
            Utf8PathBuf::try_from(path).context("Could not represent a path as valid UTF-8")
        })
        .collect::<Result<_>>()?;
    if dirs.is_empty() {
        bail!("Could not build the list of Kakoune-related directories");
    }
    // No need for a closure, we use this in all our messages
    let joined_dirs = dirs.iter().map(AsRef::<str>::as_ref).join(" ");

    let empty_list: [&str; 0] = [];
    let res = liboverdrop_utf8::scan_utf8(
        &dirs,
        "",
        &empty_list,
        true,
    ).with_context(|| format!("Could not examine {joined_dirs}"))?;
    if let Some(path) = res.get("kakrc") {
        println!("Found a Kakoune config file at {path} in {joined_dirs}");
    } else {
        println!("No Kakoune config file in {joined_dirs}");
    }
    Ok(())
}

Contact

The liboverdrop-utf8 library was written by Peter Pentchev. It is developed in a sourcehut repository. This documentation is hosted at Ringlet with a copy at ReadTheDocs.