From 3fd8bfccab37184fd5bd706fec36d9d03da8c765 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Wed, 6 Oct 2021 18:40:16 +0200 Subject: [PATCH] style(package): fix formatting --- espanso-package/src/archive/util.rs | 4 +- espanso-package/src/lib.rs | 11 ++++- espanso-package/src/logging.rs | 2 +- espanso-package/src/package/default.rs | 8 +--- espanso-package/src/package/mod.rs | 2 +- espanso-package/src/provider/git.rs | 8 +--- espanso-package/src/provider/github.rs | 6 +-- espanso-package/src/provider/gitlab.rs | 6 +-- espanso-package/src/provider/mod.rs | 4 +- espanso-package/src/util/download.rs | 10 +++-- espanso-package/src/util/github.rs | 56 ++++++++++++++++--------- espanso-package/src/util/gitlab.rs | 58 +++++++++++++++++--------- espanso-package/src/util/mod.rs | 2 +- 13 files changed, 107 insertions(+), 70 deletions(-) diff --git a/espanso-package/src/archive/util.rs b/espanso-package/src/archive/util.rs index 8d07613..25394f3 100644 --- a/espanso-package/src/archive/util.rs +++ b/espanso-package/src/archive/util.rs @@ -24,7 +24,7 @@ use fs_extra::dir::CopyOptions; use crate::PackageSpecifier; -use super::{PACKAGE_SOURCE_FILE, PackageSource}; +use super::{PackageSource, PACKAGE_SOURCE_FILE}; // TODO: test pub fn copy_dir_without_dot_files(source_dir: &Path, inside_dir: &Path) -> Result<()> { @@ -57,4 +57,4 @@ pub fn create_package_source_file(specifier: &PackageSpecifier, target_dir: &Pat let yaml = serde_yaml::to_string(&source)?; std::fs::write(target_dir.join(PACKAGE_SOURCE_FILE), yaml)?; Ok(()) -} \ No newline at end of file +} diff --git a/espanso-package/src/lib.rs b/espanso-package/src/lib.rs index 9548291..c5d793c 100644 --- a/espanso-package/src/lib.rs +++ b/espanso-package/src/lib.rs @@ -34,7 +34,11 @@ pub use archive::{ArchivedPackage, Archiver, SaveOptions, StoredPackage}; pub use package::Package; pub use provider::{PackageProvider, PackageSpecifier, ProviderOptions}; -pub fn get_provider(package: &PackageSpecifier, runtime_dir: &Path, options: &ProviderOptions) -> Result> { +pub fn get_provider( + package: &PackageSpecifier, + runtime_dir: &Path, + options: &ProviderOptions, +) -> Result> { if let Some(git_repo_url) = package.git_repo_url.as_deref() { if !package.use_native_git { let matches_known_hosts = @@ -81,7 +85,10 @@ pub fn get_provider(package: &PackageSpecifier, runtime_dir: &Path, options: &Pr Ok(Box::new(provider::git::GitPackageProvider::new())) } else { // Download from the official espanso hub - Ok(Box::new(provider::hub::EspansoHubPackageProvider::new(runtime_dir, options.force_index_update))) + Ok(Box::new(provider::hub::EspansoHubPackageProvider::new( + runtime_dir, + options.force_index_update, + ))) } } diff --git a/espanso-package/src/logging.rs b/espanso-package/src/logging.rs index cb27597..3e31c13 100644 --- a/espanso-package/src/logging.rs +++ b/espanso-package/src/logging.rs @@ -20,4 +20,4 @@ macro_rules! error_eprintln { eprintln!($($tts)*); log::error!($($tts)*); } -} \ No newline at end of file +} diff --git a/espanso-package/src/package/default.rs b/espanso-package/src/package/default.rs index 0733fbb..b5abe9b 100644 --- a/espanso-package/src/package/default.rs +++ b/espanso-package/src/package/default.rs @@ -21,7 +21,7 @@ use std::path::PathBuf; use tempdir::TempDir; -use crate::{Package, manifest::Manifest}; +use crate::{manifest::Manifest, Package}; #[allow(dead_code)] pub struct DefaultPackage { @@ -34,11 +34,7 @@ pub struct DefaultPackage { } impl DefaultPackage { - pub fn new( - manifest: Manifest, - temp_dir: TempDir, - location: PathBuf, - ) -> Self { + pub fn new(manifest: Manifest, temp_dir: TempDir, location: PathBuf) -> Self { Self { manifest, temp_dir, diff --git a/espanso-package/src/package/mod.rs b/espanso-package/src/package/mod.rs index 8a45e7e..a8c559c 100644 --- a/espanso-package/src/package/mod.rs +++ b/espanso-package/src/package/mod.rs @@ -48,4 +48,4 @@ impl std::fmt::Debug for dyn Package { } } -pub use default::DefaultPackage; \ No newline at end of file +pub use default::DefaultPackage; diff --git a/espanso-package/src/provider/git.rs b/espanso-package/src/provider/git.rs index a382c44..d47896d 100644 --- a/espanso-package/src/provider/git.rs +++ b/espanso-package/src/provider/git.rs @@ -17,14 +17,10 @@ * along with espanso. If not, see . */ -use crate::{ - package::DefaultPackage, - resolver::{resolve_package}, - Package, PackageSpecifier, -}; +use super::PackageProvider; +use crate::{package::DefaultPackage, resolver::resolve_package, Package, PackageSpecifier}; use anyhow::{anyhow, bail, Context, Result}; use std::{path::Path, process::Command}; -use super::PackageProvider; pub struct GitPackageProvider {} diff --git a/espanso-package/src/provider/github.rs b/espanso-package/src/provider/github.rs index 22f70c2..d9e4f99 100644 --- a/espanso-package/src/provider/github.rs +++ b/espanso-package/src/provider/github.rs @@ -17,11 +17,9 @@ * along with espanso. If not, see . */ -use crate::{ - package::DefaultPackage, resolver::resolve_package, Package, PackageSpecifier, -}; -use anyhow::{Result}; use super::PackageProvider; +use crate::{package::DefaultPackage, resolver::resolve_package, Package, PackageSpecifier}; +use anyhow::Result; pub struct GitHubPackageProvider { repo_author: String, diff --git a/espanso-package/src/provider/gitlab.rs b/espanso-package/src/provider/gitlab.rs index 4657900..d1627f3 100644 --- a/espanso-package/src/provider/gitlab.rs +++ b/espanso-package/src/provider/gitlab.rs @@ -17,11 +17,9 @@ * along with espanso. If not, see . */ -use crate::{ - package::DefaultPackage, resolver::resolve_package, Package, PackageSpecifier, -}; -use anyhow::{Result}; use super::PackageProvider; +use crate::{package::DefaultPackage, resolver::resolve_package, Package, PackageSpecifier}; +use anyhow::Result; pub struct GitLabPackageProvider { repo_author: String, diff --git a/espanso-package/src/provider/mod.rs b/espanso-package/src/provider/mod.rs index e881bc5..a469005 100644 --- a/espanso-package/src/provider/mod.rs +++ b/espanso-package/src/provider/mod.rs @@ -21,10 +21,10 @@ use anyhow::Result; use crate::Package; -pub(crate) mod hub; pub(crate) mod git; pub(crate) mod github; pub(crate) mod gitlab; +pub(crate) mod hub; #[derive(Debug, Default)] pub struct PackageSpecifier { @@ -48,4 +48,4 @@ pub trait PackageProvider { #[derive(Debug, Default)] pub struct ProviderOptions { pub force_index_update: bool, -} \ No newline at end of file +} diff --git a/espanso-package/src/util/download.rs b/espanso-package/src/util/download.rs index 49724d9..45258a2 100644 --- a/espanso-package/src/util/download.rs +++ b/espanso-package/src/util/download.rs @@ -17,16 +17,20 @@ * along with espanso. If not, see . */ -use anyhow::{Context, Result, bail}; +use anyhow::{bail, Context, Result}; +use sha2::{Digest, Sha256}; use std::io::{copy, Cursor}; use std::path::Path; -use sha2::{Sha256, Digest}; pub fn download_and_extract_zip(url: &str, dest_dir: &Path) -> Result<()> { download_and_extract_zip_verify_sha256(url, dest_dir, None) } -pub fn download_and_extract_zip_verify_sha256(url: &str, dest_dir: &Path, sha256: Option<&str>) -> Result<()> { +pub fn download_and_extract_zip_verify_sha256( + url: &str, + dest_dir: &Path, + sha256: Option<&str>, +) -> Result<()> { let data = download(url).context("error downloading archive")?; if let Some(sha256) = sha256 { info_println!("validating sha256 signature..."); diff --git a/espanso-package/src/util/github.rs b/espanso-package/src/util/github.rs index 92680b6..69cbf8f 100644 --- a/espanso-package/src/util/github.rs +++ b/espanso-package/src/util/github.rs @@ -23,7 +23,9 @@ use regex::Regex; use reqwest::StatusCode; lazy_static! { - static ref GITHUB_REGEX: Regex = Regex::new(r"(https://github.com/|git@github.com:)(?P.*?)/(?P.*?)(\.|$)").unwrap(); + static ref GITHUB_REGEX: Regex = + Regex::new(r"(https://github.com/|git@github.com:)(?P.*?)/(?P.*?)(\.|$)") + .unwrap(); } #[derive(Debug, PartialEq)] @@ -36,7 +38,7 @@ pub fn extract_github_url_parts(url: &str) -> Option { let captures = GITHUB_REGEX.captures(url)?; let author = captures.name("author")?; let name = captures.name("name")?; - + Some(GitHubParts { author: author.as_str().to_string(), name: name.as_str().to_string(), @@ -49,14 +51,17 @@ pub struct ResolvedRepoScheme { pub branch: String, } -pub fn resolve_repo_scheme(parts: GitHubParts, force_branch: Option<&str>) -> Result> { +pub fn resolve_repo_scheme( + parts: GitHubParts, + force_branch: Option<&str>, +) -> Result> { if let Some(force_branch) = force_branch { if check_repo_with_branch(&parts, force_branch)? { return Ok(Some(ResolvedRepoScheme { author: parts.author, name: parts.name, branch: force_branch.to_string(), - })) + })); } } else { if check_repo_with_branch(&parts, "main")? { @@ -74,7 +79,6 @@ pub fn resolve_repo_scheme(parts: GitHubParts, force_branch: Option<&str>) -> Re branch: "master".to_string(), })); } - } Ok(None) @@ -90,7 +94,10 @@ pub fn check_repo_with_branch(parts: &GitHubParts, branch: &str) -> Result } fn generate_github_download_url(parts: &GitHubParts, branch: &str) -> String { - format!("https://github.com/{}/{}/archive/refs/heads/{}.zip", parts.author, parts.name, branch) + format!( + "https://github.com/{}/{}/archive/refs/heads/{}.zip", + parts.author, parts.name, branch + ) } #[cfg(test)] @@ -99,21 +106,32 @@ mod tests { #[test] fn test_extract_github_url_parts() { - assert_eq!(extract_github_url_parts("https://github.com/federico-terzi/espanso").unwrap(), GitHubParts { - author: "federico-terzi".to_string(), - name: "espanso".to_string(), - }); + assert_eq!( + extract_github_url_parts("https://github.com/federico-terzi/espanso").unwrap(), + GitHubParts { + author: "federico-terzi".to_string(), + name: "espanso".to_string(), + } + ); - assert_eq!(extract_github_url_parts("https://github.com/federico-terzi/espanso.git").unwrap(), GitHubParts { - author: "federico-terzi".to_string(), - name: "espanso".to_string(), - }); + assert_eq!( + extract_github_url_parts("https://github.com/federico-terzi/espanso.git").unwrap(), + GitHubParts { + author: "federico-terzi".to_string(), + name: "espanso".to_string(), + } + ); - assert_eq!(extract_github_url_parts("git@github.com:federico-terzi/espanso.git").unwrap(), GitHubParts { - author: "federico-terzi".to_string(), - name: "espanso".to_string(), - }); + assert_eq!( + extract_github_url_parts("git@github.com:federico-terzi/espanso.git").unwrap(), + GitHubParts { + author: "federico-terzi".to_string(), + name: "espanso".to_string(), + } + ); - assert!(extract_github_url_parts("https://gitlab.com/federicoterzi/espanso-test-package/").is_none()); + assert!( + extract_github_url_parts("https://gitlab.com/federicoterzi/espanso-test-package/").is_none() + ); } } diff --git a/espanso-package/src/util/gitlab.rs b/espanso-package/src/util/gitlab.rs index 97dd960..dbe15b7 100644 --- a/espanso-package/src/util/gitlab.rs +++ b/espanso-package/src/util/gitlab.rs @@ -17,13 +17,15 @@ * along with espanso. If not, see . */ +use anyhow::Result; use lazy_static::lazy_static; use regex::Regex; -use anyhow::Result; use reqwest::StatusCode; lazy_static! { - static ref GITLAB_REGEX: Regex = Regex::new(r"(https://gitlab.com/|git@gitlab.com:)(?P.*?)/(?P.*?)(/|\.|$)").unwrap(); + static ref GITLAB_REGEX: Regex = + Regex::new(r"(https://gitlab.com/|git@gitlab.com:)(?P.*?)/(?P.*?)(/|\.|$)") + .unwrap(); } #[derive(Debug, PartialEq)] @@ -36,7 +38,7 @@ pub fn extract_gitlab_url_parts(url: &str) -> Option { let captures = GITLAB_REGEX.captures(url)?; let author = captures.name("author")?; let name = captures.name("name")?; - + Some(GitLabParts { author: author.as_str().to_string(), name: name.as_str().to_string(), @@ -49,14 +51,17 @@ pub struct ResolvedRepoScheme { pub branch: String, } -pub fn resolve_repo_scheme(parts: GitLabParts, force_branch: Option<&str>) -> Result> { +pub fn resolve_repo_scheme( + parts: GitLabParts, + force_branch: Option<&str>, +) -> Result> { if let Some(force_branch) = force_branch { if check_repo_with_branch(&parts, force_branch)? { return Ok(Some(ResolvedRepoScheme { author: parts.author, name: parts.name, branch: force_branch.to_string(), - })) + })); } } else { if check_repo_with_branch(&parts, "main")? { @@ -89,7 +94,10 @@ pub fn check_repo_with_branch(parts: &GitLabParts, branch: &str) -> Result } fn generate_gitlab_download_url(parts: &GitLabParts, branch: &str) -> String { - format!("https://gitlab.com/{}/{}/-/archive/{}/{}-{}.zip", parts.author, parts.name, branch, parts.name, branch) + format!( + "https://gitlab.com/{}/{}/-/archive/{}/{}-{}.zip", + parts.author, parts.name, branch, parts.name, branch + ) } #[cfg(test)] @@ -98,21 +106,33 @@ mod tests { #[test] fn test_extract_gitlab_url_parts() { - assert_eq!(extract_gitlab_url_parts("https://gitlab.com/federicoterzi/espanso-test-package/").unwrap(), GitLabParts { - author: "federicoterzi".to_string(), - name: "espanso-test-package".to_string(), - }); + assert_eq!( + extract_gitlab_url_parts("https://gitlab.com/federicoterzi/espanso-test-package/").unwrap(), + GitLabParts { + author: "federicoterzi".to_string(), + name: "espanso-test-package".to_string(), + } + ); - assert_eq!(extract_gitlab_url_parts("git@gitlab.com:federicoterzi/espanso-test-package.git").unwrap(), GitLabParts { - author: "federicoterzi".to_string(), - name: "espanso-test-package".to_string(), - }); + assert_eq!( + extract_gitlab_url_parts("git@gitlab.com:federicoterzi/espanso-test-package.git").unwrap(), + GitLabParts { + author: "federicoterzi".to_string(), + name: "espanso-test-package".to_string(), + } + ); - assert_eq!(extract_gitlab_url_parts("https://gitlab.com/federicoterzi/espanso-test-package.git").unwrap(), GitLabParts { - author: "federicoterzi".to_string(), - name: "espanso-test-package".to_string(), - }); + assert_eq!( + extract_gitlab_url_parts("https://gitlab.com/federicoterzi/espanso-test-package.git") + .unwrap(), + GitLabParts { + author: "federicoterzi".to_string(), + name: "espanso-test-package".to_string(), + } + ); - assert!(extract_gitlab_url_parts("https://github.com/federicoterzi/espanso-test-package/").is_none()); + assert!( + extract_gitlab_url_parts("https://github.com/federicoterzi/espanso-test-package/").is_none() + ); } } diff --git a/espanso-package/src/util/mod.rs b/espanso-package/src/util/mod.rs index 0079cd1..157861e 100644 --- a/espanso-package/src/util/mod.rs +++ b/espanso-package/src/util/mod.rs @@ -20,4 +20,4 @@ pub(crate) mod download; pub(crate) mod git; pub(crate) mod github; -pub(crate) mod gitlab; \ No newline at end of file +pub(crate) mod gitlab;