CHANGE LOCK TO NOT BE DROPPED INSTANTLY. DANG U RUST

co-authored-by: kay@zed.dev
This commit is contained in:
Mikayla Maki 2022-12-02 12:43:02 -08:00
parent 5e240f98f0
commit 5262e8c77e
6 changed files with 39 additions and 29 deletions

View file

@ -10,9 +10,37 @@ lazy_static::lazy_static! {
#[proc_macro]
pub fn sql(tokens: TokenStream) -> TokenStream {
let (spans, sql) = make_sql(tokens);
let error = SQLITE.sql_has_syntax_error(sql.trim());
let formatted_sql = sqlformat::format(&sql, &sqlformat::QueryParams::None, Default::default());
if let Some((error, error_offset)) = error {
create_error(spans, error_offset, error, &formatted_sql)
} else {
format!("r#\"{}\"#", &formatted_sql).parse().unwrap()
}
}
fn create_error(
spans: Vec<(usize, Span)>,
error_offset: usize,
error: String,
formatted_sql: &String,
) -> TokenStream {
let error_span = spans
.into_iter()
.skip_while(|(offset, _)| offset <= &error_offset)
.map(|(_, span)| span)
.next()
.unwrap_or(Span::call_site());
let error_text = format!("Sql Error: {}\nFor Query: {}", error, formatted_sql);
TokenStream::from(Error::new(error_span.into(), error_text).into_compile_error())
}
fn make_sql(tokens: TokenStream) -> (Vec<(usize, Span)>, String) {
let mut sql_tokens = vec![];
flatten_stream(tokens.clone(), &mut sql_tokens);
// Lookup of spans by offset at the end of the token
let mut spans: Vec<(usize, Span)> = Vec::new();
let mut sql = String::new();
@ -20,23 +48,7 @@ pub fn sql(tokens: TokenStream) -> TokenStream {
sql.push_str(&token_text);
spans.push((sql.len(), span));
}
let error = SQLITE.sql_has_syntax_error(sql.trim());
let formatted_sql = sqlformat::format(&sql, &sqlformat::QueryParams::None, Default::default());
if let Some((error, error_offset)) = error {
let error_span = spans
.into_iter()
.skip_while(|(offset, _)| offset <= &error_offset)
.map(|(_, span)| span)
.next()
.unwrap_or(Span::call_site());
let error_text = format!("Sql Error: {}\nFor Query: {}", error, formatted_sql);
TokenStream::from(Error::new(error_span.into(), error_text).into_compile_error())
} else {
format!("r#\"{}\"#", &formatted_sql).parse().unwrap()
}
(spans, sql)
}
/// This method exists to normalize the representation of groups