Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature anyhow removal #459

Merged
merged 4 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ name = "mostrod"
path = "src/main.rs"

[dependencies]
anyhow = "1.0.89"
chrono = "0.4.35"
easy-hasher = "2.2.1"
lightning-invoice = { version = "0.33.1", features = ["std"] }
Expand All @@ -38,7 +37,7 @@ uuid = { version = "1.8.0", features = [
"serde",
] }
reqwest = { version = "0.12.1", features = ["json"] }
mostro-core = { version = "0.6.28", features = ["sqlx"] }
mostro-core = { version = "0.6.30", features = ["sqlx"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
config = "0.15.8"
Expand Down
3 changes: 1 addition & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ use crate::util::enqueue_cant_do_msg;
use crate::Settings;

// External dependencies
use anyhow::Result;
use mostro_core::error::CantDoReason;
use mostro_core::error::MostroError;
use mostro_core::error::ServiceError;
Expand Down Expand Up @@ -354,7 +353,7 @@ pub async fn run(
{
match e.downcast::<MostroError>() {
Ok(err) => {
manage_errors(err, message, event, &action).await;
manage_errors(*err, message, event, &action).await;
}
Err(e) => {
tracing::error!("Unexpected error type: {}", e);
Expand Down
2 changes: 0 additions & 2 deletions src/app/add_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use crate::util::{
enqueue_order_msg, get_order, show_hold_invoice, update_order_event, validate_invoice,
};

use anyhow::Result;

use mostro_core::error::MostroError::{self, *};
use mostro_core::error::{CantDoReason, ServiceError};
use mostro_core::message::{Action, Message, Payload};
Expand Down
1 change: 0 additions & 1 deletion src/app/admin_add_solver.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::db::add_new_user;
use crate::util::send_dm;

use anyhow::Result;
use mostro_core::error::{
MostroError::{self, *},
ServiceError,
Expand Down
1 change: 0 additions & 1 deletion src/app/rate_user.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::util::{enqueue_order_msg, get_order, update_user_rating_event};

use crate::db::{is_user_present, update_user_rating};
use anyhow::Result;
use mostro_core::error::MostroError::{self, *};
use mostro_core::error::{CantDoReason, ServiceError};
use mostro_core::message::{Action, Message, Payload};
Expand Down
1 change: 0 additions & 1 deletion src/app/take_buy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::util::{
};

use crate::db::{seller_has_pending_order, update_user_trade_index};
use anyhow::Result;
use mostro_core::error::MostroError::{self, *};
use mostro_core::error::{CantDoReason, ServiceError};
use mostro_core::message::Message;
Expand Down
1 change: 0 additions & 1 deletion src/app/take_sell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use mostro_core::error::MostroError::{self, *};
use mostro_core::error::{CantDoReason, ServiceError};

use crate::db::{buyer_has_pending_order, update_user_trade_index};
use anyhow::Result;
use mostro_core::message::Message;
use mostro_core::order::{Order, Status};
use nostr::nips::nip59::UnwrappedGift;
Expand Down
29 changes: 21 additions & 8 deletions src/bitcoin_price.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Result;
use mostro_core::error::MostroError::{self, *};
use mostro_core::error::ServiceError;
use once_cell::sync::Lazy;
use serde::Deserialize;
use std::collections::HashMap;
Expand All @@ -19,21 +20,33 @@ static BITCOIN_PRICES: Lazy<RwLock<HashMap<String, f64>>> =
pub struct BitcoinPriceManager;

impl BitcoinPriceManager {
pub async fn update_prices() -> Result<()> {
let response = reqwest::get(YADIO_API_URL).await?;
let yadio_response: YadioResponse = response.json().await?;
pub async fn update_prices() -> Result<(), MostroError> {
let response = reqwest::get(YADIO_API_URL)
.await
.map_err(|_| MostroInternalErr(ServiceError::NoAPIResponse))?;
let yadio_response: YadioResponse = response
.json()
.await
.map_err(|_| MostroInternalErr(ServiceError::MessageSerializationError))?;
info!(
"Bitcoin prices updated. Got BTC price in {} fiat currencies",
yadio_response.btc.keys().collect::<Vec<&String>>().len()
);

let mut prices_write = BITCOIN_PRICES.write().unwrap();
let mut prices_write = BITCOIN_PRICES
.write()
.map_err(|e| MostroInternalErr(ServiceError::IOError(e.to_string())))?;
*prices_write = yadio_response.btc;
Ok(())
}

pub fn get_price(currency: &str) -> Option<f64> {
let prices_read = BITCOIN_PRICES.read().unwrap();
prices_read.get(currency).cloned()
pub fn get_price(currency: &str) -> Result<f64, MostroError> {
let prices_read: std::sync::RwLockReadGuard<'_, HashMap<String, f64>> = BITCOIN_PRICES
.read()
.map_err(|e| MostroInternalErr(ServiceError::IOError(e.to_string())))?;
prices_read
.get(currency)
.cloned()
.ok_or(MostroInternalErr(ServiceError::NoAPIResponse))
}
}
9 changes: 4 additions & 5 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
pub mod settings;

use crate::cli::settings::init_default_dir;

use anyhow::Result;
use clap::Parser;
use mostro_core::error::MostroError;
use std::path::PathBuf;

#[derive(Parser)]
Expand All @@ -30,12 +29,12 @@ pub struct Cli {
dirsettings: Option<String>,
}

pub fn settings_init() -> Result<PathBuf> {
pub fn settings_init() -> Result<PathBuf, MostroError> {
let cli = Cli::parse();

if let Some(path) = cli.dirsettings.as_deref() {
init_default_dir(Some(path.to_string()))
Ok(init_default_dir(Some(path.to_string()))?)
} else {
init_default_dir(None)
Ok(init_default_dir(None)?)
}
}
35 changes: 21 additions & 14 deletions src/cli/settings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::MOSTRO_CONFIG;
use anyhow::{Error, Result};
use config::{Config, ConfigError, Environment, File};
use mostro_core::error::MostroError::{self, *};
use mostro_core::error::ServiceError;
use serde::Deserialize;
use std::ffi::OsString;
use std::io::Write;
Expand Down Expand Up @@ -46,9 +47,9 @@ pub struct Database {
}

impl TryFrom<Settings> for Database {
type Error = Error;
type Error = MostroError;

fn try_from(_: Settings) -> Result<Self, Error> {
fn try_from(_: Settings) -> Result<Self, MostroError> {
Ok(MOSTRO_CONFIG.get().unwrap().database.clone())
}
}
Expand All @@ -66,9 +67,9 @@ pub struct Lightning {
}

impl TryFrom<Settings> for Lightning {
type Error = Error;
type Error = MostroError;

fn try_from(_: Settings) -> Result<Self, Error> {
fn try_from(_: Settings) -> Result<Self, MostroError> {
Ok(MOSTRO_CONFIG.get().unwrap().lightning.clone())
}
}
Expand All @@ -80,9 +81,9 @@ pub struct Nostr {
}

impl TryFrom<Settings> for Nostr {
type Error = Error;
type Error = MostroError;

fn try_from(_: Settings) -> Result<Self, Error> {
fn try_from(_: Settings) -> Result<Self, MostroError> {
Ok(MOSTRO_CONFIG.get().unwrap().nostr.clone())
}
}
Expand All @@ -103,9 +104,9 @@ pub struct Mostro {
}

impl TryFrom<Settings> for Mostro {
type Error = Error;
type Error = MostroError;

fn try_from(_: Settings) -> Result<Self, Error> {
fn try_from(_: Settings) -> Result<Self, MostroError> {
Ok(MOSTRO_CONFIG.get().unwrap().mostro.clone())
}
}
Expand Down Expand Up @@ -169,7 +170,7 @@ impl Settings {
}
}

pub fn init_default_dir(config_path: Option<String>) -> Result<PathBuf> {
pub fn init_default_dir(config_path: Option<String>) -> Result<PathBuf, MostroError> {
// , final_path : &mut PathBuf) -> Result<()> {
// Dir prefix
let home_dir: OsString;
Expand All @@ -183,7 +184,8 @@ pub fn init_default_dir(config_path: Option<String>) -> Result<PathBuf> {
settings_dir_default.push(home_dir);
} else {
// Get $HOME from env
let tmp = std::env::var("HOME")?;
let tmp = std::env::var("HOME")
.map_err(|e| MostroInternalErr(ServiceError::EnvVarError(e.to_string())))?;
// Os String
home_dir = tmp.into();
// Create default path with default .mostro value
Expand All @@ -199,10 +201,15 @@ pub fn init_default_dir(config_path: Option<String>) -> Result<PathBuf> {
if std::fs::create_dir(settings_dir_default.clone()).is_ok() {
tracing::info!("Created mostro default directory!");
let mut config_file =
std::fs::File::create_new(settings_dir_default.join("settings.toml"))?;
std::fs::File::create_new(settings_dir_default.join("settings.toml"))
.map_err(|e| MostroInternalErr(ServiceError::IOError(e.to_string())))?;
let buf = include_bytes!("../../settings.tpl.toml");
config_file.write_all(buf)?;
config_file.flush()?;
config_file
.write_all(buf)
.map_err(|e| MostroInternalErr(ServiceError::IOError(e.to_string())))?;
config_file
.flush()
.map_err(|e| MostroInternalErr(ServiceError::IOError(e.to_string())))?;
}
tracing::info!(
"Created settings file based on template and copied to {} directory",
Expand Down
Loading