From 40d629165a1b64026505ac4ce3d738a6515b471e Mon Sep 17 00:00:00 2001 From: LN Liberda Date: Sun, 8 Dec 2024 04:48:09 +0100 Subject: [PATCH] feat: add resolved (resolution) field to Entry --- src/lib.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d55a7f0..55b320f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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}, @@ -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)>, @@ -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), @@ -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> { @@ -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(_) => (), @@ -249,6 +259,7 @@ fn parse_entry(input: &str) -> Res<&str, Entry> { Entry { name, version, + resolved, integrity, dependencies, descriptors, @@ -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" @@ -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==" @@ -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"), @@ -535,6 +572,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==" @@ -542,6 +580,7 @@ mod tests { 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() @@ -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" } @@ -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"), @@ -656,6 +705,7 @@ __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() @@ -663,6 +713,7 @@ __metadata: 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", @@ -670,6 +721,7 @@ __metadata: 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" @@ -712,6 +764,7 @@ __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")], @@ -719,6 +772,7 @@ __metadata: 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![], @@ -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==", @@ -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==", @@ -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==" @@ -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() @@ -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() @@ -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==", @@ -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==", @@ -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==", @@ -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==", @@ -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![(