From f77b6e558323fe8f58c9bac2b49d2074d6f8965e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Wed, 7 Sep 2022 23:53:53 +0200 Subject: [PATCH] split error code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/error.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 33 +++++---------------------------- 2 files changed, 50 insertions(+), 28 deletions(-) create mode 100644 src/error.rs diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..9aaf764 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,45 @@ +// Copyright (C) 2022-2099 The crate authors. +// +// This program is free software: you can redistribute it and/or modify it +// under the terms of the GNU Affero General Public License as published by the +// Free Software Foundation, either version 3 of the License, or (at your +// option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License +// for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +use std::error::Error as StdError; +use std::fmt; + +use tokio_xmpp::Error as TokioXMPPError; +use xmpp_parsers::Jid; + +#[derive(Debug)] +pub(crate) enum Error { + MismatchJids(Jid), + NickAlreadyAssigned(String), + XMPPError(TokioXMPPError), +} + +impl StdError for Error {} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Error::MismatchJids(err) => write!(f, "Mismatch Jids: {}", err), + Error::NickAlreadyAssigned(err) => write!(f, "Nickname already assigned: {}", err), + Error::XMPPError(err) => write!(f, "XMPP error: {}", err), + } + } +} + +impl From for Error { + fn from(err: TokioXMPPError) -> Error { + Error::XMPPError(err) + } +} diff --git a/src/main.rs b/src/main.rs index 698fe58..a7bf5a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,11 +15,13 @@ #![feature(once_cell)] +mod error; + +use crate::error::Error; + use std::collections::HashMap; use std::convert::TryFrom; use std::env::args; -use std::error::Error as StdError; -use std::fmt; use std::iter::IntoIterator; use std::ops::ControlFlow; use std::process::exit; @@ -28,7 +30,7 @@ use std::sync::{LazyLock, Mutex}; use env_logger; use futures::stream::StreamExt; use log::{debug, error, info}; -use tokio_xmpp::{Component, Error as TokioXMPPError}; +use tokio_xmpp::Component; use xmpp_parsers::{ disco::{DiscoInfoQuery, DiscoInfoResult, Feature, Identity}, iq::{Iq, IqType}, @@ -43,31 +45,6 @@ use xmpp_parsers::{ BareJid, Element, FullJid, Jid, }; -#[derive(Debug)] -enum Error { - MismatchJids(Jid), - NickAlreadyAssigned(String), - XMPPError(TokioXMPPError), -} - -impl StdError for Error {} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Error::MismatchJids(err) => write!(f, "Mismatch Jids: {}", err), - Error::NickAlreadyAssigned(err) => write!(f, "Nickname already assigned: {}", err), - Error::XMPPError(err) => write!(f, "XMPP error: {}", err), - } - } -} - -impl From for Error { - fn from(err: TokioXMPPError) -> Error { - Error::XMPPError(err) - } -} - async fn send_stanza>(component: &mut Component, el: E) -> Result<(), Error> { let el: Element = el.into(); debug!("SEND: {}", String::from(&el));