use anyhow/thiserror for error handling
This commit is contained in:
parent
65bad5849d
commit
3f08ec6b26
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -75,6 +75,12 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.38"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "afddf7f520a80dbf76e6f50a35bca42a2331ef227a28b3b6dc5c2e2338d114b1"
|
||||
|
||||
[[package]]
|
||||
name = "assign"
|
||||
version = "1.1.1"
|
||||
|
@ -122,10 +128,12 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
|
|||
name = "bad-news"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
"matrix-sdk",
|
||||
"serde",
|
||||
"serde_yaml",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
"tracing-subscriber",
|
||||
"url",
|
||||
|
|
|
@ -7,12 +7,14 @@ edition = "2018"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
||||
clap = "3.0.0-beta.2"
|
||||
tokio = { version = "1", features = [ "full" ] }
|
||||
tracing-subscriber = "0.2"
|
||||
url = { version = "2.2", features = [ "serde" ] }
|
||||
serde_yaml = "0.8"
|
||||
serde = "1.0"
|
||||
thiserror = "1.0"
|
||||
|
||||
[dependencies.matrix-sdk]
|
||||
git = "https://github.com/matrix-org/matrix-rust-sdk"
|
||||
|
|
39
src/main.rs
39
src/main.rs
|
@ -1,6 +1,6 @@
|
|||
use std::{
|
||||
fs::File,
|
||||
io::{BufReader, BufWriter},
|
||||
io::{self, BufReader, BufWriter},
|
||||
};
|
||||
use std::{path::PathBuf, time::Duration};
|
||||
|
||||
|
@ -23,6 +23,8 @@ use matrix_sdk::{
|
|||
|
||||
use serde::Deserialize;
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
struct AutoJoinBot {
|
||||
client: Client,
|
||||
}
|
||||
|
@ -101,20 +103,19 @@ async fn load_or_init_session(
|
|||
session_file: PathBuf,
|
||||
username: &str,
|
||||
password: &str,
|
||||
) {
|
||||
) -> anyhow::Result<()> {
|
||||
if session_file.is_file() {
|
||||
let reader = BufReader::new(File::open(session_file).unwrap());
|
||||
let reader = BufReader::new(File::open(session_file)?);
|
||||
|
||||
let session: Session = serde_yaml::from_reader(reader).unwrap();
|
||||
let session: Session = serde_yaml::from_reader(reader)?;
|
||||
|
||||
client.restore_login(session.clone()).await.unwrap();
|
||||
client.restore_login(session.clone()).await?;
|
||||
|
||||
println!("Reused session: {}, {}", session.user_id, session.device_id);
|
||||
} else {
|
||||
let response = client
|
||||
.login(username, password, None, Some("autojoin bot"))
|
||||
.await
|
||||
.unwrap();
|
||||
.await?;
|
||||
|
||||
println!("logged in as {}", username);
|
||||
|
||||
|
@ -124,9 +125,11 @@ async fn load_or_init_session(
|
|||
device_id: response.device_id,
|
||||
};
|
||||
|
||||
let writer = BufWriter::new(File::create(session_file).unwrap());
|
||||
serde_yaml::to_writer(writer, &session).unwrap();
|
||||
let writer = BufWriter::new(File::create(session_file)?);
|
||||
serde_yaml::to_writer(writer, &session)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn login_and_sync(
|
||||
|
@ -134,12 +137,12 @@ async fn login_and_sync(
|
|||
username: &str,
|
||||
password: &str,
|
||||
state_dir: PathBuf,
|
||||
) -> Result<(), matrix_sdk::Error> {
|
||||
) -> anyhow::Result<()> {
|
||||
let client_config = ClientConfig::new().store_path(state_dir.join("store"));
|
||||
|
||||
let client = Client::new_with_config(homeserver_url, client_config).unwrap();
|
||||
let client = Client::new_with_config(homeserver_url, client_config)?;
|
||||
|
||||
load_or_init_session(&client, state_dir.join("session.yaml"), username, password).await;
|
||||
load_or_init_session(&client, state_dir.join("session.yaml"), username, password).await?;
|
||||
|
||||
client
|
||||
.add_event_emitter(Box::new(AutoJoinBot::new(client.clone())))
|
||||
|
@ -150,6 +153,13 @@ async fn login_and_sync(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
enum BadNewsError {
|
||||
#[error("problem accessing configuration file")]
|
||||
ConfigFile(#[from] io::Error),
|
||||
#[error("Matrix communication error")]
|
||||
Matrix(#[from] matrix_sdk::Error),
|
||||
}
|
||||
|
||||
#[derive(Clap)]
|
||||
#[clap(version = "0.1", author = "Antoine Martin")]
|
||||
|
@ -168,14 +178,13 @@ struct Config {
|
|||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), matrix_sdk::Error> {
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let opts = Opts::parse();
|
||||
let config_file = opts.config;
|
||||
|
||||
let config: Config =
|
||||
serde_yaml::from_reader(BufReader::new(File::open(config_file).unwrap())).unwrap();
|
||||
let config: Config = serde_yaml::from_reader(BufReader::new(File::open(config_file)?))?;
|
||||
|
||||
login_and_sync(
|
||||
config.homeserver,
|
||||
|
|
Loading…
Reference in a new issue