diff options
author | Daniel Schadt <kingdread@gmx.de> | 2021-03-06 12:56:09 +0100 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2021-03-06 12:56:09 +0100 |
commit | 15e573150547f956956b7c58c752e31184253fbd (patch) | |
tree | c986211e9a9b8758c85f0461b208679c28a1181b | |
parent | 9079d9ec37bfd4293347539155f1164a979379be (diff) | |
download | ezau-15e573150547f956956b7c58c752e31184253fbd.tar.gz ezau-15e573150547f956956b7c58c752e31184253fbd.tar.bz2 ezau-15e573150547f956956b7c58c752e31184253fbd.zip |
put matrix & discord functionality behind gates
serenity and matrix-sdk pull in quite a lot of dependencies, which for a
minimal use of ezau might not be wanted. Therefore, we now disable those
parts by default, and the user has to opt-in into building ezau with
Discord or Matrix functionality.
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | Cargo.toml | 12 | ||||
-rw-r--r-- | src/discord.rs | 2 | ||||
-rw-r--r-- | src/logbag.rs | 5 | ||||
-rw-r--r-- | src/main.rs | 53 |
5 files changed, 67 insertions, 7 deletions
@@ -1475,6 +1475,8 @@ dependencies = [ [[package]] name = "olm-sys" version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "108c8902295791150d37f0d7b200de1fdd759f34bf479dfdc63c19de8c84c584" dependencies = [ "cmake", ] @@ -7,6 +7,10 @@ license = "MIT" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +im-matrix = ["matrix-sdk", "tokio"] +im-discord = ["serenity", "tokio"] + [dependencies] clap = "3.0.0-beta.1" log = "0.4.8" @@ -14,15 +18,16 @@ pretty_env_logger = "0.4.0" anyhow = "1.0.31" evtclib = "0.5.0" reqwest = { version = "0.10.6", features = ["json", "blocking"] } -serde = "1.0.111" +serde = { version = "1.0.111", features = ["derive"] } chrono = "0.4.11" notify = "4.0.15" regex = "1.3.9" zip = "0.5.5" toml = "0.5.6" -tokio = { version = "0.2", features = ["rt-core"] } -matrix-sdk = "0.2.0" itertools = "0.10.0" +# Optional features for IM integration +tokio = { version = "0.2", features = ["rt-core"], optional = true } +matrix-sdk = { version = "0.2.0", optional = true } [dependencies.serenity] default-features = false @@ -37,3 +42,4 @@ features = [ "rustls_backend", ] version = "0.9" +optional = true diff --git a/src/discord.rs b/src/discord.rs index ea0ea36..c98a6c7 100644 --- a/src/discord.rs +++ b/src/discord.rs @@ -11,7 +11,7 @@ use tokio::runtime::Runtime; use log::info; use super::categories::Categorizable; -use super::logbag::{LogBag, state_emoji}; +use super::logbag::{state_emoji, LogBag}; const MAX_HOURS: i64 = 5; const MAX_MESSAGE_LENGTH: usize = 2000; diff --git a/src/logbag.rs b/src/logbag.rs index f94b80a..362bee5 100644 --- a/src/logbag.rs +++ b/src/logbag.rs @@ -14,15 +14,16 @@ pub struct LogBag { data: Vec<(String, Vec<String>)>, } +// Conditional compilation makes it hard to really use all the code, so we just allow dead code +// here locally. +#[allow(dead_code)] impl LogBag { /// Construct a new, empty [`LogBag`]. - #[cfg(test)] pub fn new() -> Self { LogBag { data: Vec::new() } } /// Return an iterator over all available categories. - #[cfg(test)] pub fn categories(&self) -> impl Iterator<Item = &str> { self.data.iter().map(|x| &x.0 as &str) } diff --git a/src/main.rs b/src/main.rs index ab40d86..55bd637 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use std::{ time::Duration, }; -use anyhow::{anyhow, Context, Result}; +use anyhow::{anyhow, bail, Context, Result}; use clap::Clap; use evtclib::{Compression, Encounter, Log}; use log::{debug, error, info, warn}; @@ -20,8 +20,11 @@ mod categories; use categories::Categorizable; mod config; use config::Config; +#[cfg(feature = "im-discord")] mod discord; +#[cfg(any(feature = "im-discord", feature = "im-matrix"))] mod logbag; +#[cfg(feature = "im-matrix")] mod matrix; const DPS_REPORT_API: &str = "https://dps.report/uploadContent"; @@ -75,6 +78,8 @@ fn main() { fn inner_main(opts: &Opts) -> Result<()> { let config = config::load(&opts.config).context("Could not load configuration")?; + sanity_check(&config)?; + match &opts.subcmd { SubCommand::Watch(w) => watch(w, &config)?, SubCommand::Upload(u) => { @@ -244,3 +249,49 @@ fn upload_log(file: &Path) -> Result<String> { .json()?; Ok(resp.permalink) } + +fn sanity_check(config: &Config) -> Result<()> { + if config.discord.is_some() && !cfg!(feature = "im-discord") { + bail!( + "Discord is configured but ezau was built without Discord support. \ + Please enable the `im-discord` feature or adjust your configuration \ + to continue." + ); + } + if config.matrix.is_some() && !cfg!(feature = "im-matrix") { + bail!( + "Matrix is configured but ezau was built without Matrix support. \ + Please enable the `im-matrix` feature or adjust your configuration \ + to continue." + ); + } + Ok(()) +} + +// Dummy modules for when the features are disabled +#[cfg(not(feature = "im-discord"))] +mod discord { + use super::{Log, Result}; + use anyhow::bail; + + /// Stub, enable the `im-discord` feature to use this function. + pub fn post_link(_: &str, _: u64, _: &Log, _: &str) -> Result<()> { + bail!("Discord feature is disabled in this build!") + } +} + +#[cfg(not(feature = "im-matrix"))] +mod matrix { + use super::{Log, Result}; + use anyhow::bail; + pub struct MatrixUser; + impl From<super::config::Matrix> for MatrixUser { + fn from(_: super::config::Matrix) -> MatrixUser { + MatrixUser + } + } + /// Stub, enable the `im-matrix` feature to use this function. + pub fn post_link(_: MatrixUser, _: &str, _: &Log, _: &str) -> Result<()> { + bail!("Matrix feature is disabled in this build!") + } +} |