Skip to content

Commit 33f52e3

Browse files
committed
feat(rust): add basic validate for rocket rs framewrok
1 parent 76eab2e commit 33f52e3

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Languages Stages (Welcome to PR your usage languages)
1919
| http api decl || 🆕 | 🆕 ||| 🆕 | 🆕 | | | |
2020
| syntax parse |||||| 🆕 | 🆕 || 🆕 | 🆕 |
2121
| function call || 🆕 | 🆕 | 🆕 || | | | | 🆕 |
22-
| arch/package || | ||| | || | |
22+
| arch/package || | ||| | || | 🆕 |
2323
| real world || | | 🆕 || | | | | |
2424

2525
language versions(tested):

chapi-ast-rust/src/main/kotlin/chapi/ast/rustast/RustAstBaseListener.kt

+6
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ open class RustAstBaseListener(private val fileName: String) : RustParserBaseLis
221221
Value = value
222222
)
223223
}
224+
is RustParser.MacroLiteralTokenContext -> {
225+
AnnotationKeyValue(
226+
Key = child.text,
227+
Value = child.text
228+
)
229+
}
224230

225231
else -> {
226232
null

chapi-ast-rust/src/test/kotlin/chapi/ast/rustast/RustFullIdentListenerTest.kt

+33
Original file line numberDiff line numberDiff line change
@@ -339,4 +339,37 @@ class RustFullIdentListenerTest {
339339

340340
assertEquals("CmykColor", codeDataStruct.Fields[4].TypeValue)
341341
}
342+
343+
@Test
344+
fun should_identify_rocket_rs_url() {
345+
val code = """
346+
#[macro_use] extern crate rocket;
347+
348+
#[get("/hello/<name>/<age>")]
349+
fn hello(name: &str, age: u8) -> String {
350+
format!("Hello, {} year old named {}!", age, name)
351+
}
352+
353+
#[launch]
354+
fn rocket() -> _ {
355+
rocket::build().mount("/", routes![hello])
356+
}
357+
""".trimIndent()
358+
359+
val codeContainer = RustAnalyser().analysis(code, "lib.rs")
360+
val codeDataStruct = codeContainer.DataStructures[0]
361+
val firstFunction = codeDataStruct.Functions[0]
362+
// annotation
363+
assertEquals(1, firstFunction.Annotations.size)
364+
assertEquals("get", firstFunction.Annotations[0].Name)
365+
366+
assertEquals(1, firstFunction.Annotations[0].KeyValues.size)
367+
assertEquals("\"/hello/<name>/<age>\"", firstFunction.Annotations[0].KeyValues[0].Value)
368+
369+
val secondFunction = codeDataStruct.Functions[1]
370+
assertEquals("rocket", secondFunction.Name)
371+
assertEquals("_", secondFunction.ReturnType)
372+
assertEquals(2, secondFunction.FunctionCalls.size)
373+
assertEquals("rocket::build", secondFunction.FunctionCalls[0].NodeName)
374+
}
342375
}

0 commit comments

Comments
 (0)