summaryrefslogtreecommitdiffstats
path: root/vendor/gix-odb/src/alternate/parse.rs
blob: 1c297d1539c98734f1a0859627d4a9c58ddea29c (plain)
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
use std::{borrow::Cow, path::PathBuf};

use gix_object::bstr::ByteSlice;

/// Returned as part of [`crate::alternate::Error::Parse`]
#[derive(thiserror::Error, Debug)]
#[allow(missing_docs)]
pub enum Error {
    #[error("Could not obtain an object path for the alternate directory '{}'", String::from_utf8_lossy(.0))]
    PathConversion(Vec<u8>),
    #[error("Could not unquote alternate path")]
    Unquote(#[from] gix_quote::ansi_c::undo::Error),
}

pub(crate) fn content(input: &[u8]) -> Result<Vec<PathBuf>, Error> {
    let mut out = Vec::new();
    for line in input.split(|b| *b == b'\n') {
        let line = line.as_bstr();
        if line.is_empty() || line.starts_with(b"#") {
            continue;
        }
        out.push(
            gix_path::try_from_bstr(if line.starts_with(b"\"") {
                gix_quote::ansi_c::undo(line)?.0
            } else {
                Cow::Borrowed(line)
            })
            .map_err(|_| Error::PathConversion(line.to_vec()))?
            .into_owned(),
        )
    }
    Ok(out)
}