Skip to content

Commit 7b4f376

Browse files
committed
fix: use a proper toml parser
1 parent 33ee13a commit 7b4f376

3 files changed

Lines changed: 25 additions & 50 deletions

File tree

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"https-proxy-agent": "^7.0.6",
5757
"node-fetch": "^3.3.2",
5858
"packageurl-js": "~1.0.2",
59+
"smol-toml": "^1.6.0",
5960
"tree-sitter-requirements": "github:Strum355/tree-sitter-requirements#d0261ee76b84253997fe70d7d397e78c006c3801",
6061
"web-tree-sitter": "^0.26.6",
6162
"yargs": "^18.0.0"

src/providers/rust_cargo.js

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'node:fs'
22
import path from 'node:path'
33

44
import { PackageURL } from 'packageurl-js'
5+
import { parse as parseToml } from 'smol-toml'
56

67
import { getLicense } from '../license/license_utils.js'
78
import Sbom from '../sbom.js'
@@ -83,26 +84,11 @@ function readLicenseFromManifest(manifestPath) {
8384
let fromManifest = null
8485
try {
8586
let content = fs.readFileSync(manifestPath, 'utf-8')
86-
let lines = content.split(/\r?\n/)
87-
let currentSection = ''
87+
let parsed = parseToml(content)
8888

89-
for (let line of lines) {
90-
let trimmed = line.trim()
91-
92-
let sectionMatch = trimmed.match(/^\[([^\]]+)\]\s*(?:#.*)?$/)
93-
if (sectionMatch) {
94-
currentSection = sectionMatch[1]
95-
continue
96-
}
97-
98-
if (currentSection === 'package' || currentSection === 'workspace.package') {
99-
let licenseMatch = trimmed.match(/^license\s*=\s*"([^"]+)"/)
100-
if (licenseMatch) {
101-
fromManifest = licenseMatch[1].trim()
102-
break
103-
}
104-
}
105-
}
89+
fromManifest = parsed.package?.license
90+
|| parsed.workspace?.package?.license
91+
|| null
10692
} catch (_) {
10793
// leave fromManifest as null
10894
}
@@ -504,35 +490,13 @@ function findResolveNode(metadata, packageId) {
504490
* @private
505491
*/
506492
function getWorkspaceDepsFromManifest(manifest) {
507-
let content
508493
try {
509-
content = fs.readFileSync(manifest, 'utf-8')
494+
let content = fs.readFileSync(manifest, 'utf-8')
495+
let parsed = parseToml(content)
496+
return Object.keys(parsed.workspace?.dependencies || {})
510497
} catch (_) {
511498
return []
512499
}
513-
514-
let lines = content.split(/\r?\n/)
515-
let inSection = false
516-
let names = []
517-
518-
for (let line of lines) {
519-
let trimmed = line.trim()
520-
521-
let sectionMatch = trimmed.match(/^\[([^\]]+)\]\s*(?:#.*)?$/)
522-
if (sectionMatch) {
523-
inSection = sectionMatch[1] === 'workspace.dependencies'
524-
continue
525-
}
526-
527-
if (inSection) {
528-
let nameMatch = trimmed.match(/^([a-zA-Z0-9_-]+)\s*=/)
529-
if (nameMatch) {
530-
names.push(nameMatch[1])
531-
}
532-
}
533-
}
534-
535-
return names
536500
}
537501

538502
/**
@@ -546,14 +510,11 @@ function getWorkspaceVersion(metadata) {
546510
let cargoTomlPath = path.join(workspaceRoot, 'Cargo.toml')
547511
try {
548512
let content = fs.readFileSync(cargoTomlPath, 'utf-8')
549-
let versionMatch = content.match(/\[workspace\.package\][\s\S]*?version\s*=\s*"([^"]+)"/)
550-
if (versionMatch) {
551-
return versionMatch[1]
552-
}
513+
let parsed = parseToml(content)
514+
return parsed.workspace?.package?.version || '0.0.0'
553515
} catch (_) {
554-
// fall through to default
516+
return '0.0.0'
555517
}
556-
return '0.0.0'
557518
}
558519

559520
/**

0 commit comments

Comments
 (0)