aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/db/mod.rs21
-rw-r--r--src/db/utils.rs12
-rw-r--r--src/main.rs11
-rw-r--r--src/templates.rs18
4 files changed, 36 insertions, 26 deletions
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);