diff options
author | Joris | 2025-02-06 21:15:32 +0100 |
---|---|---|
committer | Joris | 2025-02-06 21:15:32 +0100 |
commit | fedb4e7c7ebf21619f89c29d011e288363a978e9 (patch) | |
tree | a1caf36b68fc672bf8bc0740ee05dbb6770d02a6 | |
parent | 2956aa5f5324e4183dbc87f81fbc71fd0f43dbf9 (diff) |
Use anyhow Error
-rw-r--r-- | Cargo.lock | 7 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/db/mod.rs | 21 | ||||
-rw-r--r-- | src/db/utils.rs | 12 | ||||
-rw-r--r-- | src/main.rs | 11 | ||||
-rw-r--r-- | src/templates.rs | 18 |
6 files changed, 44 insertions, 26 deletions
@@ -104,6 +104,12 @@ dependencies = [ ] [[package]] +name = "anyhow" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34ac096ce696dc2fcabef30516bb13c0a68a11d30131d3df6f04711467681b04" + +[[package]] name = "atomic-waker" version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -178,6 +184,7 @@ dependencies = [ name = "budget" version = "0.1.0" dependencies = [ + "anyhow", "bcrypt", "chrono", "env_logger", @@ -5,6 +5,7 @@ authors = ["Joris <joris@guyonvarch.me>"] edition = "2021" [dependencies] +anyhow = "1.0" bcrypt = "0.17" chrono = "0.4" env_logger = "0.11" diff --git a/src/db/mod.rs b/src/db/mod.rs index f5facd1..1282f0c 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,6 +1,25 @@ +use anyhow::{Error, Result}; +use tokio_rusqlite::Connection; + pub mod categories; pub mod incomes; pub mod jobs; pub mod payments; pub mod users; -pub mod utils; +mod utils; + +pub async fn init(path: &str) -> Result<Connection> { + let connection = Connection::open(path).await.map_err(|err| { + Error::msg(format!("Error opening connection: {err}")) + })?; + + support_foreign_keys(&connection).await?; + + Ok(connection) +} + +async fn support_foreign_keys(conn: &Connection) -> Result<()> { + Ok(conn + .call(move |conn| Ok(conn.pragma_update(None, "foreign_keys", "ON"))) + .await??) +} diff --git a/src/db/utils.rs b/src/db/utils.rs index bd4d867..2ff0f13 100644 --- a/src/db/utils.rs +++ b/src/db/utils.rs @@ -1,15 +1,5 @@ use crate::model::report::Report; -use tokio_rusqlite::{Connection, Row}; - -pub async fn support_foreign_keys(conn: &Connection) { - let res = conn - .call(move |conn| Ok(conn.pragma_update(None, "foreign_keys", "ON"))) - .await; - - if let Err(err) = res { - log::error!("Error supporting foreign keys: {err:?}"); - } -} +use tokio_rusqlite::Row; pub fn format_key_for_search(value: &str) -> String { // Lower doesn’t work on accentuated letters, hence the need to remove manually accents for diff --git a/src/main.rs b/src/main.rs index 18713bb..30832d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,8 @@ +use anyhow::Result; use hyper::server::conn::http1; use hyper::service::service_fn; use hyper_util::rt::TokioIo; use tokio::net::TcpListener; -use tokio_rusqlite::Connection; mod assets; mod controller; @@ -21,18 +21,13 @@ mod validation; use model::config; #[tokio::main] -async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { +async fn main() -> Result<()> { env_logger::init(); let config = config::from_env() .unwrap_or_else(|err| panic!("Error reading config: {err}")); - let db_conn = Connection::open(config.db_path.clone()) - .await - .unwrap_or_else(|_| { - panic!("Error while openning DB: {}", config.db_path) - }); - db::utils::support_foreign_keys(&db_conn).await; + let db_conn = db::init(&config.db_path).await?; let assets = assets::get(); diff --git a/src/templates.rs b/src/templates.rs index ebe8831..f6f4e62 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -1,3 +1,4 @@ +use anyhow::{Error, Result}; use std::fs; use crate::queries; @@ -11,21 +12,26 @@ pub enum Header { Statistics, } -pub fn get() -> Result<minijinja::Environment<'static>, String> { +pub fn get() -> Result<minijinja::Environment<'static>> { let mut env = minijinja::Environment::new(); for path in read_files_recursive("templates") { let path = path .to_str() - .ok_or("Error getting string of path: {path:?}")? + .ok_or(Error::msg("Error getting string of path: {path:?}"))? .to_string(); - let content = fs::read_to_string(&path) - .map_err(|_| "Error reading template {path}")?; + let content = fs::read_to_string(&path).map_err(|err| { + Error::msg(format!("Error reading template {path}: {err}")) + })?; let path_without_prefix = path .strip_prefix("templates/") - .ok_or("Error removing prefix from template path")? + .ok_or(Error::msg("Error removing prefix from template path"))? .to_string(); env.add_template_owned(path_without_prefix, content) - .map_err(|_| "Error adding template {path} to environment")?; + .map_err(|err| { + Error::msg(format!( + "Error adding template {path} to environment: {err}" + )) + })?; } env.add_function("payments_params", payments_params); |