Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 98 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@
pub resolved: &'a str,
pub integrity: &'a str,
pub dependencies: Vec<(&'a str, &'a str)>,
pub optional_dependencies: Vec<(&'a str, &'a str)>,
pub descriptors: Vec<(&'a str, &'a str)>,
}

/// Accepts the `yarn.lock` content and returns all the entries.
/// # Errors
/// - `YarnLockError`
pub fn parse_str(content: &str) -> Result<Lockfile, YarnLockError> {

Check warning on line 62 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 62 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 62 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, macOS-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 62 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, windows-latest)

hiding a lifetime that's elided elsewhere is confusing
parse(content).map(|(_, entries)| entries).map_err(|e| {
e.map(|ve| {
let errors = ve
Expand All @@ -72,7 +73,7 @@
})
}

fn parse(input: &str) -> Res<&str, Lockfile> {

Check warning on line 76 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 76 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 76 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, macOS-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 76 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, windows-latest)

hiding a lifetime that's elided elsewhere is confusing
let (i, (is_bun, is_v1)) = yarn_lock_header(input)?;
let (i, version) = cond(!is_v1, yarn_lock_metadata).parse(i)?;
let (i, mut entries) = many0(entry).parse(i)?;
Expand Down Expand Up @@ -162,7 +163,7 @@
.parse(input)
}

fn entry_final(input: &str) -> Res<&str, Entry> {

Check warning on line 166 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 166 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 166 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, macOS-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 166 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, windows-latest)

hiding a lifetime that's elided elsewhere is confusing
recognize(many_till(take_till_optional_line_end, eof))
.parse(input)
.and_then(|(i, capture)| {
Expand All @@ -171,7 +172,7 @@
})
}

fn entry(input: &str) -> Res<&str, Entry> {

Check warning on line 175 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 175 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 175 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, macOS-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 175 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, windows-latest)

hiding a lifetime that's elided elsewhere is confusing
recognize(many_till(
take_till_line_end,
recognize((space0, line_ending)),
Expand All @@ -188,15 +189,16 @@
Version(&'a str),
Resolved(&'a str),
Dependencies(Vec<(&'a str, &'a str)>),
OptionalDependencies(Vec<(&'a str, &'a str)>),
Integrity(&'a str),
Unknown(&'a str),
}

fn unknown_line(input: &str) -> Res<&str, EntryItem> {

Check warning on line 197 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 197 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 197 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, macOS-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 197 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, windows-latest)

hiding a lifetime that's elided elsewhere is confusing
take_till_line_end(input).map(|(i, res)| (i, EntryItem::Unknown(res)))
}

fn integrity(input: &str) -> Res<&str, EntryItem> {

Check warning on line 201 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 201 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 201 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, macOS-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 201 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, windows-latest)

hiding a lifetime that's elided elsewhere is confusing
context(
"integrity",
(
Expand All @@ -214,10 +216,11 @@
.map(|(i, (_, _, _, _, _, _, _, integrity))| (i, EntryItem::Integrity(integrity)))
}

fn entry_item(input: &str) -> Res<&str, EntryItem> {

Check warning on line 219 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 219 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 219 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, macOS-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 219 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, windows-latest)

hiding a lifetime that's elided elsewhere is confusing
alt((
entry_version,
parse_dependencies,
parse_optional_dependencies,
integrity,
entry_resolved,
unknown_line,
Expand All @@ -225,7 +228,7 @@
.parse(input)
}

fn parse_entry(input: &str) -> Res<&str, Entry> {

Check warning on line 231 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 231 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 231 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, macOS-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 231 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, windows-latest)

hiding a lifetime that's elided elsewhere is confusing
context("entry", (entry_descriptors, many1(entry_item)))
.parse(input)
.and_then(|(next_input, res)| {
Expand All @@ -239,13 +242,15 @@
let mut version = "";
let mut resolved = "";
let mut dependencies = Vec::new();
let mut optional_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::OptionalDependencies(d) => optional_dependencies = d,
EntryItem::Integrity(c) => integrity = c,
EntryItem::Unknown(_) => (),
}
Expand All @@ -266,6 +271,7 @@
resolved,
integrity,
dependencies,
optional_dependencies,
descriptors,
},
))
Expand All @@ -276,7 +282,7 @@
alt((double_quoted_text, not_line_ending)).parse(input)
}

fn parse_dependencies(input: &str) -> Res<&str, EntryItem> {

Check warning on line 285 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 285 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 285 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, macOS-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 285 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, windows-latest)

hiding a lifetime that's elided elsewhere is confusing
let (input, (indent, _, _)) = (space1, tag("dependencies:"), line_ending).parse(input)?;

let dependencies_parser = many1(move |i| {
Expand All @@ -297,6 +303,28 @@
.map(|(i, res)| (i, EntryItem::Dependencies(res)))
}

fn parse_optional_dependencies(input: &str) -> Res<&str, EntryItem> {

Check warning on line 306 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 306 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 306 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, macOS-latest)

hiding a lifetime that's elided elsewhere is confusing

Check warning on line 306 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable, windows-latest)

hiding a lifetime that's elided elsewhere is confusing
let (input, (indent, _, _)) =
(space1, tag("optionalDependencies:"), line_ending).parse(input)?;

let optional_dependencies_parser = many1(move |i| {
(
tag(indent), // indented as much as the parent...
space1, // ... plus extra indentation
is_not(": "), // package name
one_of(": "),
space0,
dependency_version, // version
alt((line_ending, space0)), // newline or space
)
.parse(i)
.map(|(i, (_, _, p, _, _, v, _))| (i, (p.trim_matches('"'), v)))
});
context("optionalDependencies", optional_dependencies_parser)
.parse(input)
.map(|(i, res)| (i, EntryItem::OptionalDependencies(res)))
}

/**
* Simple version, it doesn't consider escaped quotes since in our scenarios
* it can't happen.
Expand Down Expand Up @@ -452,7 +480,8 @@
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=="
integrity: "sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==",
..Default::default()
}
);

Expand All @@ -478,7 +507,8 @@
("y18n", "^3.2.1"),
("yargs-parser", "^7.0.0"),
],
integrity: "sha1-UqzCP+7Kw0BCB47njAwAf1CF20w="
integrity: "sha1-UqzCP+7Kw0BCB47njAwAf1CF20w=",
..Default::default()
}
);
}
Expand Down Expand Up @@ -565,6 +595,15 @@
dependencies:
"@babel/highlight" "^7.12.13"

cli-table3@~0.6.1:
version "0.6.5"
resolved "https://registry.yarnpkg.com/cli-table3/-/cli-table3-0.6.5.tgz#013b91351762739c16a9567c21a04632e449bf2f"
integrity sha512-+W/5efTR7y5HRD7gACw9yQjqMVvEMLBHmboM/kPWam+H+Hmyrgjh6YncVKK122YZkXrLudzTuAukUw9FnMf7IQ==
dependencies:
string-width "^4.2.0"
optionalDependencies:
"@colors/colors" "1.5.0"

"@babel/helper-validator-identifier@^7.12.11":
version "7.12.11"
resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.12.11.tgz#c9a1f021917dcb5ccf0d4e453e399022981fc9ed"
Expand All @@ -577,7 +616,17 @@
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=="
integrity: "sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==",
..Default::default()
},
Entry {
name: "cli-table3",
version: "0.6.5",
resolved: "https://registry.yarnpkg.com/cli-table3/-/cli-table3-0.6.5.tgz#013b91351762739c16a9567c21a04632e449bf2f",
descriptors: vec![("cli-table3", "~0.6.1")],
dependencies: vec![("string-width", "^4.2.0")],
optional_dependencies: vec![("@colors/colors", "1.5.0")],
integrity: "sha512-+W/5efTR7y5HRD7gACw9yQjqMVvEMLBHmboM/kPWam+H+Hmyrgjh6YncVKK122YZkXrLudzTuAukUw9FnMf7IQ=="
},
Entry {
name: "@babel/helper-validator-identifier",
Expand Down Expand Up @@ -614,7 +663,8 @@
"sha512-TDCmlK5eOvH+eH7cdAFlNXeVJqWIQ7gW9tY1GJIpUtFb6CmjVyq2VM3u71bOyR8CRihcCgMUYoDNyLXao3+70Q=="
} else {
"195e2be3172d7684bf95cff69ae3b7a15a9841ea9d27d3c843662d50cdd7d6470fd9c8e64be84d031117e4a4083486effba39f9aef6bbb2c89f7f21bcfba33ba"
}
},
..Default::default()
}
);

Expand Down Expand Up @@ -642,7 +692,8 @@
"sha512-t6YAJcxDkNX7NFYiVtKvWUz8l+PaKTLiL63mJYWR2GnHq2gjEWISzsLp9wg3aY36dY1j+gfIEL3pIF+XlJJfbA=="
} else {
"00d58a2c052937fa044834313f07910fd0a115dec5ee35919e857eeee3736b21a4eafa8264535800ba8bac312991ce785ecb8a51f4d2cc8c4676d865af1cfbde"
}
},
..Default::default()
}
);
}
Expand Down Expand Up @@ -719,14 +770,16 @@
descriptors: vec![("@babel/plugin-transform-for-of", "npm:^7.12.1")],
dependencies: vec![("@babel/helper-plugin-utils", "^7.16.7")],
integrity: "35c9264ee4bef814818123d70afe8b2f0a85753a0a9dc7b73f93a71cadc5d7de852f1a3e300a7c69a491705805704611de1e2ccceb5686f7828d6bca2e5a7306",
..Default::default()
},
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"
integrity: "4d56bdb82890f386d5a57c40ef985a0ed7f0a78f789377a2d0c3e8826819e0f7f16ba0fe906d9b2241c5f7ca56630ef0653f5bb99f03771f7b87ff8af4bf5fe3",
..Default::default()
},
],
);
Expand Down Expand Up @@ -770,6 +823,7 @@
integrity: "",
descriptors: vec![("foo", "workspace:.")],
dependencies: vec![("valib-aliased", "npm:valib@1.0.0 || 1.0.1")],
..Default::default()
},
Entry {
name: "valib-aliased",
Expand All @@ -778,6 +832,7 @@
integrity: "ad4f5a0b5dde5ab5e3cc87050fad4d7096c32797454d8e37c7dadf3455a43a7221a3caaa0ad9e72b8cd96668168e5a25d5f0072e21990f7f80a64b1a4e34e921",
descriptors: vec![("valib-aliased", "npm:valib@1.0.0 || 1.0.1")],
dependencies: vec![],
..Default::default()
},
],
);
Expand Down Expand Up @@ -831,7 +886,8 @@
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=="
integrity: "sha512-HV1Cm0Q3ZrpCR93tkWOYiuYIgLxZXZFVG2VgK+MBWjUqZTundupbfx2aXarXuw5Ko5aMcjtJgbSs4vUGBS5v6g==",
..Default::default()
},
);

Expand Down Expand Up @@ -1129,6 +1185,38 @@
);
}

#[test]
fn parse_optional_dependencies_work() {
fn assert(input: &str, expect: EntryItem) {
let res = parse_optional_dependencies(input).unwrap();
assert_eq!(res.1, expect);
}

assert(
r#" optionalDependencies:
foo "1.0"
"bar" "0.3-alpha1"
"#,
EntryItem::OptionalDependencies(vec![("foo", "1.0"), ("bar", "0.3-alpha1")]),
);

assert(
r#" optionalDependencies:
foo "1.0 || 2.0"
"bar" "0.3-alpha1"
"#,
EntryItem::OptionalDependencies(vec![("foo", "1.0 || 2.0"), ("bar", "0.3-alpha1")]),
);

assert(
r#" optionalDependencies:
foo: 1.0 || 2.0
"bar": "0.3-alpha1"
"#,
EntryItem::OptionalDependencies(vec![("foo", "1.0 || 2.0"), ("bar", "0.3-alpha1")]),
);
}

#[test]
fn take_till_the_end_works() {
let k = take_till_line_end("foo\r\nbar").unwrap();
Expand Down Expand Up @@ -1171,6 +1259,7 @@
"minimatch",
"https://github.com/isaacs/minimatch.git#v10.0.1"
)],
..Default::default()
}
);
}
Expand All @@ -1192,6 +1281,7 @@
"node-semver",
"ssh://git@github.com/npm/node-semver.git#semver:^7.5.0"
)],
..Default::default()
}
);
}
Expand All @@ -1214,6 +1304,7 @@
"@a/verboden(name~'!*)",
"https://s.lnl.gay/@a/verboden(name~'!*)/-/verboden(name~'!*)-1.0.0.tgz"
),],
..Default::default()
}
)
}
Expand Down
Loading