Skip to content

Commit

Permalink
Try to fix all unused warning
Browse files Browse the repository at this point in the history
  • Loading branch information
thynson committed Jun 22, 2024
1 parent 7e938f5 commit ade8ca6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/conf/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl Default for CachedFetchRequest {
}
}

#[cfg(feature = "conf")]
impl PerformRequest for CachedFetchRequest {
type Response = Properties;

Expand Down Expand Up @@ -130,6 +131,7 @@ impl FetchRequest {
}
}

#[cfg(feature = "conf")]
impl PerformRequest for FetchRequest {
type Response = FetchResponse;

Expand Down Expand Up @@ -215,6 +217,7 @@ impl NotifyRequest {
}
}

#[cfg(feature = "conf")]
impl PerformRequest for NotifyRequest {
type Response = Vec<Notification>;

Expand Down
5 changes: 5 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Crate level errors.

#[cfg(feature = "conf")]
use http::StatusCode;
#[cfg(feature = "conf")]
use reqwest::Response;
use std::str::Utf8Error;

Expand All @@ -27,6 +29,7 @@ pub enum ApolloClientError {
#[error(transparent)]
IniParse(#[from] ini::ParseError),

#[cfg(feature = "conf")]
#[error(transparent)]
ApolloResponse(#[from] ApolloResponseError),

Expand All @@ -38,6 +41,7 @@ pub enum ApolloClientError {
}

/// Apollo api response error, when http status is not success.
#[cfg(feature = "conf")]
#[derive(thiserror::Error, Debug)]
#[error(r#"error occurred when apollo response, status: {status}, body: "{body}""#)]
pub struct ApolloResponseError {
Expand All @@ -47,6 +51,7 @@ pub struct ApolloResponseError {
pub body: String,
}

#[cfg(feature = "conf")]
impl ApolloResponseError {
pub(crate) async fn from_response(response: Response) -> Result<Response, Self> {
if response.status().is_success() {
Expand Down
33 changes: 20 additions & 13 deletions src/meta.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//! Common api metadata.

#[cfg(feature = "conf")]
use crate::errors::{ApolloClientResult, ApolloResponseError};
use async_trait::async_trait;
use http::Method;
#[cfg(feature = "conf")]
use reqwest::{RequestBuilder, Response};
use std::{borrow::Cow, fmt, fmt::Display, time::Duration};
use url::Url;
use std::{fmt, fmt::Display, time::Duration};

#[allow(dead_code)]
pub(crate) const DEFAULT_CLUSTER_NAME: &str = "default";
Expand Down Expand Up @@ -57,6 +56,7 @@ impl Display for NamespaceKind {
}

/// Common api request trait.
#[cfg(feature = "conf")]
pub(crate) trait PerformRequest {
/// The returned response after request is success.
type Response: PerformResponse;
Expand All @@ -66,11 +66,13 @@ pub(crate) trait PerformRequest {

/// Request method.
fn method(&self) -> http::Method {
Method::GET
http::Method::GET
}

/// Url queries.
fn queries(&self) -> ApolloClientResult<Vec<(Cow<'_, str>, Cow<'_, str>)>> {
fn queries(
&self,
) -> ApolloClientResult<Vec<(std::borrow::Cow<'_, str>, std::borrow::Cow<'_, str>)>> {
Ok(vec![])
}

Expand Down Expand Up @@ -140,21 +142,23 @@ pub(crate) trait PerformRequest {
}

/// Common api response trait.
#[async_trait]
#[cfg(feature = "conf")]
#[async_trait::async_trait]
pub(crate) trait PerformResponse: Sized {
/// Create Self from response.
async fn from_response(response: Response) -> ApolloClientResult<Self>;
}

#[async_trait]
#[cfg(feature = "conf")]
#[async_trait::async_trait]
impl PerformResponse for () {
async fn from_response(_response: Response) -> ApolloClientResult<Self> {
Ok(())
}
}

#[cfg(feature = "conf")]
#[async_trait]
#[async_trait::async_trait]
impl PerformResponse for ini::Properties {
async fn from_response(response: Response) -> ApolloClientResult<Self> {
let content = response.text().await?;
Expand All @@ -166,8 +170,11 @@ impl PerformResponse for ini::Properties {
}

/// Create request url from base url, mainly path and queries.
#[allow(dead_code)]
pub(crate) fn handle_url(request: &impl PerformRequest, base_url: Url) -> ApolloClientResult<Url> {
#[cfg(feature = "conf")]
pub(crate) fn handle_url(
request: &impl PerformRequest,
base_url: url::Url,
) -> ApolloClientResult<url::Url> {
let mut url = base_url;
let path = &request.path();
let query = &request.queries()?;
Expand All @@ -183,15 +190,15 @@ pub(crate) fn handle_url(request: &impl PerformRequest, base_url: Url) -> Apollo
}

/// Validate response is successful or not.
#[allow(dead_code)]
#[cfg(feature = "conf")]
pub(crate) async fn validate_response(response: Response) -> ApolloClientResult<Response> {
ApolloResponseError::from_response(response)
.await
.map_err(Into::into)
}

/// Implement PerformResponse for response struct which content type is `application/json`.
#[allow(unused_macros)]
#[cfg(feature = "conf")]
macro_rules! implement_json_perform_response_for {
($t:ty) => {
#[async_trait::async_trait]
Expand Down

0 comments on commit ade8ca6

Please sign in to comment.