| @@ -11,6 +11,7 @@ use std::process; |
| 11 | 11 | use clap::Parser; |
| 12 | 12 | use matrix_sdk::ruma::{OwnedUserId, UserId}; |
| 13 | 13 | use serde::{de::Error as SerdeError, de::Visitor, Deserialize, Deserializer}; |
| 14 | use tracing::Level; |
| 14 | 15 | use url::Url; |
| 15 | 16 | |
| 16 | 17 | use modalkit::tui::{ |
| @@ -108,6 +109,47 @@ pub enum ConfigError { |
| 108 | 109 | Invalid(#[from] serde_json::Error), |
| 109 | 110 | } |
| 110 | 111 | |
| 112 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 113 | pub struct LogLevel(pub Level); |
| 114 | pub struct LogLevelVisitor; |
| 115 | |
| 116 | impl From<LogLevel> for Level { |
| 117 | fn from(level: LogLevel) -> Level { |
| 118 | level.0 |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | impl<'de> Visitor<'de> for LogLevelVisitor { |
| 123 | type Value = LogLevel; |
| 124 | |
| 125 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 126 | formatter.write_str("a valid log level (e.g. \"warn\" or \"debug\")") |
| 127 | } |
| 128 | |
| 129 | fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> |
| 130 | where |
| 131 | E: SerdeError, |
| 132 | { |
| 133 | match value { |
| 134 | "info" => Ok(LogLevel(Level::INFO)), |
| 135 | "debug" => Ok(LogLevel(Level::DEBUG)), |
| 136 | "warn" => Ok(LogLevel(Level::WARN)), |
| 137 | "error" => Ok(LogLevel(Level::ERROR)), |
| 138 | "trace" => Ok(LogLevel(Level::TRACE)), |
| 139 | _ => Err(E::custom("Could not parse log level")), |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | impl<'de> Deserialize<'de> for LogLevel { |
| 145 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 146 | where |
| 147 | D: Deserializer<'de>, |
| 148 | { |
| 149 | deserializer.deserialize_str(LogLevelVisitor) |
| 150 | } |
| 151 | } |
| 152 | |
| 111 | 153 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 112 | 154 | pub struct UserColor(pub Color); |
| 113 | 155 | pub struct UserColorVisitor; |
| @@ -180,6 +222,7 @@ fn merge_users(a: Option<UserOverrides>, b: Option<UserOverrides>) -> Option<Use |
| 180 | 222 | |
| 181 | 223 | #[derive(Clone)] |
| 182 | 224 | pub struct TunableValues { |
| 225 | pub log_level: Level, |
| 183 | 226 | pub reaction_display: bool, |
| 184 | 227 | pub reaction_shortcode_display: bool, |
| 185 | 228 | pub read_receipt_send: bool, |
| @@ -193,6 +236,7 @@ pub struct TunableValues { |
| 193 | 236 | |
| 194 | 237 | #[derive(Clone, Default, Deserialize)] |
| 195 | 238 | pub struct Tunables { |
| 239 | pub log_level: Option<LogLevel>, |
| 196 | 240 | pub reaction_display: Option<bool>, |
| 197 | 241 | pub reaction_shortcode_display: Option<bool>, |
| 198 | 242 | pub read_receipt_send: Option<bool>, |
| @@ -207,6 +251,7 @@ pub struct Tunables { |
| 207 | 251 | impl Tunables { |
| 208 | 252 | fn merge(self, other: Self) -> Self { |
| 209 | 253 | Tunables { |
| 254 | log_level: self.log_level.or(other.log_level), |
| 210 | 255 | reaction_display: self.reaction_display.or(other.reaction_display), |
| 211 | 256 | reaction_shortcode_display: self |
| 212 | 257 | .reaction_shortcode_display |
| @@ -223,6 +268,7 @@ impl Tunables { |
| 223 | 268 | |
| 224 | 269 | fn values(self) -> TunableValues { |
| 225 | 270 | TunableValues { |
| 271 | log_level: self.log_level.map(Level::from).unwrap_or(Level::INFO), |
| 226 | 272 | reaction_display: self.reaction_display.unwrap_or(true), |
| 227 | 273 | reaction_shortcode_display: self.reaction_shortcode_display.unwrap_or(false), |
| 228 | 274 | read_receipt_send: self.read_receipt_send.unwrap_or(true), |