Skip to content

Commit 57e6b83

Browse files
asurkovanatolystansler
authored andcommitted
feat: add rust extractor (#285)
* feat - add rust extractor * address comments
1 parent 5a2aba2 commit 57e6b83

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2018 Sourcerer Inc. All Rights Reserved.
2+
// Author: Alexander Surkov ([email protected])
3+
4+
package app.extractors
5+
6+
import app.model.CommitStats
7+
import app.model.DiffFile
8+
9+
class RustExtractor : ExtractorInterface {
10+
companion object {
11+
const val LANGUAGE_NAME = Lang.Rust
12+
val evaluator by lazy {
13+
ExtractorInterface.getLibraryClassifier(LANGUAGE_NAME)
14+
}
15+
val importRegex = Regex("""^extern crate \w+;$""")
16+
val commentRegex = Regex("(//.+$)|(/[*].*?[*]/)")
17+
val extractImportRegex = Regex("""^extern crate (\w+);$""")
18+
}
19+
20+
override fun extract(files: List<DiffFile>): List<CommitStats> {
21+
files.map { file -> file.language = LANGUAGE_NAME }
22+
return super.extract(files)
23+
}
24+
25+
override fun extractImports(fileContent: List<String>): List<String> {
26+
val imports = mutableSetOf<String>()
27+
28+
fileContent.forEach {
29+
val res = extractImportRegex.find(it)
30+
if (res != null) {
31+
val lineLib = res.groupValues[1]
32+
imports.add(lineLib)
33+
}
34+
}
35+
36+
return imports.toList()
37+
}
38+
39+
override fun tokenize(line: String): List<String> {
40+
var newLine = importRegex.replace(line, "")
41+
newLine = commentRegex.replace(newLine, "")
42+
return super.tokenize(newLine)
43+
}
44+
45+
override fun getLineLibraries(line: String,
46+
fileLibraries: List<String>): List<String> {
47+
48+
return super.getLineLibraries(line, fileLibraries, evaluator,
49+
LANGUAGE_NAME)
50+
}
51+
}

src/test/kotlin/test/tests/extractors/ExtractorTest.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,22 @@ class ExtractorTest : Spek({
278278
}
279279
}
280280

281+
// Rust
282+
given("Rust") {
283+
it("Rust imports") {
284+
var lines = listOf(
285+
"extern crate foo;",
286+
"extern crate boo;"
287+
)
288+
var actualLineImports = RustExtractor().extractImports(lines)
289+
assertEquals(actualLineImports, listOf("foo", "boo"))
290+
291+
lines = listOf(
292+
"// extern crate foo;",
293+
"/* extern crate boo; */"
294+
)
295+
actualLineImports = RustExtractor().extractImports(lines)
296+
assertTrue(actualLineImports.isEmpty())
297+
}
298+
}
281299
})

0 commit comments

Comments
 (0)