Closed senkenn closed 7 months ago
ast expoler https://astexplorer.net/
https://docs.rs/syn/latest/syn/fn.parse_file.html it can parse file content to last.
sample code
pub fn extract_sql_list(source_txt: String) -> Vec<String> {
let mut sql_nodes = Vec::new();
let ast = syn::parse_file(&source_txt).unwrap();
if let Some(shebang) = ast.shebang {
println!("{}", shebang);
}
println!("{} items", ast.items.len());
sql_nodes
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = extract_sql_list(
r##"
async fn add_todo(pool: &PgPool, description: String) -> anyhow::Result<i64> {
let rec = sqlx::query!(
r#"
INSERT INTO todos ( description )
VALUES ( $1 )
RETURNING id
"#,
description
)
.fetch_one(pool)
.await?;
Ok(rec.id)
}
"##
.to_string(),
);
}
}
https://docs.rs/syn/latest/syn/visit/index.html
there is visit function similar to ts compiler api
syn cannot get the parent node...
close with https://github.com/senkenn/sqlsurge/pull/20
https://github.com/mtshiba/ruast There is a implementation by shiba.