split error code

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2022-09-07 23:53:53 +02:00
parent 05033ee6f7
commit f77b6e5583
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2
2 changed files with 50 additions and 28 deletions

45
src/error.rs Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
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<TokioXMPPError> for Error {
fn from(err: TokioXMPPError) -> Error {
Error::XMPPError(err)
}
}

View file

@ -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<TokioXMPPError> for Error {
fn from(err: TokioXMPPError) -> Error {
Error::XMPPError(err)
}
}
async fn send_stanza<E: Into<Element>>(component: &mut Component, el: E) -> Result<(), Error> {
let el: Element = el.into();
debug!("SEND: {}", String::from(&el));