Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proc macro to derive ApiResult #231

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

members = [
"cloudflare",
"cloudflare-derive-macros",
"cloudflare-examples",
"cloudflare-e2e-test",
]
13 changes: 13 additions & 0 deletions cloudflare-derive-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "cloudflare-derive-macros"
version = "0.1.0"
edition = "2018"
description = "Internal macros for the Cloudflare crate"
license = "BSD-3-Clause"

[lib]
proc-macro = true

[dependencies]
syn = "1.0"
quote = "1.0"
27 changes: 27 additions & 0 deletions cloudflare-derive-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

#[proc_macro_derive(ApiResult)]
pub fn api_result_derive(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
let name = &ast.ident;

let gen = quote! {
impl ::cloudflare::framework::response::ApiResult for #name {}
};

gen.into()
}

#[proc_macro_derive(VecApiResult)]
pub fn vec_api_result_derive(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
let name = &ast.ident;

let gen = quote! {
impl ::cloudflare::framework::response::ApiResult for Vec<#name> {}
};

gen.into()
}
1 change: 1 addition & 0 deletions cloudflare/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ serde_urlencoded = "0.7.1"
thiserror = "1"
url = "2.2"
uuid = { version = "1.0", features = ["serde"] }
cloudflare-derive-macros = { path = "../cloudflare-derive-macros" }
7 changes: 2 additions & 5 deletions cloudflare/src/endpoints/account/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::framework::response::ApiResult;
use chrono::{DateTime, Utc};
use cloudflare_derive_macros::{ApiResult, VecApiResult};
use serde::{Deserialize, Serialize};

pub mod list_accounts;
Expand All @@ -8,7 +8,7 @@ pub use list_accounts::ListAccounts;
/// Cloudflare Accounts
/// An Account is the root object which owns other resources such as zones, load balancers and billing details.
/// <https://api.cloudflare.com/#accounts-properties>
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, ApiResult, VecApiResult)]
pub struct Account {
/// Account identifier tag.
pub id: String,
Expand Down Expand Up @@ -38,6 +38,3 @@ pub struct AccountDetails {
/// Account name
pub name: String,
}

impl ApiResult for Account {}
impl ApiResult for Vec<Account> {}
12 changes: 3 additions & 9 deletions cloudflare/src/endpoints/argo_tunnel/data_structures.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use chrono::{offset::Utc, DateTime};
use cloudflare_derive_macros::{ApiResult, VecApiResult};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::framework::response::ApiResult;

/// A Named Argo Tunnel
/// This is an Argo Tunnel that has been created. It can be used for routing and subsequent running.
/// <https://api.cloudflare.com/#argo-tunnel-properties>
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, ApiResult, VecApiResult)]
pub struct Tunnel {
pub id: Uuid,
pub created_at: DateTime<Utc>,
Expand All @@ -26,11 +25,8 @@ pub struct ActiveConnection {
pub is_pending_reconnect: bool,
}

impl ApiResult for Tunnel {}
impl ApiResult for Vec<Tunnel> {}

/// The result of a route request for a Named Argo Tunnel
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, ApiResult)]
#[serde(untagged)]
pub enum RouteResult {
Dns(DnsRouteResult),
Expand All @@ -55,5 +51,3 @@ pub enum Change {
New,
Updated,
}

impl ApiResult for RouteResult {}
14 changes: 4 additions & 10 deletions cloudflare/src/endpoints/dns.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use crate::framework::{
endpoint::{serialize_query, EndpointSpec, Method},
response::ApiResult,
};
use crate::framework::endpoint::{serialize_query, EndpointSpec, Method};
/// <https://api.cloudflare.com/#dns-records-for-a-zone-properties>
use crate::framework::{OrderDirection, SearchMatch};
use chrono::offset::Utc;
use chrono::DateTime;
use cloudflare_derive_macros::{ApiResult, VecApiResult};
use serde::{Deserialize, Serialize};
use std::net::{Ipv4Addr, Ipv6Addr};

Expand Down Expand Up @@ -174,13 +172,13 @@ pub enum DnsContent {
SRV { content: String },
}

#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, ApiResult)]
pub struct DeleteDnsRecordResponse {
/// DNS record identifier tag
pub id: String,
}

#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, ApiResult, VecApiResult)]
pub struct DnsRecord {
/// Extra Cloudflare-specific information about the record
pub meta: Meta,
Expand Down Expand Up @@ -208,7 +206,3 @@ pub struct DnsRecord {
/// The domain of the record
pub zone_name: String,
}

impl ApiResult for DnsRecord {}
impl ApiResult for Vec<DnsRecord> {}
impl ApiResult for DeleteDnsRecordResponse {}
5 changes: 2 additions & 3 deletions cloudflare/src/endpoints/load_balancing/delete_lb.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::framework::endpoint::{EndpointSpec, Method};
use crate::framework::response::ApiResult;

use cloudflare_derive_macros::ApiResult;
use serde::Deserialize;

/// Delete Load Balancer
Expand All @@ -25,8 +25,7 @@ impl<'a> EndpointSpec<Response> for DeleteLoadBalancer<'a> {
}
}

#[derive(Deserialize, Clone, Debug)]
#[derive(Deserialize, Clone, Debug, ApiResult)]
pub struct Response {
pub id: String,
}
impl ApiResult for Response {}
5 changes: 2 additions & 3 deletions cloudflare/src/endpoints/load_balancing/delete_pool.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::framework::endpoint::{EndpointSpec, Method};
use crate::framework::response::ApiResult;

use cloudflare_derive_macros::ApiResult;
use serde::Deserialize;

/// Delete Pool
Expand All @@ -25,8 +25,7 @@ impl<'a> EndpointSpec<Response> for DeletePool<'a> {
}
}

#[derive(Deserialize, Clone, Debug)]
#[derive(Deserialize, Clone, Debug, ApiResult)]
pub struct Response {
pub id: String,
}
impl ApiResult for Response {}
3 changes: 0 additions & 3 deletions cloudflare/src/endpoints/load_balancing/list_lb.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::endpoints::load_balancing::LoadBalancer;
use crate::framework::endpoint::{EndpointSpec, Method};
use crate::framework::response::ApiResult;

/// List Load Balancers
/// <https://api.cloudflare.com/#load-balancers-list-load-balancers>
Expand All @@ -18,5 +17,3 @@ impl<'a> EndpointSpec<Vec<LoadBalancer>> for ListLoadBalancers<'a> {
format!("zones/{}/load_balancers", self.zone_identifier)
}
}

impl ApiResult for Vec<LoadBalancer> {}
13 changes: 4 additions & 9 deletions cloudflare/src/endpoints/load_balancing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ pub mod delete_pool;
pub mod list_lb;
pub mod pool_details;

use crate::framework::response::ApiResult;
use chrono::offset::Utc;
use chrono::DateTime;
use cloudflare_derive_macros::{ApiResult, VecApiResult};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};
use std::net::IpAddr;

#[derive(Eq, PartialEq, Deserialize, Serialize, Clone, Debug)]
#[derive(Eq, PartialEq, Deserialize, Serialize, Clone, Debug, ApiResult, VecApiResult)]
pub struct LoadBalancer {
pub id: String,
pub created_on: DateTime<Utc>,
Expand Down Expand Up @@ -108,8 +108,6 @@ pub enum Secure {
Never,
}

impl ApiResult for LoadBalancer {}

/// A pool is a set of origins that requests could be routed to (e.g. each of your data centers or
/// regions have its own pool).
/// Requests will be routed to particular pools according to your steering policy, and then balanced
Expand All @@ -120,7 +118,7 @@ impl ApiResult for LoadBalancer {}
/// handle. Then you might use a "dynamic latency" steering policy to ensure requests get routed
/// to whatever pool can serve them fastest. So US users will probably get routed to the US pool. If
/// the US pool becomes unavailable, they'll fail over to the Oceania pool.
#[derive(Eq, PartialEq, Deserialize, Serialize, Clone, Debug)]
#[derive(Eq, PartialEq, Deserialize, Serialize, Clone, Debug, ApiResult)]
pub struct Pool {
pub id: String,
pub created_on: DateTime<Utc>,
Expand Down Expand Up @@ -151,7 +149,7 @@ pub struct Pool {
/// An origin represents something that can serve user requests. Usually a machine, maybe an ELB.
/// Origins with similar latency functions (e.g. origins in the same data center or region) might be
/// in the same pool.
#[derive(Deserialize, Serialize, Clone, Debug)]
#[derive(Deserialize, Serialize, Clone, Debug, ApiResult)]
pub struct Origin {
/// A human-identifiable name for the origin.
/// e.g. app-server-1
Expand Down Expand Up @@ -190,6 +188,3 @@ impl Hash for Origin {
self.weight.to_bits().hash(state);
}
}

impl ApiResult for Origin {}
impl ApiResult for Pool {}
4 changes: 2 additions & 2 deletions cloudflare/src/endpoints/r2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use chrono::offset::Utc;
use chrono::DateTime;
use cloudflare_derive_macros::ApiResult;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

Expand All @@ -16,14 +17,13 @@ pub struct Bucket {
}

/// ListBucketsResult contains a list of buckets in an account.
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, ApiResult)]
pub struct ListBucketsResult {
pub buckets: Vec<Bucket>,
}

type EmptyMap = HashMap<(), ()>;
impl ApiResult for EmptyMap {}
impl ApiResult for ListBucketsResult {}

/// Lists all buckets within the account.
#[derive(Debug)]
Expand Down
8 changes: 3 additions & 5 deletions cloudflare/src/endpoints/user.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::framework::endpoint::{EndpointSpec, Method};
use crate::framework::response::ApiResult;

use chrono::{DateTime, Utc};
use cloudflare_derive_macros::ApiResult;
use serde::{Deserialize, Serialize};

/// Get User Details
Expand All @@ -17,7 +17,7 @@ pub struct Organization {
roles: Vec<String>, // List of role names for the User at the Organization
}

#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, ApiResult)]
pub struct UserDetails {
pub organizations: Vec<Organization>,
#[serde(default)]
Expand All @@ -35,7 +35,6 @@ pub struct UserDetails {
pub suspended: bool,
pub email: String,
}
impl ApiResult for UserDetails {}

#[test]
fn handles_empty_betas_field() {
Expand Down Expand Up @@ -81,12 +80,11 @@ impl EndpointSpec<UserDetails> for GetUserDetails {
/// Returns whether a given token is valid or not.
/// <https://blog.cloudflare.com/api-tokens-general-availability/>
///
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, ApiResult)]
pub struct UserTokenStatus {
pub id: String,
pub status: String,
}
impl ApiResult for UserTokenStatus {}

#[derive(Debug)]
pub struct GetUserTokenStatus {}
Expand Down
5 changes: 2 additions & 3 deletions cloudflare/src/endpoints/workers/delete_script.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::framework::endpoint::{EndpointSpec, Method};
use crate::framework::response::ApiResult;

use cloudflare_derive_macros::ApiResult;
use serde::{Deserialize, Serialize};

/// Delete Workers script
Expand All @@ -25,8 +25,7 @@ impl<'a> EndpointSpec<ScriptDeleteID> for DeleteScript<'a> {
}
}

#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, ApiResult)]
pub struct ScriptDeleteID {
pub id: String,
}
impl ApiResult for ScriptDeleteID {}
Loading