diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 79 | ||||
-rw-r--r-- | src/misc.rs | 10 | ||||
-rw-r--r-- | src/notification.rs | 80 | ||||
-rw-r--r-- | src/organization.rs | 2 | ||||
-rw-r--r-- | src/package.rs | 94 | ||||
-rw-r--r-- | src/repository.rs | 52 |
6 files changed, 226 insertions, 91 deletions
@@ -8,19 +8,19 @@ pub struct Forgejo { client: Client, } +mod issue; mod misc; mod notification; mod organization; mod package; -mod issue; mod repository; mod user; +pub use issue::*; pub use misc::*; pub use notification::*; pub use organization::*; pub use package::*; -pub use issue::*; pub use repository::*; pub use user::*; @@ -124,21 +124,13 @@ impl Forgejo { self.execute_str(request).await } - async fn post_unit<T: Serialize>( - &self, - path: &str, - body: &T, - ) -> Result<(), ForgejoError> { + async fn post_unit<T: Serialize>(&self, path: &str, body: &T) -> Result<(), ForgejoError> { let url = self.url.join("api/v1/").unwrap().join(path).unwrap(); let request = self.client.post(url).json(body).build()?; self.execute_unit(request).await } - async fn post_raw( - &self, - path: &str, - body: String, - ) -> Result<String, ForgejoError> { + async fn post_raw(&self, path: &str, body: String) -> Result<String, ForgejoError> { let url = self.url.join("api/v1/").unwrap().join(path).unwrap(); let request = self.client.post(url).body(body).build()?; self.execute_str(request).await @@ -171,12 +163,17 @@ impl Forgejo { match response.status() { status if status.is_success() => { let body = response.text().await?; - let out = serde_json::from_str(&body).map_err(|e| ForgejoError::BadStructure(e, body))?; + let out = + serde_json::from_str(&body).map_err(|e| ForgejoError::BadStructure(e, body))?; Ok(out) - }, + } status if status.is_client_error() => Err(ForgejoError::ApiError( status, - response.json::<ErrorMessage>().await?.message.unwrap_or_else(|| String::from("[no message]")), + response + .json::<ErrorMessage>() + .await? + .message + .unwrap_or_else(|| String::from("[no message]")), )), status => Err(ForgejoError::UnexpectedStatusCode(status)), } @@ -189,7 +186,11 @@ impl Forgejo { status if status.is_success() => Ok(response.text().await?), status if status.is_client_error() => Err(ForgejoError::ApiError( status, - response.json::<ErrorMessage>().await?.message.unwrap_or_else(|| String::from("[no message]")), + response + .json::<ErrorMessage>() + .await? + .message + .unwrap_or_else(|| String::from("[no message]")), )), status => Err(ForgejoError::UnexpectedStatusCode(status)), } @@ -202,7 +203,11 @@ impl Forgejo { status if status.is_success() => Ok(()), status if status.is_client_error() => Err(ForgejoError::ApiError( status, - response.json::<ErrorMessage>().await?.message.unwrap_or_else(|| String::from("[no message]")), + response + .json::<ErrorMessage>() + .await? + .message + .unwrap_or_else(|| String::from("[no message]")), )), status => Err(ForgejoError::UnexpectedStatusCode(status)), } @@ -217,30 +222,36 @@ impl Forgejo { match response.status() { status if status.is_success() => { let body = response.text().await?; - let out = serde_json::from_str(&body).map_err(|e| ForgejoError::BadStructure(e, body))?; + let out = + serde_json::from_str(&body).map_err(|e| ForgejoError::BadStructure(e, body))?; Ok(out) - }, + } StatusCode::NOT_FOUND => Ok(None), status if status.is_client_error() => Err(ForgejoError::ApiError( status, - response.json::<ErrorMessage>().await?.message.unwrap_or_else(|| String::from("[no message]")), + response + .json::<ErrorMessage>() + .await? + .message + .unwrap_or_else(|| String::from("[no message]")), )), status => Err(ForgejoError::UnexpectedStatusCode(status)), } } /// Like `execute`, but returns `false` on 404. - async fn execute_exists( - &self, - request: Request, - ) -> Result<bool, ForgejoError> { + async fn execute_exists(&self, request: Request) -> Result<bool, ForgejoError> { let response = self.client.execute(request).await?; match response.status() { status if status.is_success() => Ok(true), StatusCode::NOT_FOUND => Ok(false), status if status.is_client_error() => Err(ForgejoError::ApiError( status, - response.json::<ErrorMessage>().await?.message.unwrap_or_else(|| String::from("[no message]")), + response + .json::<ErrorMessage>() + .await? + .message + .unwrap_or_else(|| String::from("[no message]")), )), status => Err(ForgejoError::UnexpectedStatusCode(status)), } @@ -256,14 +267,15 @@ struct ErrorMessage { // Forgejo can return blank strings for URLs. This handles that by deserializing // that as `None` -fn none_if_blank_url<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<Option<Url>, D::Error> { +fn none_if_blank_url<'de, D: serde::Deserializer<'de>>( + deserializer: D, +) -> Result<Option<Url>, D::Error> { use serde::de::{Error, Unexpected, Visitor}; use std::fmt; struct EmptyUrlVisitor; - impl<'de> Visitor<'de> for EmptyUrlVisitor - { + impl<'de> Visitor<'de> for EmptyUrlVisitor { type Value = Option<Url>; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -294,13 +306,14 @@ fn none_if_blank_url<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Resul if s.is_empty() { return Ok(None); } - Url::parse(s).map_err(|err| { - let err_s = format!("{}", err); - Error::invalid_value(Unexpected::Str(s), &err_s.as_str()) - }).map(Some) + Url::parse(s) + .map_err(|err| { + let err_s = format!("{}", err); + Error::invalid_value(Unexpected::Str(s), &err_s.as_str()) + }) + .map(Some) } } deserializer.deserialize_str(EmptyUrlVisitor) } - diff --git a/src/misc.rs b/src/misc.rs index e2aeb5a..76fc3b8 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -5,7 +5,10 @@ impl Forgejo { self.get("gitignore/templates").await } - pub async fn get_gitignore_template(&self, name: &str) -> Result<Option<GitignoreTemplateInfo>, ForgejoError> { + pub async fn get_gitignore_template( + &self, + name: &str, + ) -> Result<Option<GitignoreTemplateInfo>, ForgejoError> { self.get_opt(&format!("gitignore/templates/{name}")).await } @@ -21,7 +24,10 @@ impl Forgejo { self.get("licenses").await } - pub async fn get_license(&self, name: &str) -> Result<Option<GitignoreTemplateInfo>, ForgejoError> { + pub async fn get_license( + &self, + name: &str, + ) -> Result<Option<GitignoreTemplateInfo>, ForgejoError> { self.get_opt(&format!("license/{name}")).await } diff --git a/src/notification.rs b/src/notification.rs index 9dbff21..348363a 100644 --- a/src/notification.rs +++ b/src/notification.rs @@ -1,32 +1,72 @@ use super::*; impl Forgejo { - pub async fn notifications(&self, query: NotificationQuery) -> Result<Vec<NotificationThread>, ForgejoError> { - self.get(&format!("notifications?{}", query.query_string())).await + pub async fn notifications( + &self, + query: NotificationQuery, + ) -> Result<Vec<NotificationThread>, ForgejoError> { + self.get(&format!("notifications?{}", query.query_string())) + .await } - pub async fn set_notifications_state(&self, query: NotificationPutQuery) -> Result<Vec<NotificationThread>, ForgejoError> { - self.put(&format!("notifications?{}", query.query_string())).await + pub async fn set_notifications_state( + &self, + query: NotificationPutQuery, + ) -> Result<Vec<NotificationThread>, ForgejoError> { + self.put(&format!("notifications?{}", query.query_string())) + .await } pub async fn notification_count(&self) -> Result<Vec<NotificationCount>, ForgejoError> { self.get("notifications/new").await } - pub async fn get_notification(&self, id: u64) -> Result<Option<NotificationThread>, ForgejoError> { + pub async fn get_notification( + &self, + id: u64, + ) -> Result<Option<NotificationThread>, ForgejoError> { self.get_opt(&format!("notifications/threads/{id}")).await } - pub async fn set_notification_state(&self, id: u64, to_status: ToStatus) -> Result<Option<NotificationThread>, ForgejoError> { - self.patch(&format!("notifications/threads/{id}?to-status={}", to_status.as_str()), &()).await + pub async fn set_notification_state( + &self, + id: u64, + to_status: ToStatus, + ) -> Result<Option<NotificationThread>, ForgejoError> { + self.patch( + &format!( + "notifications/threads/{id}?to-status={}", + to_status.as_str() + ), + &(), + ) + .await } - pub async fn get_repo_notifications(&self, owner: &str, name: &str, query: NotificationQuery) -> Result<Vec<NotificationThread>, ForgejoError> { - self.get(&format!("repos/{owner}/{name}/notifications?{}", query.query_string())).await + pub async fn get_repo_notifications( + &self, + owner: &str, + name: &str, + query: NotificationQuery, + ) -> Result<Vec<NotificationThread>, ForgejoError> { + self.get(&format!( + "repos/{owner}/{name}/notifications?{}", + query.query_string() + )) + .await } - pub async fn set_repo_notifications_state(&self, owner: &str, name: &str, query: NotificationPutQuery) -> Result<Vec<NotificationThread>, ForgejoError> { - self.put(&format!("repos/{owner}/{name}/notifications?{}", query.query_string())).await + pub async fn set_repo_notifications_state( + &self, + owner: &str, + name: &str, + query: NotificationPutQuery, + ) -> Result<Vec<NotificationThread>, ForgejoError> { + self.put(&format!( + "repos/{owner}/{name}/notifications?{}", + query.query_string() + )) + .await } } @@ -82,12 +122,20 @@ impl NotificationQuery { } if let Some(since) = &self.since { s.push_str("since="); - s.push_str(&since.format(&time::format_description::well_known::Rfc3339).unwrap()); + s.push_str( + &since + .format(&time::format_description::well_known::Rfc3339) + .unwrap(), + ); s.push('&'); } if let Some(before) = &self.before { s.push_str("before="); - s.push_str(&before.format(&time::format_description::well_known::Rfc3339).unwrap()); + s.push_str( + &before + .format(&time::format_description::well_known::Rfc3339) + .unwrap(), + ); s.push('&'); } if let Some(page) = self.page { @@ -176,7 +224,11 @@ impl NotificationPutQuery { let mut s = String::new(); if let Some(last_read_at) = &self.last_read_at { s.push_str("since="); - s.push_str(&last_read_at.format(&time::format_description::well_known::Rfc3339).unwrap()); + s.push_str( + &last_read_at + .format(&time::format_description::well_known::Rfc3339) + .unwrap(), + ); s.push('&'); } if self.all { diff --git a/src/organization.rs b/src/organization.rs index 55cbd45..6a54f10 100644 --- a/src/organization.rs +++ b/src/organization.rs @@ -1,5 +1,5 @@ -use std::collections::BTreeMap; use crate::*; +use std::collections::BTreeMap; #[derive(serde::Deserialize, Debug, PartialEq)] pub struct Organization { diff --git a/src/package.rs b/src/package.rs index 5788704..4c49d5b 100644 --- a/src/package.rs +++ b/src/package.rs @@ -3,20 +3,54 @@ use std::fmt::Write; use super::*; impl Forgejo { - pub async fn get_user_packages(&self, owner: &str, query: PackagesQuery) -> Result<Vec<Package>, ForgejoError> { + pub async fn get_user_packages( + &self, + owner: &str, + query: PackagesQuery, + ) -> Result<Vec<Package>, ForgejoError> { self.get(&query.path(owner)).await } - - pub async fn get_package(&self, owner: &str, _type: PackageType, name: &str, version: &str) -> Result<Option<Package>, ForgejoError> { - self.get_opt(&format!("packages/{owner}/{}/{name}/{version}", _type.as_str())).await + + pub async fn get_package( + &self, + owner: &str, + _type: PackageType, + name: &str, + version: &str, + ) -> Result<Option<Package>, ForgejoError> { + self.get_opt(&format!( + "packages/{owner}/{}/{name}/{version}", + _type.as_str() + )) + .await } - - pub async fn delete_package(&self, owner: &str, _type: PackageType, name: &str, version: &str) -> Result<(), ForgejoError> { - self.delete(&format!("packages/{owner}/{}/{name}/{version}", _type.as_str())).await + + pub async fn delete_package( + &self, + owner: &str, + _type: PackageType, + name: &str, + version: &str, + ) -> Result<(), ForgejoError> { + self.delete(&format!( + "packages/{owner}/{}/{name}/{version}", + _type.as_str() + )) + .await } - pub async fn get_package_files(&self, owner: &str, _type: PackageType, name: &str, version: &str) -> Result<Vec<PackageFile>, ForgejoError> { - self.get(&format!("packages/{owner}/{}/{name}/{version}", _type.as_str())).await + pub async fn get_package_files( + &self, + owner: &str, + _type: PackageType, + name: &str, + version: &str, + ) -> Result<Vec<PackageFile>, ForgejoError> { + self.get(&format!( + "packages/{owner}/{}/{name}/{version}", + _type.as_str() + )) + .await } } @@ -35,12 +69,14 @@ impl PackagesQuery { s.push('?'); if let Some(page) = self.page { s.push_str("page="); - s.write_fmt(format_args!("{page}")).expect("writing to string can't fail"); + s.write_fmt(format_args!("{page}")) + .expect("writing to string can't fail"); s.push('&'); } if let Some(limit) = self.limit { s.push_str("limit="); - s.write_fmt(format_args!("{limit}")).expect("writing to string can't fail"); + s.write_fmt(format_args!("{limit}")) + .expect("writing to string can't fail"); s.push('&'); } if let Some(kind) = self.kind { @@ -63,24 +99,24 @@ impl PackagesQuery { pub enum PackageType { Alpine, Cargo, - Chef, - Composer, - Conan, - Conda, - Container, - Cran, - Debian, - Generic, - Go, - Helm, - Maven, - Npm, - Nuget, - Pub, - Pypi, - Rpm, - RubyGems, - Swift, + Chef, + Composer, + Conan, + Conda, + Container, + Cran, + Debian, + Generic, + Go, + Helm, + Maven, + Npm, + Nuget, + Pub, + Pypi, + Rpm, + RubyGems, + Swift, Vagrant, } diff --git a/src/repository.rs b/src/repository.rs index 8b063a0..966b5e3 100644 --- a/src/repository.rs +++ b/src/repository.rs @@ -3,7 +3,11 @@ use super::*; /// Repository operations. impl Forgejo { /// Gets info about the specified repository. - pub async fn get_repo(&self, user: &str, repo: &str) -> Result<Option<Repository>, ForgejoError> { + pub async fn get_repo( + &self, + user: &str, + repo: &str, + ) -> Result<Option<Repository>, ForgejoError> { self.get_opt(&format!("repos/{user}/{repo}/")).await } @@ -32,7 +36,8 @@ impl Forgejo { } pub async fn is_merged(&self, owner: &str, repo: &str, pr: u64) -> Result<bool, ForgejoError> { - self.get_exists(&format!("repos/{owner}/{repo}/pulls/{pr}/merge")).await + self.get_exists(&format!("repos/{owner}/{repo}/pulls/{pr}/merge")) + .await } pub async fn merge_pr( @@ -144,8 +149,13 @@ impl Forgejo { ) -> Result<Attachment, ForgejoError> { self.post_multipart( &format!("repos/{owner}/{repo}/releases/{id}/assets?name={name}"), - reqwest::multipart::Form::new() - .part("attachment", reqwest::multipart::Part::bytes(file).file_name("file").mime_str("*/*").unwrap()), + reqwest::multipart::Form::new().part( + "attachment", + reqwest::multipart::Part::bytes(file) + .file_name("file") + .mime_str("*/*") + .unwrap(), + ), ) .await } @@ -197,20 +207,38 @@ impl Forgejo { .await } - pub async fn get_tags(&self, owner: &str, repo: &str, query: TagQuery) -> Result<Vec<Tag>, ForgejoError> { + pub async fn get_tags( + &self, + owner: &str, + repo: &str, + query: TagQuery, + ) -> Result<Vec<Tag>, ForgejoError> { self.get(&query.to_string(owner, repo)).await } - pub async fn create_tag(&self, owner: &str, repo: &str, opts: CreateTagOption) -> Result<Tag, ForgejoError> { - self.post(&format!("repos/{owner}/{repo}/tags"), &opts).await + pub async fn create_tag( + &self, + owner: &str, + repo: &str, + opts: CreateTagOption, + ) -> Result<Tag, ForgejoError> { + self.post(&format!("repos/{owner}/{repo}/tags"), &opts) + .await } - pub async fn get_tag(&self, owner: &str, repo: &str, tag: &str) -> Result<Option<Tag>, ForgejoError> { - self.get_opt(&format!("repos/{owner}/{repo}/tags/{tag}")).await + pub async fn get_tag( + &self, + owner: &str, + repo: &str, + tag: &str, + ) -> Result<Option<Tag>, ForgejoError> { + self.get_opt(&format!("repos/{owner}/{repo}/tags/{tag}")) + .await } pub async fn delete_tag(&self, owner: &str, repo: &str, tag: &str) -> Result<(), ForgejoError> { - self.delete(&format!("repos/{owner}/{repo}/tags/{tag}")).await + self.delete(&format!("repos/{owner}/{repo}/tags/{tag}")) + .await } } @@ -608,7 +636,8 @@ pub struct TagQuery { impl TagQuery { fn to_string(&self, owner: &str, repo: &str) -> String { - format!("repos/{owner}/{repo}/tags?page={}&limit={}", + format!( + "repos/{owner}/{repo}/tags?page={}&limit={}", self.page.map(|page| page.to_string()).unwrap_or_default(), self.limit.map(|page| page.to_string()).unwrap_or_default(), ) @@ -661,4 +690,3 @@ pub struct RepoTransfer { pub recipient: User, pub teams: Vec<Team>, } - |