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