From bfc5a6f90da5f6bdb597b655cdfb4e4c285e7b47 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Tue, 14 Nov 2023 11:02:58 -0600 Subject: [PATCH] Sync everything before trying sqlx --- .cargo/config.toml | 2 ++ Cargo.lock | 60 ++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 6 +++++ flake.nix | 1 + src/app.rs | 2 +- src/error.rs | 15 ++++++----- src/instrumentation.rs | 5 ++++ src/views.rs | 25 ++++++++++++++---- 8 files changed, 103 insertions(+), 13 deletions(-) create mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..e724ea0 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[env] +RUST_BACKTRACE = "1" diff --git a/Cargo.lock b/Cargo.lock index 6c0e5a0..4b57ed3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -266,6 +266,33 @@ dependencies = [ "inout", ] +[[package]] +name = "color-eyre" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a667583cca8c4f8436db8de46ea8233c42a7d9ae424a82d338f2e4675229204" +dependencies = [ + "backtrace", + "color-spantrace", + "eyre", + "indenter", + "once_cell", + "owo-colors", + "tracing-error", +] + +[[package]] +name = "color-spantrace" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ba75b3d9449ecdccb27ecbc479fdc0b87fa2dd43d2f8298f9bf0e59aacc8dce" +dependencies = [ + "once_cell", + "owo-colors", + "tracing-core", + "tracing-error", +] + [[package]] name = "cookie" version = "0.18.0" @@ -444,6 +471,16 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "eyre" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c2b6b5a29c02cdc822728b7d7b8ae1bab3e3b05d44522770ddd49722eeac7eb" +dependencies = [ + "indenter", + "once_cell", +] + [[package]] name = "filetime" version = "0.2.22" @@ -670,6 +707,12 @@ dependencies = [ "want", ] +[[package]] +name = "indenter" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" + [[package]] name = "indexmap" version = "2.1.0" @@ -783,6 +826,7 @@ dependencies = [ "axum-macros", "axum_csrf", "base64", + "color-eyre", "cookie", "deadpool", "deadpool-diesel", @@ -966,6 +1010,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" +[[package]] +name = "owo-colors" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" + [[package]] name = "parking_lot" version = "0.12.1" @@ -1633,6 +1683,16 @@ dependencies = [ "valuable", ] +[[package]] +name = "tracing-error" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e" +dependencies = [ + "tracing", + "tracing-subscriber", +] + [[package]] name = "tracing-log" version = "0.1.4" diff --git a/Cargo.toml b/Cargo.toml index d073c6d..bdaf6bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,4 +32,10 @@ tower-livereload = "0.8.2" argon2 = { version = "0.5.2", features = ["std"] } thiserror = "1.0.50" axum-macros = "0.3.8" +color-eyre = "0.6.2" +# color-eyre +# irust +# bacon +# sqlx (sea orm?) +# poem-openapi? diff --git a/flake.nix b/flake.nix index 400ce22..96104b5 100644 --- a/flake.nix +++ b/flake.nix @@ -50,6 +50,7 @@ rustfmt rustPackages.clippy + rustPackages.bacon rust-analyzer nodePackages_latest.vscode-langservers-extracted diff --git a/src/app.rs b/src/app.rs index f3e0a1f..1aec342 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,7 +1,7 @@ use crate::{instrumentation, router, server::listen}; pub async fn run() -> Result<(), anyhow::Error> { - instrumentation::setup_trace_logger(); + instrumentation::init(); listen(router::new().await?).await; Ok(()) } diff --git a/src/error.rs b/src/error.rs index fb3678e..6b7514a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,9 +1,13 @@ +use std::fmt::Display; + use axum::{ http::StatusCode, response::{IntoResponse, Response}, }; +use thiserror::Error; -pub struct AppError(anyhow::Error); +#[derive(Error, Debug)] +pub struct AppError(#[from] anyhow::Error); impl IntoResponse for AppError { fn into_response(self) -> Response { @@ -15,11 +19,8 @@ impl IntoResponse for AppError { } } -impl From for AppError -where - E: Into, -{ - fn from(err: E) -> Self { - Self(err.into()) +impl Display for AppError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_fmt(format!("app error")) } } diff --git a/src/instrumentation.rs b/src/instrumentation.rs index 25ed7d0..cc3f7da 100644 --- a/src/instrumentation.rs +++ b/src/instrumentation.rs @@ -1,6 +1,11 @@ use tracing::{instrument, trace}; use tracing_subscriber::{filter::LevelFilter, EnvFilter}; +pub fn init() { + color_eyre::install(); + setup_trace_logger(); +} + #[instrument] pub fn setup_trace_logger() { let filter = EnvFilter::builder() diff --git a/src/views.rs b/src/views.rs index a6a190c..5937540 100644 --- a/src/views.rs +++ b/src/views.rs @@ -1,11 +1,15 @@ +use std::{error::Error, sync::Arc}; + use axum::{ extract::State, response::{Html, IntoResponse}, }; use axum_csrf::CsrfToken; use axum_macros::debug_handler; +use deadpool_diesel::InteractError; use diesel::{QueryDsl, RunQueryDsl, SelectableHelper}; use maud::html; +use thiserror::Error; use tracing::instrument; use crate::{ @@ -98,21 +102,32 @@ pub async fn login(csrf: CsrfToken) -> impl IntoResponse { .into_response() } +#[derive(Error, Debug)] +enum AllUsersError { + #[error("other application error")] + App(#[from] AppError), + #[error("failed to retrieve users")] + DB(#[from] InteractError), +} + #[debug_handler] pub async fn all_users( State(state): State, -) -> Result { +) -> Result, AllUsersError> { use crate::schema::users::dsl::*; let conn = state.database.pool.get().await?; - let cc = |c| { + let cc = |c| -> Vec { users .select(User::as_select()) .load(c) .expect("error loading users") }; - let all_users = conn.interact(cc).await?; + let all_users: Vec = match conn.interact(cc).await { + Ok(u) => u, + Err(e) => return Err(("failed to retrieve users")), + }; // @if let Some(name) = u.name { // name @@ -125,9 +140,9 @@ pub async fn all_users( main class="prose" { h1 { "Users" } ul { - @for u in &all_users { + @for u in all_users { li { - (u.username) + (u) "(" ")" }