From e0b102d932b98f6418b363d40d38b56a7b364907 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Mon, 7 Aug 2023 11:19:39 +0200 Subject: [PATCH] client: prepare to get rid of Error trait First rename it so it's clear what "Error" refers to in the following patches. Signed-off-by: Wolfgang Bumiller --- proxmox-client/src/client.rs | 18 +++++++++--------- proxmox-client/src/environment.rs | 4 ++-- proxmox-client/src/error.rs | 4 ++-- proxmox-client/src/lib.rs | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/proxmox-client/src/client.rs b/proxmox-client/src/client.rs index 6b606a1c..a8135e22 100644 --- a/proxmox-client/src/client.rs +++ b/proxmox-client/src/client.rs @@ -13,13 +13,13 @@ use serde_json::Value; use proxmox_login::{Login, TicketResult}; use crate::auth::AuthenticationKind; -use crate::{Authentication, Environment, Error, Token}; +use crate::{Authentication, Environment, ErrorTrait, Token}; /// HTTP client backend trait. /// /// An async [`Client`] requires some kind of async HTTP client implementation. pub trait HttpClient: Send + Sync { - type Error: Error; + type Error: ErrorTrait; type ResponseFuture: Future>, Self::Error>>; fn request(&self, request: Request>) -> Self::ResponseFuture; @@ -58,7 +58,7 @@ where } } -fn to_request(request: proxmox_login::Request) -> Result>, E> { +fn to_request(request: proxmox_login::Request) -> Result>, E> { http::Request::builder() .method(http::Method::POST) .uri(request.url) @@ -309,7 +309,7 @@ where .set_auth_headers(Request::get(self.build_uri(uri)?)) .await? .body(Vec::new()) - .map_err(Error::internal)?; + .map_err(E::Error::internal)?; Self::handle_response(self.client.request(request).await?) } @@ -364,7 +364,7 @@ where .set_auth_headers(Request::delete(self.build_uri(uri)?)) .await? .body(Vec::new()) - .map_err(Error::internal)?; + .map_err(E::Error::internal)?; Self::handle_response(self.client.request(request).await?) } @@ -437,7 +437,7 @@ where let request = auth .set_auth_headers(request) .body(body.clone()) - .map_err(Error::internal)?; + .map_err(E::Error::internal)?; Ok(self.client.request(request).await?) } @@ -459,12 +459,12 @@ where // Ok(value) => // if value["error"] let (response, body) = response.into_parts(); - let body = String::from_utf8(body).map_err(Error::bad_api)?; + let body = String::from_utf8(body).map_err(E::Error::bad_api)?; return Err(E::Error::api_error(response.status, body)); } let data: RawApiResponse = - serde_json::from_slice(&response.into_body()).map_err(Error::bad_api)?; + serde_json::from_slice(&response.into_body()).map_err(E::Error::bad_api)?; data.check() } @@ -528,7 +528,7 @@ struct RawApiResponse { } impl RawApiResponse { - pub fn check(mut self) -> Result, E> { + pub fn check(mut self) -> Result, E> { if !self.success.unwrap_or(false) { let status = http::StatusCode::from_u16(self.status.unwrap_or(400)) .unwrap_or(http::StatusCode::BAD_REQUEST); diff --git a/proxmox-client/src/environment.rs b/proxmox-client/src/environment.rs index 9aefa93b..d87c282e 100644 --- a/proxmox-client/src/environment.rs +++ b/proxmox-client/src/environment.rs @@ -6,12 +6,12 @@ use http::Uri; use proxmox_login::tfa::TfaChallenge; -use crate::Error; +use crate::ErrorTrait; /// Provide input from the environment for storing/loading tickets or tokens and querying the user /// for passwords or 2nd factors. pub trait Environment: Send + Sync { - type Error: Error; + type Error: ErrorTrait; /// Store a ticket belonging to a user of an API. /// diff --git a/proxmox-client/src/error.rs b/proxmox-client/src/error.rs index 83548009..a4dd5043 100644 --- a/proxmox-client/src/error.rs +++ b/proxmox-client/src/error.rs @@ -2,7 +2,7 @@ use std::any::Any; use std::fmt::{self, Display}; /// For error types provided by the user of this crate. -pub trait Error: Sized + Display + fmt::Debug + Any + Send + Sync + 'static { +pub trait ErrorTrait: Sized + Display + fmt::Debug + Any + Send + Sync + 'static { /// An arbitrary error message. fn custom(msg: T) -> Self; @@ -54,7 +54,7 @@ pub trait Error: Sized + Display + fmt::Debug + Any + Send + Sync + 'static { } } -impl Error for anyhow::Error { +impl ErrorTrait for anyhow::Error { fn custom(msg: T) -> Self { anyhow::format_err!("{msg}") } diff --git a/proxmox-client/src/lib.rs b/proxmox-client/src/lib.rs index 929b4a1b..e046d1c5 100644 --- a/proxmox-client/src/lib.rs +++ b/proxmox-client/src/lib.rs @@ -2,7 +2,7 @@ mod environment; mod error; pub use environment::Environment; -pub use error::Error; +pub use error::ErrorTrait; pub use proxmox_login::tfa::TfaChallenge; pub use proxmox_login::{Authentication, Ticket};