Compare commits
3 Commits
86c6ec26f5
...
f5edc44717
Author | SHA1 | Date | |
---|---|---|---|
f5edc44717
|
|||
73ee8114ca
|
|||
3fee20f0cf
|
@ -18,16 +18,7 @@
|
||||
*/
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::{fs, io};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[allow(dead_code)]
|
||||
pub enum Error {
|
||||
FileNotFoundError(String),
|
||||
PermissionDeniedError(),
|
||||
InvalidFormatError(serde_yaml::Error),
|
||||
IoError(io::Error),
|
||||
}
|
||||
use std::{fs, io, fmt};
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
pub struct FeedConfig {
|
||||
@ -46,19 +37,49 @@ pub struct Config {
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load(config_file: &str) -> Result<Self, Error> {
|
||||
pub fn load(config_file: &str) -> Result<Self> {
|
||||
|
||||
let serialized_config = fs::read_to_string(config_file).map_err(|e| {
|
||||
match e.kind() {
|
||||
io::ErrorKind::NotFound => Error::FileNotFoundError(config_file.into()),
|
||||
io::ErrorKind::PermissionDenied => Error::PermissionDeniedError(),
|
||||
_ => Error::IoError(e),
|
||||
_ => e.into(),
|
||||
}
|
||||
})?;
|
||||
|
||||
let config: Config = serde_yaml::from_str(&serialized_config)
|
||||
.map_err(|e| Error::InvalidFormatError(e))?;
|
||||
let config: Config = serde_yaml::from_str(&serialized_config)?;
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[allow(dead_code)] // we use Debug, so inner fields are never read
|
||||
pub enum Error {
|
||||
FileNotFoundError(String),
|
||||
PermissionDeniedError(),
|
||||
InvalidFormatError(serde_yaml::Error),
|
||||
IoError(io::Error),
|
||||
}
|
||||
|
||||
impl From<serde_yaml::Error> for Error {
|
||||
fn from(e: serde_yaml::Error) -> Self {
|
||||
Self::InvalidFormatError(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<io::Error> for Error {
|
||||
fn from(e: io::Error) -> Self {
|
||||
Self::IoError(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> std::result::Result<(), fmt::Error> {
|
||||
write!(fmt, "{self:?}")
|
||||
}
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
11
src/main.rs
11
src/main.rs
@ -51,12 +51,13 @@ async fn main() -> anyhow::Result<()> {
|
||||
});
|
||||
let config = Arc::new(config);
|
||||
|
||||
let state_db = FeedReaderStateDb::new("state.yaml").await.unwrap_or_else(|e| {
|
||||
panic!("Failed to initialize feed reader state db: {e:?}")
|
||||
});
|
||||
|
||||
// This message passing channel is used for sending messages to the matrix module,
|
||||
// it holds a tuple with an HTML message and a list of rooms to post to
|
||||
let (bcast_tx, bcast_rx) = broadcast::channel(1024);
|
||||
|
||||
let state_db = FeedReaderStateDb::new("state.yaml").await
|
||||
.expect("Failed to initialize feed reader state db");
|
||||
let (bcast_tx, bcast_rx) = broadcast::channel::<(String, Vec<String>)>(1024);
|
||||
|
||||
let handles: Vec<JoinHandle<_>> = config.feeds.clone().into_iter().map(|feed_config| {
|
||||
let state_db = Arc::clone(&state_db);
|
||||
@ -130,7 +131,7 @@ async fn main() -> anyhow::Result<()> {
|
||||
})
|
||||
}).collect();
|
||||
|
||||
login_and_sync::<(String, Vec<String>)>(
|
||||
login_and_sync(
|
||||
config.homeserver_uri.clone().into(),
|
||||
&config.username,
|
||||
&config.password,
|
||||
|
@ -85,7 +85,7 @@ async fn send_to_room(
|
||||
};
|
||||
}
|
||||
|
||||
pub async fn login_and_sync<T: Clone>(
|
||||
pub async fn login_and_sync(
|
||||
homeserver_url: String,
|
||||
username: &str,
|
||||
password: &str,
|
||||
|
Reference in New Issue
Block a user