Skip to content

Commit

Permalink
feat: add resolved (resolution) field to Entry (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
selfisekai authored Dec 8, 2024
1 parent 9e3adf0 commit d18f930
Showing 1 changed file with 66 additions and 2 deletions.
68 changes: 66 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use nom::{
complete::{digit1, line_ending, not_line_ending, one_of, space0, space1},
streaming::multispace0,
},
combinator::{cond, eof, fail, map_res, opt, recognize},
combinator::{cond, eof, fail, map, map_res, opt, recognize},
error::{context, ParseError, VerboseError},
multi::{count, many0, many1, many_till, separated_list1},
sequence::{delimited, preceded, terminated, tuple},
Expand Down Expand Up @@ -48,6 +48,7 @@ pub enum Generator {
pub struct Entry<'a> {
pub name: &'a str,
pub version: &'a str,
pub resolved: &'a str,
pub integrity: &'a str,
pub dependencies: Vec<(&'a str, &'a str)>,
pub descriptors: Vec<(&'a str, &'a str)>,
Expand Down Expand Up @@ -182,6 +183,7 @@ fn entry(input: &str) -> Res<&str, Entry> {
#[derive(PartialEq, Debug)]
enum EntryItem<'a> {
Version(&'a str),
Resolved(&'a str),
Dependencies(Vec<(&'a str, &'a str)>),
Integrity(&'a str),
Unknown(&'a str),
Expand Down Expand Up @@ -209,7 +211,13 @@ fn integrity(input: &str) -> Res<&str, EntryItem> {
}

fn entry_item(input: &str) -> Res<&str, EntryItem> {
alt((entry_version, parse_dependencies, integrity, unknown_line))(input)
alt((
entry_version,
parse_dependencies,
integrity,
entry_resolved,
unknown_line,
))(input)
}

fn parse_entry(input: &str) -> Res<&str, Entry> {
Expand All @@ -223,12 +231,14 @@ fn parse_entry(input: &str) -> Res<&str, Entry> {
let name = first_descriptor.0;

let mut version = "";
let mut resolved = "";
let mut dependencies = Vec::new();
let mut integrity = "";

for ei in entry_items {
match ei {
EntryItem::Version(v) => version = v,
EntryItem::Resolved(r) => resolved = r,
EntryItem::Dependencies(d) => dependencies = d,
EntryItem::Integrity(c) => integrity = c,
EntryItem::Unknown(_) => (),
Expand All @@ -249,6 +259,7 @@ fn parse_entry(input: &str) -> Res<&str, Entry> {
Entry {
name,
version,
resolved,
integrity,
dependencies,
descriptors,
Expand Down Expand Up @@ -360,6 +371,30 @@ fn entry_descriptors<'a>(input: &'a str) -> Res<&'a str, Vec<(&'a str, &'a str)>
)(input)
}

fn entry_resolved(input: &str) -> Res<&str, EntryItem> {
// " resolved \"https://registry.yarnpkg.com/yargs/-/yargs-9.0.1.tgz#52acc23feecac34042078ee78c0c007f5085db4c\"\r\n"
// " resolution: \"@babel/code-frame@npm:7.18.6\"\r\n"

context(
"resolved",
preceded(
tuple((
space1,
opt(tag("\"")),
alt((tag("resolved"), tag("resolution"))),
opt(tag("\"")),
opt(tag(":")),
space1,
tag("\""),
)),
terminated(
map(is_not("\"\r\n"), EntryItem::Resolved),
tuple((tag("\""), line_ending)),
),
),
)(input)
}

fn entry_version(input: &str) -> Res<&str, EntryItem> {
// "version \"7.12.13\"\r\n"
// "version \"workspace:foobar\"\r\n"
Expand Down Expand Up @@ -412,6 +447,7 @@ mod tests {
&Entry {
name: "@babel/code-frame",
version: "7.12.13",
resolved: "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.13.tgz#dcfc826beef65e75c50e21d3837d7d95798dd658",
descriptors: vec![("@babel/code-frame", "^7.0.0")],
dependencies: vec![("@babel/highlight", "^7.12.13")],
integrity: "sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g=="
Expand All @@ -423,6 +459,7 @@ mod tests {
&Entry {
name: "yargs",
version: "9.0.1",
resolved: "https://registry.yarnpkg.com/yargs/-/yargs-9.0.1.tgz#52acc23feecac34042078ee78c0c007f5085db4c",
descriptors: vec![("yargs", "^9.0.0")],
dependencies: vec![
("camelcase", "^4.1.0"),
Expand Down Expand Up @@ -535,13 +572,15 @@ mod tests {
Entry {
name: "@babel/code-frame",
version: "7.12.13",
resolved: "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.13.tgz#dcfc826beef65e75c50e21d3837d7d95798dd658",
descriptors: vec![("@babel/code-frame", "^7.0.0")],
dependencies: vec![("@babel/highlight", "^7.12.13")],
integrity: "sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g=="
},
Entry {
name: "@babel/helper-validator-identifier",
version: "7.12.11",
resolved: "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.12.11.tgz#c9a1f021917dcb5ccf0d4e453e399022981fc9ed",
descriptors: vec![("@babel/helper-validator-identifier", "^7.12.11")],
integrity: "sha512-np/lG3uARFybkoHokJUmf1QfEvRVCPbmQeUQpKow5cQ3xWrV9i3rUHodKDJPQfTVX61qKi+UdYk8kik84n7XOw==",
..Default::default()
Expand All @@ -559,6 +598,11 @@ mod tests {
&Entry {
name: "@babel/code-frame",
version: "7.18.6",
resolved: if with_bug {
"https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.18.6.tgz"
} else {
"@babel/code-frame@npm:7.18.6"
},
descriptors: vec![(
"@babel/code-frame",
if with_bug { "^7.18.6" } else { "npm:^7.18.6" }
Expand All @@ -577,6 +621,11 @@ mod tests {
&Entry {
name: "yargs",
version: "17.5.1",
resolved: if with_bug {
"https://registry.npmjs.org/yargs/-/yargs-17.5.1.tgz"
} else {
"yargs@npm:17.5.1"
},
descriptors: vec![("yargs", if with_bug { "^17.5.1" } else { "npm:^17.5.1" })],
dependencies: vec![
("cliui", "^7.0.2"),
Expand Down Expand Up @@ -656,20 +705,23 @@ __metadata:
Entry {
name: "@babel/helper-plugin-utils",
version: "7.16.7",
resolved: "@babel/helper-plugin-utils@npm:7.16.7",
descriptors: vec![("@babel/helper-plugin-utils", "npm:^7.16.7")],
integrity: "d08dd86554a186c2538547cd537552e4029f704994a9201d41d82015c10ed7f58f9036e8d1527c3760f042409163269d308b0b3706589039c5f1884619c6d4ce",
..Default::default()
},
Entry {
name: "@babel/plugin-transform-for-of",
version: "7.16.7",
resolved: "@babel/plugin-transform-for-of@npm:7.16.7",
descriptors: vec![("@babel/plugin-transform-for-of", "npm:^7.12.1")],
dependencies: vec![("@babel/helper-plugin-utils", "^7.16.7")],
integrity: "35c9264ee4bef814818123d70afe8b2f0a85753a0a9dc7b73f93a71cadc5d7de852f1a3e300a7c69a491705805704611de1e2ccceb5686f7828d6bca2e5a7306",
},
Entry {
name: "@babel/runtime",
version: "7.17.9",
resolved: "@babel/runtime@npm:7.17.9",
descriptors: vec![("@babel/runtime", "npm:^7.12.5")],
dependencies: vec![("regenerator-runtime", "^0.13.4")],
integrity: "4d56bdb82890f386d5a57c40ef985a0ed7f0a78f789377a2d0c3e8826819e0f7f16ba0fe906d9b2241c5f7ca56630ef0653f5bb99f03771f7b87ff8af4bf5fe3"
Expand Down Expand Up @@ -712,13 +764,15 @@ __metadata:
Entry {
name: "foo",
version: "0.0.0-use.local",
resolved: "foo@workspace:.",
integrity: "",
descriptors: vec![("foo", "workspace:.")],
dependencies: vec![("valib-aliased", "1.0.0 || 1.0.1")],
},
Entry {
name: "valib-aliased",
version: "1.0.0",
resolved: "valib@npm:1.0.0",
integrity: "ad4f5a0b5dde5ab5e3cc87050fad4d7096c32797454d8e37c7dadf3455a43a7221a3caaa0ad9e72b8cd96668168e5a25d5f0072e21990f7f80a64b1a4e34e921",
descriptors: vec![("valib-aliased", "1.0.0 || 1.0.1")],
dependencies: vec![],
Expand All @@ -739,6 +793,7 @@ __metadata:
Entry {
name: "@babel/code-frame",
version: "7.12.13",
resolved: "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.13.tgz#dcfc826beef65e75c50e21d3837d7d95798dd658",
descriptors: vec![("@babel/code-frame", "^7.0.0")],
dependencies: vec![("@babel/highlight", "^7.12.13")],
integrity: "sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==",
Expand All @@ -751,6 +806,7 @@ __metadata:
Entry {
name: "@babel/code-frame",
version: "7.12.13",
resolved: "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.13.tgz#dcfc826beef65e75c50e21d3837d7d95798dd658",
descriptors: vec![("@babel/code-frame", "^7.0.0")],
dependencies: vec![("@babel/highlight", "^7.12.13")],
integrity: "sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==",
Expand All @@ -770,6 +826,7 @@ __metadata:
Entry {
name: "@babel/code-frame",
version: "7.12.13",
resolved: "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.13.tgz#dcfc826beef65e75c50e21d3837d7d95798dd658",
descriptors: vec![("@babel/code-frame", "^7.0.0")],
dependencies: vec![("@babel/highlight", "^7.12.13")],
integrity: "sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g=="
Expand All @@ -787,6 +844,7 @@ __metadata:
Entry {
name: "@babel/helper-validator-identifier",
version: "7.12.11",
resolved: "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.12.11.tgz#c9a1f021917dcb5ccf0d4e453e399022981fc9ed",
descriptors: vec![("@babel/helper-validator-identifier", "^7.12.11")],
integrity: "sha512-np/lG3uARFybkoHokJUmf1QfEvRVCPbmQeUQpKow5cQ3xWrV9i3rUHodKDJPQfTVX61qKi+UdYk8kik84n7XOw==",
..Default::default()
Expand All @@ -804,6 +862,7 @@ __metadata:
Entry {
name: "@babel/helper-validator-identifier",
version: "7.12.11",
resolved: "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.12.11.tgz#c9a1f021917dcb5ccf0d4e453e399022981fc9ed",
descriptors: vec![("@babel/helper-validator-identifier", "^7.12.11")],
integrity: "sha512-np/lG3uARFybkoHokJUmf1QfEvRVCPbmQeUQpKow5cQ3xWrV9i3rUHodKDJPQfTVX61qKi+UdYk8kik84n7XOw==",
..Default::default()
Expand All @@ -823,6 +882,7 @@ __metadata:
Entry {
name: "@babel/code-frame",
version: "7.12.13",
resolved: "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.13.tgz#dcfc826beef65e75c50e21d3837d7d95798dd658",
descriptors: vec![("@babel/code-frame", "^7.0.0")],
dependencies: vec![("@babel/highlight", "^7.12.13")],
integrity: "sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==",
Expand All @@ -836,6 +896,7 @@ __metadata:
Entry {
name: "@babel/code-frame",
version: "7.12.13",
resolved: "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.13.tgz#dcfc826beef65e75c50e21d3837d7d95798dd658",
descriptors: vec![("@babel/code-frame", "^7.0.0")],
dependencies: vec![("@babel/highlight", "^7.12.13")],
integrity: "sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==",
Expand All @@ -856,6 +917,7 @@ __metadata:
Entry {
name: "@babel/code-frame",
version: "7.12.13",
resolved: "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.13.tgz#dcfc826beef65e75c50e21d3837d7d95798dd658",
descriptors: vec![("@babel/code-frame", "^7.0.0")],
dependencies: vec![("@babel/highlight", "^7.12.13")],
integrity: "sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==",
Expand All @@ -881,6 +943,7 @@ __metadata:
Entry {
name: "@babel/code-frame",
version: "7.12.13",
resolved: "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.13.tgz#dcfc826beef65e75c50e21d3837d7d95798dd658",
descriptors: vec![("@babel/code-frame", "^7.0.0")],
dependencies: vec![("@babel/highlight", "^7.12.13")],
integrity: "sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==",
Expand Down Expand Up @@ -1099,6 +1162,7 @@ __metadata:
&Entry {
name: "minimatch",
version: "10.0.1",
resolved: "https://github.com/isaacs/minimatch.git#0569cd3373408f9d701d3aab187b3f43a24a0db7",
integrity: "",
dependencies: vec![("brace-expansion", "^2.0.1")],
descriptors: vec![(
Expand Down

0 comments on commit d18f930

Please sign in to comment.