From fc85f34301a99e404433650f3f4cd4d4ed24ec3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Tue, 6 Apr 2021 20:40:44 +0200 Subject: remove anyhow --- mumctl/Cargo.toml | 1 - mumctl/src/main.rs | 41 +++++++++++++++++++++++++++++------------ 2 files changed, 29 insertions(+), 13 deletions(-) (limited to 'mumctl') diff --git a/mumctl/Cargo.toml b/mumctl/Cargo.toml index 0da7fa4..621b1d0 100644 --- a/mumctl/Cargo.toml +++ b/mumctl/Cargo.toml @@ -13,7 +13,6 @@ license = "MIT" [dependencies] mumlib = { version = "0.3", path = "../mumlib" } -anyhow = "1" bincode = "1" colored = "2" log = "0.4" diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 4ce8016..0314cab 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -1,15 +1,9 @@ -use anyhow::Result; use colored::Colorize; use log::*; use mumlib::command::{Command as MumCommand, CommandResponse}; use mumlib::config::{self, Config, ServerConfig}; use mumlib::state::Channel as MumChannel; -use std::{ - fmt::Formatter, - io::{self, BufRead, Read, Write}, - iter, - os::unix::net::UnixStream, -}; +use std::{fmt,io::{self, BufRead, Read, Write}, iter, os::unix::net::UnixStream}; use structopt::{clap::Shell, StructOpt}; const INDENTATION: &str = " "; @@ -153,10 +147,28 @@ enum CliError { NoServers, } +#[derive(Debug)] +struct Error(Box); // new type since otherwise we'd get an impl conflict with impl From for T below + +impl From for Error +where + E: std::error::Error + fmt::Display + 'static, +{ + fn from(e: E) -> Self { + Error(Box::new(e)) + } +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + std::fmt::Display::fmt(&self.0, f) + } +} + impl std::error::Error for CliError {} -impl std::fmt::Display for CliError { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { +impl fmt::Display for CliError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { CliError::NoUsername => { write!(f, "No username specified") @@ -168,7 +180,7 @@ impl std::fmt::Display for CliError { write!(f, "Server '{}' not found", s) } CliError::NotSet(s) => { - write!(f, "Key '{}' not set", s) //TODO + write!(f, "Key '{}' not set", s) } CliError::UseServerRename => { write!(f, "Use 'server rename' instead") @@ -186,10 +198,15 @@ impl std::fmt::Display for CliError { } } -fn main() -> Result<()> { +fn main() { log::set_logger(&LOGGER) .map(|()| log::set_max_level(LevelFilter::Info)) .unwrap(); + if let Err(e) = match_opt() { + error!("{}", e); + } +} +fn match_opt() -> Result<(), Error> { let mut config = config::read_default_cfg()?; let opt = Mum::from_args(); @@ -346,7 +363,7 @@ fn main() -> Result<()> { Ok(()) } -fn match_server_command(server_command: Server, config: &mut Config) -> Result<()> { +fn match_server_command(server_command: Server, config: &mut Config) -> Result<(), CliError> { match server_command { Server::Config { server_name, -- cgit v1.2.1