refactor(nix-compat/aterm): update function names a bit

Don't call functions bstr or str when they return BString or String,
it's confusing.

Rename them to `string` and `bytes`. We might be able to generalize over
this being BString or Vec<u8> later.

Change-Id: I8198551ed3ba1cfc479bf7e3cbbc13a426faf4c0
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12257
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Reviewed-by: Connor Brewster <cbrewster@hey.com>
This commit is contained in:
Florian Klink 2024-08-20 16:21:52 +03:00 committed by clbot
parent e579d3869d
commit 413135b925
3 changed files with 26 additions and 26 deletions

View file

@ -2,6 +2,6 @@ mod escape;
mod parser; mod parser;
pub(crate) use escape::escape_bytes; pub(crate) use escape::escape_bytes;
pub(crate) use parser::parse_bstr_field; pub(crate) use parser::parse_bytes_field;
pub(crate) use parser::parse_str_list;
pub(crate) use parser::parse_string_field; pub(crate) use parser::parse_string_field;
pub(crate) use parser::parse_string_list;

View file

@ -11,8 +11,10 @@ use nom::multi::separated_list0;
use nom::sequence::delimited; use nom::sequence::delimited;
use nom::IResult; use nom::IResult;
/// Parse a bstr and undo any escaping. /// Parse a bstr and undo any escaping (which is why this needs to allocate).
fn parse_escaped_bstr(i: &[u8]) -> IResult<&[u8], BString> { // FUTUREWORK: have a version for fields that are known to not need escaping
// (like store paths), and use &str.
fn parse_escaped_bytes(i: &[u8]) -> IResult<&[u8], BString> {
escaped_transform( escaped_transform(
is_not("\"\\"), is_not("\"\\"),
'\\', '\\',
@ -29,14 +31,14 @@ fn parse_escaped_bstr(i: &[u8]) -> IResult<&[u8], BString> {
/// Parse a field in double quotes, undo any escaping, and return the unquoted /// Parse a field in double quotes, undo any escaping, and return the unquoted
/// and decoded `Vec<u8>`. /// and decoded `Vec<u8>`.
pub(crate) fn parse_bstr_field(i: &[u8]) -> IResult<&[u8], BString> { pub(crate) fn parse_bytes_field(i: &[u8]) -> IResult<&[u8], BString> {
// inside double quotes… // inside double quotes…
delimited( delimited(
nomchar('\"'), nomchar('\"'),
// There is // There is
alt(( alt((
// …either is a bstr after unescaping // …either is a bstr after unescaping
parse_escaped_bstr, parse_escaped_bytes,
// …or an empty string. // …or an empty string.
map(tag(b""), |_| BString::default()), map(tag(b""), |_| BString::default()),
)), )),
@ -45,8 +47,8 @@ pub(crate) fn parse_bstr_field(i: &[u8]) -> IResult<&[u8], BString> {
} }
/// Parse a field in double quotes, undo any escaping, and return the unquoted /// Parse a field in double quotes, undo any escaping, and return the unquoted
/// and decoded string, if it's a valid string. Or fail parsing if the bytes are /// and decoded [String], if it's valid UTF-8.
/// no valid UTF-8. /// Or fail parsing if the bytes are no valid UTF-8.
pub(crate) fn parse_string_field(i: &[u8]) -> IResult<&[u8], String> { pub(crate) fn parse_string_field(i: &[u8]) -> IResult<&[u8], String> {
// inside double quotes… // inside double quotes…
delimited( delimited(
@ -54,18 +56,18 @@ pub(crate) fn parse_string_field(i: &[u8]) -> IResult<&[u8], String> {
// There is // There is
alt(( alt((
// either is a String after unescaping // either is a String after unescaping
nom::combinator::map_opt(parse_escaped_bstr, |escaped_bstr| { nom::combinator::map_opt(parse_escaped_bytes, |escaped_bytes| {
String::from_utf8(escaped_bstr.into()).ok() String::from_utf8(escaped_bytes.into()).ok()
}), }),
// or an empty string. // or an empty string.
map(tag(b""), |_| String::new()), map(tag(b""), |_| "".to_string()),
)), )),
nomchar('\"'), nomchar('\"'),
)(i) )(i)
} }
/// Parse a list of of string fields (enclosed in brackets) /// Parse a list of string fields (enclosed in brackets)
pub(crate) fn parse_str_list(i: &[u8]) -> IResult<&[u8], Vec<String>> { pub(crate) fn parse_string_list(i: &[u8]) -> IResult<&[u8], Vec<String>> {
// inside brackets // inside brackets
delimited( delimited(
nomchar('['), nomchar('['),
@ -89,7 +91,7 @@ mod tests {
#[case] expected: &[u8], #[case] expected: &[u8],
#[case] exp_rest: &[u8], #[case] exp_rest: &[u8],
) { ) {
let (rest, parsed) = super::parse_bstr_field(input).expect("must parse"); let (rest, parsed) = super::parse_bytes_field(input).expect("must parse");
assert_eq!(exp_rest, rest, "expected remainder"); assert_eq!(exp_rest, rest, "expected remainder");
assert_eq!(expected, parsed); assert_eq!(expected, parsed);
} }
@ -118,7 +120,7 @@ mod tests {
#[case::empty_list(b"[]", vec![], b"")] #[case::empty_list(b"[]", vec![], b"")]
#[case::empty_list_with_rest(b"[]blub", vec![], b"blub")] #[case::empty_list_with_rest(b"[]blub", vec![], b"blub")]
fn parse_list(#[case] input: &[u8], #[case] expected: Vec<String>, #[case] exp_rest: &[u8]) { fn parse_list(#[case] input: &[u8], #[case] expected: Vec<String>, #[case] exp_rest: &[u8]) {
let (rest, parsed) = super::parse_str_list(input).expect("must parse"); let (rest, parsed) = super::parse_string_list(input).expect("must parse");
assert_eq!(exp_rest, rest, "expected remainder"); assert_eq!(exp_rest, rest, "expected remainder");
assert_eq!(expected, parsed); assert_eq!(expected, parsed);
} }

View file

@ -3,7 +3,6 @@
//! //!
//! [ATerm]: http://program-transformation.org/Tools/ATermFormat.html //! [ATerm]: http://program-transformation.org/Tools/ATermFormat.html
use bstr::BString;
use nom::bytes::complete::tag; use nom::bytes::complete::tag;
use nom::character::complete::char as nomchar; use nom::character::complete::char as nomchar;
use nom::combinator::{all_consuming, map_res}; use nom::combinator::{all_consuming, map_res};
@ -73,7 +72,7 @@ fn parse_output(i: &[u8]) -> NomResult<&[u8], (String, Output)> {
terminated(aterm::parse_string_field, nomchar(',')), terminated(aterm::parse_string_field, nomchar(',')),
terminated(aterm::parse_string_field, nomchar(',')), terminated(aterm::parse_string_field, nomchar(',')),
terminated(aterm::parse_string_field, nomchar(',')), terminated(aterm::parse_string_field, nomchar(',')),
aterm::parse_bstr_field, aterm::parse_bytes_field,
))(i) ))(i)
.map_err(into_nomerror) .map_err(into_nomerror)
}, },
@ -150,10 +149,10 @@ fn parse_outputs(i: &[u8]) -> NomResult<&[u8], BTreeMap<String, Output>> {
} }
fn parse_input_derivations(i: &[u8]) -> NomResult<&[u8], BTreeMap<StorePath, BTreeSet<String>>> { fn parse_input_derivations(i: &[u8]) -> NomResult<&[u8], BTreeMap<StorePath, BTreeSet<String>>> {
let (i, input_derivations_list) = parse_kv::<Vec<String>, _>(aterm::parse_str_list)(i)?; let (i, input_derivations_list) = parse_kv(aterm::parse_string_list)(i)?;
// This is a HashMap of drv paths to a list of output names. // This is a HashMap of drv paths to a list of output names.
let mut input_derivations: BTreeMap<StorePath, BTreeSet<String>> = BTreeMap::new(); let mut input_derivations: BTreeMap<StorePath, BTreeSet<_>> = BTreeMap::new();
for (input_derivation, output_names) in input_derivations_list { for (input_derivation, output_names) in input_derivations_list {
let mut new_output_names = BTreeSet::new(); let mut new_output_names = BTreeSet::new();
@ -179,7 +178,7 @@ fn parse_input_derivations(i: &[u8]) -> NomResult<&[u8], BTreeMap<StorePath, BTr
} }
fn parse_input_sources(i: &[u8]) -> NomResult<&[u8], BTreeSet<StorePath>> { fn parse_input_sources(i: &[u8]) -> NomResult<&[u8], BTreeSet<StorePath>> {
let (i, input_sources_lst) = aterm::parse_str_list(i).map_err(into_nomerror)?; let (i, input_sources_lst) = aterm::parse_string_list(i).map_err(into_nomerror)?;
let mut input_sources: BTreeSet<_> = BTreeSet::new(); let mut input_sources: BTreeSet<_> = BTreeSet::new();
for input_source in input_sources_lst.into_iter() { for input_source in input_sources_lst.into_iter() {
@ -240,9 +239,9 @@ pub fn parse_derivation(i: &[u8]) -> NomResult<&[u8], Derivation> {
// // parse builder // // parse builder
|i| terminated(aterm::parse_string_field, nomchar(','))(i).map_err(into_nomerror), |i| terminated(aterm::parse_string_field, nomchar(','))(i).map_err(into_nomerror),
// // parse arguments // // parse arguments
|i| terminated(aterm::parse_str_list, nomchar(','))(i).map_err(into_nomerror), |i| terminated(aterm::parse_string_list, nomchar(','))(i).map_err(into_nomerror),
// parse environment // parse environment
parse_kv::<BString, _>(aterm::parse_bstr_field), parse_kv(aterm::parse_bytes_field),
)), )),
nomchar(')'), nomchar(')'),
) )
@ -427,8 +426,8 @@ mod tests {
#[case] expected: &BTreeMap<String, BString>, #[case] expected: &BTreeMap<String, BString>,
#[case] exp_rest: &[u8], #[case] exp_rest: &[u8],
) { ) {
let (rest, parsed) = super::parse_kv::<BString, _>(crate::aterm::parse_bstr_field)(input) let (rest, parsed) =
.expect("must parse"); super::parse_kv(crate::aterm::parse_bytes_field)(input).expect("must parse");
assert_eq!(exp_rest, rest, "expected remainder"); assert_eq!(exp_rest, rest, "expected remainder");
assert_eq!(*expected, parsed); assert_eq!(*expected, parsed);
} }
@ -437,8 +436,7 @@ mod tests {
#[test] #[test]
fn parse_kv_fail_dup_keys() { fn parse_kv_fail_dup_keys() {
let input: &'static [u8] = b"[(\"a\",\"1\"),(\"a\",\"2\")]"; let input: &'static [u8] = b"[(\"a\",\"1\"),(\"a\",\"2\")]";
let e = super::parse_kv::<BString, _>(crate::aterm::parse_bstr_field)(input) let e = super::parse_kv(crate::aterm::parse_bytes_field)(input).expect_err("must fail");
.expect_err("must fail");
match e { match e {
nom::Err::Failure(e) => { nom::Err::Failure(e) => {