Skip to content

Commit 4f37987

Browse files
committed
feat: replace Cargo.toml parser
1 parent a2b0237 commit 4f37987

File tree

1 file changed

+2
-40
lines changed

1 file changed

+2
-40
lines changed

pkg/lockfile/parse-cargo-lock.go

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,11 @@
11
package lockfile
22

33
import (
4-
"fmt"
5-
"os"
6-
7-
"github.com/BurntSushi/toml"
4+
"github.com/google/osv-scalibr/extractor/filesystem/language/rust/cargolock"
85
)
96

10-
type CargoLockPackage struct {
11-
Name string `toml:"name"`
12-
Version string `toml:"version"`
13-
}
14-
15-
type CargoLockFile struct {
16-
Version int `toml:"version"`
17-
Packages []CargoLockPackage `toml:"package"`
18-
}
19-
207
const CargoEcosystem Ecosystem = "crates.io"
218

229
func ParseCargoLock(pathToLockfile string) ([]PackageDetails, error) {
23-
var parsedLockfile *CargoLockFile
24-
25-
lockfileContents, err := os.ReadFile(pathToLockfile)
26-
27-
if err != nil {
28-
return []PackageDetails{}, fmt.Errorf("could not read %s: %w", pathToLockfile, err)
29-
}
30-
31-
err = toml.Unmarshal(lockfileContents, &parsedLockfile)
32-
33-
if err != nil {
34-
return []PackageDetails{}, fmt.Errorf("could not parse %s: %w", pathToLockfile, err)
35-
}
36-
37-
packages := make([]PackageDetails, 0, len(parsedLockfile.Packages))
38-
39-
for _, lockPackage := range parsedLockfile.Packages {
40-
packages = append(packages, PackageDetails{
41-
Name: lockPackage.Name,
42-
Version: lockPackage.Version,
43-
Ecosystem: CargoEcosystem,
44-
CompareAs: CargoEcosystem,
45-
})
46-
}
47-
48-
return packages, nil
10+
return extract(pathToLockfile, cargolock.New(), CargoEcosystem)
4911
}

0 commit comments

Comments
 (0)