-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #212 from Siphalor/patch-language-rust
Add Rust library detection support
- Loading branch information
Showing
4 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |