Skip to content

Commit

Permalink
Merge pull request #212 from Siphalor/patch-language-rust
Browse files Browse the repository at this point in the history
Add Rust library detection support
  • Loading branch information
peti2001 authored Jun 22, 2022
2 parents 8c3c8aa + 765a361 commit 4ad738c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions extractor/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func (r *RepoExtractor) initAnalyzers() {
librarydetection.AddAnalyzer("PHP", languages.NewPHPAnalyzer())
librarydetection.AddAnalyzer("Python", languages.NewPythonScriptAnalyzer())
librarydetection.AddAnalyzer("Ruby", languages.NewRubyScriptAnalyzer())
librarydetection.AddAnalyzer("Rust", languages.NewRustAnalyzer())
librarydetection.AddAnalyzer("Swift", languages.NewSwiftAnalyzer())
}

Expand Down
30 changes: 30 additions & 0 deletions librarydetection/languages/Rust.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package languages

import (
"github.com/codersrank-org/repo_info_extractor/v2/librarydetection"
"regexp"
)

// NewRustAnalyzer constructor
func NewRustAnalyzer() librarydetection.Analyzer {
return &rustAnalyzer{}
}

type rustAnalyzer struct {
}

func (a *rustAnalyzer) ExtractLibraries(contents string) ([]string, error) {
// regex to find "uses", braces are not supported as their tree syntax is too complicated for Go's regexes
useRegex, err := regexp.Compile(`use\s+(?:::)?(?:r#)?([^;:\s]+)`)
if err != nil {
return nil, err
}

// regex for "extern crate" statements
externCrateRegex, err := regexp.Compile(`extern\s+crate\s+(?:r#)?(\S+)\s*;`)
if err != nil {
return nil, err
}

return executeRegexes(contents, []*regexp.Regexp{useRegex, externCrateRegex}), nil
}
36 changes: 36 additions & 0 deletions librarydetection/languages/Rust_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package languages_test

import (
. "github.com/onsi/ginkgo"
"io/ioutil"

"github.com/codersrank-org/repo_info_extractor/v2/librarydetection/languages"
)

var _ = Describe("RustLibraryDetection", func() {
fixture, err := ioutil.ReadFile("./fixtures/rust.fixture")
if err != nil {
panic(err)
}

expectedLibraries := []string{
"lib1",
"lib2",
"lib3",
"lib4",
"lib5",
"lib6",
}

analyzer := languages.NewRustAnalyzer()

Describe("Extract Rust Libraries", func() {
It("Should be able to extract libraries", func() {
libs, err := analyzer.ExtractLibraries(string(fixture))
if err != nil {
panic(err)
}
assertSameUnordered(libs, expectedLibraries)
})
})
})
11 changes: 11 additions & 0 deletions librarydetection/languages/fixtures/rust.fixture
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extern crate lib1;
#[macro_use]
extern crate lib2;

use lib3::io::test;
use lib4 as something;
// whitespace action
use
lib5::test as hi;
// global path with a raw name
use ::r#lib6;

0 comments on commit 4ad738c

Please sign in to comment.