Skip to content

Commit 2a09eda

Browse files
authored
Rename GetPublicKeys trait to GitProvider (#14)
* Rename `GitProvider` to `GitProviderType` and move it to config module * Rename `GetPublicKeys` trait to `GitProvider`
1 parent 94b200e commit 2a09eda

File tree

7 files changed

+41
-36
lines changed

7 files changed

+41
-36
lines changed

src/cli/manage_sources.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::GitProvider;
1+
use crate::config;
22
use clap::Subcommand;
33

44
#[derive(Debug, Subcommand)]
@@ -9,7 +9,7 @@ pub enum ManageSources {
99
name: String,
1010
/// The Git provider used by the source.
1111
#[arg(short, long)]
12-
provider: GitProvider,
12+
provider: config::GitProviderType,
1313
/// The URL of the source.
1414
#[arg(short, long)]
1515
url: Option<reqwest::Url>,

src/config.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::GitProvider;
21
use figment::{
32
providers::{Format, Serialized, Toml},
43
Figment,
@@ -31,12 +30,12 @@ impl Default for Config {
3130
sources: Some(vec![
3231
Source {
3332
name: "github".to_string(),
34-
provider: GitProvider::Github,
33+
provider: GitProviderType::Github,
3534
url: "https://api.github.com".to_string(),
3635
},
3736
Source {
3837
name: "gitlab".to_string(),
39-
provider: GitProvider::Gitlab,
38+
provider: GitProviderType::Gitlab,
4039
url: "https://gitlab.com".to_string(),
4140
},
4241
]),
@@ -83,6 +82,16 @@ fn git_allowed_signers() -> Option<PathBuf> {
8382
Some(path.into())
8483
}
8584

85+
/// The type of Git provider.
86+
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, clap::ValueEnum)]
87+
#[serde(rename_all = "lowercase")]
88+
pub enum GitProviderType {
89+
/// A Git provider that implements the GitHub API.
90+
Github,
91+
/// A Git provider that implements the GitLab API.
92+
Gitlab,
93+
}
94+
8695
#[derive(Debug, Deserialize, Serialize, PartialEq)]
8796
struct User {
8897
name: String,
@@ -98,7 +107,7 @@ struct Organization {
98107
#[derive(Debug, Deserialize, Serialize, PartialEq)]
99108
struct Source {
100109
name: String,
101-
provider: GitProvider,
110+
provider: GitProviderType,
102111
url: String,
103112
}
104113

@@ -168,17 +177,17 @@ mod tests {
168177
sources: Some(vec![
169178
Source {
170179
name: "github".to_string(),
171-
provider: GitProvider::Github,
180+
provider: GitProviderType::Github,
172181
url: "https://api.github.com".to_string(),
173182
},
174183
Source {
175184
name: "gitlab".to_string(),
176-
provider: GitProvider::Gitlab,
185+
provider: GitProviderType::Gitlab,
177186
url: "https://gitlab.com".to_string(),
178187
},
179188
Source {
180189
name: "acme-corp".to_string(),
181-
provider: GitProvider::Gitlab,
190+
provider: GitProviderType::Gitlab,
182191
url: "https://git.acme.corp".to_string(),
183192
},
184193
])

src/core.rs

-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use async_trait::async_trait;
21
use serde::{Deserialize, Serialize};
32
use std::{fmt, str::FromStr};
43

@@ -22,19 +21,3 @@ impl fmt::Display for SshPublicKey {
2221
write!(f, "{}", self.key)
2322
}
2423
}
25-
26-
#[async_trait]
27-
pub trait GetPublicKeys {
28-
type Err;
29-
30-
/// Get the public keys of a user by their username.
31-
async fn by_username(&self, username: &str) -> Result<Vec<SshPublicKey>, Self::Err>;
32-
}
33-
34-
/// A Git provider.
35-
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, clap::ValueEnum)]
36-
#[serde(rename_all = "lowercase")]
37-
pub enum GitProvider {
38-
Github,
39-
Gitlab,
40-
}

src/github.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{GetPublicKeys, SshPublicKey, USER_AGENT};
1+
use crate::{GitProvider, SshPublicKey, USER_AGENT};
22
use async_trait::async_trait;
33
use reqwest::{Client, Result, Url};
44

@@ -16,14 +16,14 @@ impl Api<'_> {
1616
}
1717

1818
#[async_trait]
19-
impl GetPublicKeys for Api<'_> {
19+
impl GitProvider for Api<'_> {
2020
type Err = reqwest::Error;
2121

2222
/// Get the signing keys of a user by their username.
2323
///
2424
/// # API documentation
2525
/// https://docs.github.com/en/rest/users/ssh-signing-keys?apiVersion=2022-11-28#list-ssh-signing-keys-for-a-user
26-
async fn by_username(&self, username: &str) -> Result<Vec<SshPublicKey>> {
26+
async fn get_keys_by_username(&self, username: &str) -> Result<Vec<SshPublicKey>> {
2727
let url = self
2828
.base_url
2929
.join(&format!("/users/{username}/ssh_signing_keys"))
@@ -67,7 +67,7 @@ mod tests {
6767
base_url: server.base_url().parse().unwrap(),
6868
client: &client,
6969
};
70-
let _ = api.by_username(username).await;
70+
let _ = api.get_keys_by_username(username).await;
7171

7272
mock.assert();
7373
}
@@ -122,7 +122,7 @@ mod tests {
122122
base_url: server.base_url().parse().unwrap(),
123123
client: &client,
124124
};
125-
let keys = api.by_username(username).await.unwrap();
125+
let keys = api.get_keys_by_username(username).await.unwrap();
126126

127127
assert_eq!(keys, expected);
128128
}

src/gitlab.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{GetPublicKeys, SshPublicKey, USER_AGENT};
1+
use crate::{GitProvider, SshPublicKey, USER_AGENT};
22
use async_trait::async_trait;
33
use reqwest::{Client, Result, Url};
44
use serde::Deserialize;
@@ -17,14 +17,14 @@ impl Api<'_> {
1717
}
1818

1919
#[async_trait]
20-
impl GetPublicKeys for Api<'_> {
20+
impl GitProvider for Api<'_> {
2121
type Err = reqwest::Error;
2222

2323
/// Get the signing keys of a user by their username.
2424
///
2525
/// # API documentation
2626
/// https://docs.gitlab.com/16.10/ee/api/users.html#list-ssh-keys-for-user
27-
async fn by_username(&self, username: &str) -> Result<Vec<SshPublicKey>> {
27+
async fn get_keys_by_username(&self, username: &str) -> Result<Vec<SshPublicKey>> {
2828
let url = self
2929
.base_url
3030
.join(&format!(
@@ -110,7 +110,7 @@ mod tests {
110110
base_url: server.base_url().parse().unwrap(),
111111
client: &client,
112112
};
113-
let _ = api.by_username(username).await;
113+
let _ = api.get_keys_by_username(username).await;
114114

115115
mock.assert();
116116
}
@@ -170,7 +170,7 @@ mod tests {
170170
base_url: server.base_url().parse().unwrap(),
171171
client: &client,
172172
};
173-
let keys = api.by_username(username).await.unwrap();
173+
let keys = api.get_keys_by_username(username).await.unwrap();
174174

175175
assert_eq!(keys, expected);
176176
}

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
pub use allowed_signers::{AllowedSigner, AllowedSignersFile};
22
pub use config::Config;
33
pub use core::*;
4+
pub use provider::GitProvider;
45

56
mod allowed_signers;
67
pub mod cli;
78
mod config;
89
mod core;
910
mod github;
1011
mod gitlab;
12+
mod provider;

src/provider.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use crate::SshPublicKey;
2+
use async_trait::async_trait;
3+
4+
/// A Git provider.
5+
#[async_trait]
6+
pub trait GitProvider {
7+
type Err;
8+
9+
/// Get the public keys of a user by their username.
10+
async fn get_keys_by_username(&self, username: &str) -> Result<Vec<SshPublicKey>, Self::Err>;
11+
}

0 commit comments

Comments
 (0)