github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/language/rust/cargolock/cargolock.go (about)

     1  // Copyright 2025 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package cargolock extracts Cargo.lock files for rust projects
    16  package cargolock
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"path/filepath"
    22  
    23  	"github.com/BurntSushi/toml"
    24  	"github.com/google/osv-scalibr/extractor"
    25  	"github.com/google/osv-scalibr/extractor/filesystem"
    26  	"github.com/google/osv-scalibr/inventory"
    27  	"github.com/google/osv-scalibr/plugin"
    28  	"github.com/google/osv-scalibr/purl"
    29  )
    30  
    31  const (
    32  	// Name is the unique name of this extractor.
    33  	Name = "rust/cargolock"
    34  )
    35  
    36  type cargoLockPackage struct {
    37  	Name    string `toml:"name"`
    38  	Version string `toml:"version"`
    39  }
    40  
    41  type cargoLockFile struct {
    42  	Version  int                `toml:"version"`
    43  	Packages []cargoLockPackage `toml:"package"`
    44  }
    45  
    46  // Extractor extracts crates.io packages from Cargo.lock files.
    47  type Extractor struct{}
    48  
    49  // New returns a new instance of the extractor.
    50  func New() filesystem.Extractor { return &Extractor{} }
    51  
    52  // Name of the extractor
    53  func (e Extractor) Name() string { return Name }
    54  
    55  // Version of the extractor
    56  func (e Extractor) Version() int { return 0 }
    57  
    58  // FileRequired returns true if the specified file matches Cargo lockfile patterns.
    59  func (e Extractor) FileRequired(api filesystem.FileAPI) bool {
    60  	return filepath.Base(api.Path()) == "Cargo.lock"
    61  }
    62  
    63  // Requirements of the extractor
    64  func (e Extractor) Requirements() *plugin.Capabilities {
    65  	return &plugin.Capabilities{}
    66  }
    67  
    68  // Extract extracts packages from Cargo.lock files passed through the scan input.
    69  func (e Extractor) Extract(_ context.Context, input *filesystem.ScanInput) (inventory.Inventory, error) {
    70  	var parsedLockfile *cargoLockFile
    71  
    72  	_, err := toml.NewDecoder(input.Reader).Decode(&parsedLockfile)
    73  
    74  	if err != nil {
    75  		return inventory.Inventory{}, fmt.Errorf("could not extract: %w", err)
    76  	}
    77  
    78  	packages := make([]*extractor.Package, 0, len(parsedLockfile.Packages))
    79  
    80  	for _, lockPackage := range parsedLockfile.Packages {
    81  		packages = append(packages, &extractor.Package{
    82  			Name:      lockPackage.Name,
    83  			Version:   lockPackage.Version,
    84  			PURLType:  purl.TypeCargo,
    85  			Locations: []string{input.Path},
    86  		})
    87  	}
    88  
    89  	return inventory.Inventory{Packages: packages}, nil
    90  }
    91  
    92  var _ filesystem.Extractor = Extractor{}