Yuri Astrakhan a824e8468c
Cleanup format arguments (#2650)
Inlined format args make code more readable, and code more compact.

I ran this clippy command to fix most cases, and then cleaned up a few trailing commas and uncaught edge cases.

```
cargo clippy --bins --examples  --benches --tests --lib --workspace --fix -- -A clippy::all -W clippy::uninlined_format_args
```
2023-07-31 13:27:04 -07:00

49 lines
1.3 KiB
Rust

use sqlx::{query_file, query_file_as, query_file_unchecked, FromRow, PgPool};
use std::fmt::{Display, Formatter};
#[derive(FromRow)]
struct PostWithAuthorQuery {
pub post_id: i64,
pub title: String,
pub body: String,
pub author_id: i64,
pub author_username: String,
}
impl Display for PostWithAuthorQuery {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
r#"
post_id: {},
title: {},
body: {},
author_id: {},
author_username: {}
"#,
self.post_id, self.title, self.body, self.author_id, self.author_username
)
}
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
let pool = PgPool::connect(&dotenvy::var("DATABASE_URL")?).await?;
// we can use a tranditional wrapper around the `query!()` macro using files
query_file!("queries/insert_seed_data.sql")
.execute(&pool)
.await?;
// we can also use `query_file_as!()` similarly to `query_as!()` to map our database models
let posts_with_authors = query_file_as!(PostWithAuthorQuery, "queries/list_all_posts.sql")
.fetch_all(&pool)
.await?;
for post_with_author in posts_with_authors {
println!("{post_with_author}");
}
Ok(())
}