Skip to content

Commit 8b36f23

Browse files
authored
Use an enum for GitProvider instead of trait (#16)
1 parent b24ed9e commit 8b36f23

File tree

8 files changed

+39
-39
lines changed

8 files changed

+39
-39
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
async-trait = "0.1.79"
109
chrono = "0.4.37"
1110
clap = { version = "4.5.4", features = ["derive", "env", "string"] }
1211
figment = { version = "0.10.16", features = ["toml"] }

src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,4 @@ mod allowed_signers;
77
pub mod cli;
88
mod config;
99
mod core;
10-
mod github;
11-
mod gitlab;
1210
mod provider;

src/provider.rs

-11
This file was deleted.

src/provider/core.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use super::{github::Github, gitlab::Gitlab};
2+
use crate::SshPublicKey;
3+
4+
/// A Git provider.
5+
#[derive(Debug)]
6+
pub enum GitProvider<'a> {
7+
Github(Github<'a>),
8+
Gitlab(Gitlab<'a>),
9+
}
10+
11+
impl GitProvider<'_> {
12+
/// Get the public keys of a user by their username.
13+
async fn get_keys_by_username(
14+
&self,
15+
username: &str,
16+
) -> Result<Vec<SshPublicKey>, reqwest::Error> {
17+
match self {
18+
Self::Github(provider) => provider.get_keys_by_username(username).await,
19+
Self::Gitlab(provider) => provider.get_keys_by_username(username).await,
20+
}
21+
}
22+
}

src/github.rs src/provider/github.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
1-
use crate::{GitProvider, SshPublicKey, USER_AGENT};
2-
use async_trait::async_trait;
1+
use crate::{SshPublicKey, USER_AGENT};
32
use reqwest::{Client, Result, Url};
43

54
#[derive(Debug)]
6-
pub struct Api<'a> {
5+
pub struct Github<'a> {
76
/// The base URL of the API.
87
base_url: Url,
98
/// The client used to make requests to the API.
109
client: &'a Client,
1110
}
1211

13-
impl Api<'_> {
12+
impl Github<'_> {
1413
const VERSION: &'static str = "2022-11-28";
1514
const ACCEPT_HEADER: &'static str = "application/vnd.github+json";
16-
}
17-
18-
#[async_trait]
19-
impl GitProvider for Api<'_> {
20-
type Err = reqwest::Error;
2115

2216
/// Get the signing keys of a user by their username.
2317
///
2418
/// # API documentation
2519
/// https://docs.github.com/en/rest/users/ssh-signing-keys?apiVersion=2022-11-28#list-ssh-signing-keys-for-a-user
26-
async fn get_keys_by_username(&self, username: &str) -> Result<Vec<SshPublicKey>> {
20+
pub async fn get_keys_by_username(&self, username: &str) -> Result<Vec<SshPublicKey>> {
2721
let url = self
2822
.base_url
2923
.join(&format!("/users/{username}/ssh_signing_keys"))
@@ -63,7 +57,7 @@ mod tests {
6357
});
6458

6559
let client = Client::new();
66-
let api = Api {
60+
let api = Github {
6761
base_url: server.base_url().parse().unwrap(),
6862
client: &client,
6963
};
@@ -118,7 +112,7 @@ mod tests {
118112
});
119113

120114
let client = Client::new();
121-
let api = Api {
115+
let api = Github {
122116
base_url: server.base_url().parse().unwrap(),
123117
client: &client,
124118
};

src/gitlab.rs src/provider/gitlab.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
1-
use crate::{GitProvider, SshPublicKey, USER_AGENT};
2-
use async_trait::async_trait;
1+
use crate::{SshPublicKey, USER_AGENT};
32
use reqwest::{Client, Result, Url};
43
use serde::Deserialize;
54

65
#[derive(Debug)]
7-
pub struct Api<'a> {
6+
pub struct Gitlab<'a> {
87
/// The base URL of the API.
98
base_url: Url,
109
/// The client used to make requests to the API.
1110
client: &'a Client,
1211
}
1312

14-
impl Api<'_> {
13+
impl Gitlab<'_> {
1514
const VERSION: &'static str = "v4";
1615
const ACCEPT_HEADER: &'static str = "application/json";
17-
}
18-
19-
#[async_trait]
20-
impl GitProvider for Api<'_> {
21-
type Err = reqwest::Error;
2216

2317
/// Get the signing keys of a user by their username.
2418
///
2519
/// # API documentation
2620
/// https://docs.gitlab.com/16.10/ee/api/users.html#list-ssh-keys-for-user
27-
async fn get_keys_by_username(&self, username: &str) -> Result<Vec<SshPublicKey>> {
21+
pub async fn get_keys_by_username(&self, username: &str) -> Result<Vec<SshPublicKey>> {
2822
let url = self
2923
.base_url
3024
.join(&format!(
@@ -106,7 +100,7 @@ mod tests {
106100
});
107101

108102
let client = Client::new();
109-
let api = Api {
103+
let api = Gitlab {
110104
base_url: server.base_url().parse().unwrap(),
111105
client: &client,
112106
};
@@ -166,7 +160,7 @@ mod tests {
166160
});
167161

168162
let client = Client::new();
169-
let api = Api {
163+
let api = Gitlab {
170164
base_url: server.base_url().parse().unwrap(),
171165
client: &client,
172166
};

src/provider/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub use core::GitProvider;
2+
3+
mod core;
4+
mod github;
5+
mod gitlab;

0 commit comments

Comments
 (0)