From f4e004467880b7cb970fb97883b3ef174c47691d Mon Sep 17 00:00:00 2001 From: MedzikUser Date: Wed, 18 May 2022 20:52:47 +0200 Subject: [PATCH] chore: change `String` to `&str` --- CHANGELOG.md | 12 +++++++++++- src/cli/clipboard.rs | 4 ++-- src/cli/credits.rs | 3 ++- src/cli/info_image.rs | 2 +- src/cli/mod.rs | 23 ++++++++++------------- src/cli/upload_image.rs | 16 ++++++++-------- src/cli/webhook.rs | 4 ++-- src/config/mod.rs | 11 ++++++----- 8 files changed, 42 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf0f360..7439874 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +## [0.7.3] - 2022-05-18 +### Library +- add code comments and tests +- change `String` to `&str` in ImgurClient functions + +### Other +- bump deps +- use `anyhow::Result<...>` instead `Result<..., Error>` + ## [0.7.2] - 2022-04-05 ### HotFix - fix upload image from file @@ -106,7 +115,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - upload image -[Unreleased]: https://github.com/MedzikUser/imgurs/compare/v0.7.2...HEAD +[Unreleased]: https://github.com/MedzikUser/imgurs/compare/v0.7.3...HEAD +[0.7.3]: https://github.com/MedzikUser/imgurs/commits/v0.7.3 [0.7.2]: https://github.com/MedzikUser/imgurs/commits/v0.7.2 [0.7.1]: https://github.com/MedzikUser/imgurs/commits/v0.7.1 [0.7.0]: https://github.com/MedzikUser/imgurs/commits/v0.7.0 diff --git a/src/cli/clipboard.rs b/src/cli/clipboard.rs index 46aa09a..c5636ba 100644 --- a/src/cli/clipboard.rs +++ b/src/cli/clipboard.rs @@ -3,7 +3,7 @@ not(any(target_os = "macos", target_os = "android", target_os = "emscripten")) ))] // use xclip (or a similar program that is installed) because the kernel deletes the clipboard after the process ends -pub fn set_clipboard(content: String) { +pub fn set_clipboard(content: &str) { fn is_program_in_path(program: &str) -> bool { if let Ok(path) = std::env::var("PATH") { for p in path.split(':') { @@ -79,7 +79,7 @@ pub fn set_clipboard(content: String) { unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten")) )))] -pub fn set_clipboard(content: String) { +pub fn set_clipboard(content: &str) { let mut clipboard = arboard::Clipboard::new().unwrap(); clipboard.set_text(content).unwrap(); } diff --git a/src/cli/credits.rs b/src/cli/credits.rs index 920c105..cc338c9 100644 --- a/src/cli/credits.rs +++ b/src/cli/credits.rs @@ -1,7 +1,8 @@ +use std::time::{Duration, UNIX_EPOCH}; + use chrono::{prelude::DateTime, Utc}; use colored::Colorize; use imgurs::ImgurClient; -use std::time::{Duration, UNIX_EPOCH}; pub async fn credits(client: ImgurClient) { // get client ratelimit from imgur api diff --git a/src/cli/info_image.rs b/src/cli/info_image.rs index 56d9317..261c58a 100644 --- a/src/cli/info_image.rs +++ b/src/cli/info_image.rs @@ -10,5 +10,5 @@ pub async fn image_info(client: ImgurClient, id: String) { .expect("send request to imfur api"); // print image information from imgur - print_image_info(info); + print_image_info(&info); } diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 729fc75..14b769e 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -15,7 +15,7 @@ use imgurs::ImageInfo; use std::time::{Duration, UNIX_EPOCH}; // print image information from imgur -pub fn print_image_info(i: ImageInfo) { +pub fn print_image_info(i: &ImageInfo) { // format image upload date let d = UNIX_EPOCH + Duration::from_secs(i.data.datetime.try_into().unwrap()); let datetime = DateTime::::from(d); @@ -23,37 +23,34 @@ pub fn print_image_info(i: ImageInfo) { // image title if i.data.title != None { + let title = i.data.title.clone(); + println!( "{} {}", "title".green(), - i.data - .title - .unwrap_or_else(|| "unknown".to_string()) - .magenta() + title.unwrap_or_else(|| "unknown".to_string()).magenta() ); } // image description if i.data.description != None { + let desc = i.data.description.clone(); + println!( "{} {}", "description".green(), - i.data - .description - .unwrap_or_else(|| "unknown".to_string()) - .magenta() + desc.unwrap_or_else(|| "unknown".to_string()).magenta() ); } // image deletehas if i.data.deletehash != None { + let delhash = i.data.deletehash.clone(); + println!( "{} {}", "deletehash".green(), - i.data - .deletehash - .unwrap_or_else(|| "unknown".to_string()) - .magenta() + delhash.unwrap_or_else(|| "unknown".to_string()).magenta() ); } diff --git a/src/cli/upload_image.rs b/src/cli/upload_image.rs index 99fc70e..0d8b0df 100644 --- a/src/cli/upload_image.rs +++ b/src/cli/upload_image.rs @@ -1,10 +1,10 @@ -use super::clipboard::set_clipboard; use imgurs::ImgurClient; use notify_rust::Notification; -use crate::{cli::webhook::send_discord_webhook, config::toml}; - -use super::print_image_info; +use crate::{ + cli::{clipboard::set_clipboard, print_image_info, webhook::send_discord_webhook}, + config::toml, +}; // show notification macro_rules! notify ( @@ -23,7 +23,7 @@ pub async fn upload_image(client: ImgurClient, path: String) { let mut i = client.upload_image(&path).await.unwrap_or_else(|err| { notify!(Notification::new() .summary("Error!") - .body(&format!("Error: {}", &err.to_string())) + .body(&format!("Error: {}", err)) .appname("Imgurs")); // I don't think you can set it to error panic!("send request to imagur api: {}", err) @@ -35,7 +35,7 @@ pub async fn upload_image(client: ImgurClient, path: String) { } // print image information from imgur - print_image_info(i.clone()); + print_image_info(&i); // send notification that the image has been uploaded notify!(Notification::new() @@ -44,12 +44,12 @@ pub async fn upload_image(client: ImgurClient, path: String) { // if enabled copy link to clipboard if config.clipboard.enabled { - set_clipboard(i.data.link.clone()) + set_clipboard(&i.data.link) } // if enabled send embed with link and deletehash to discord (something like logger) if config.discord_webhook.enabled { - send_discord_webhook(i.data.link, i.data.deletehash.unwrap()) + send_discord_webhook(&i.data.link, &i.data.deletehash.unwrap()) .await .expect("send discord webhook"); } diff --git a/src/cli/webhook.rs b/src/cli/webhook.rs index fd03e1f..af2b651 100644 --- a/src/cli/webhook.rs +++ b/src/cli/webhook.rs @@ -5,8 +5,8 @@ use crate::config::toml; // send embed with link and deletehash to discord (something like logger) pub async fn send_discord_webhook( - link: String, - deletehash: String, + link: &str, + deletehash: &str, ) -> Result<(), Box> { // get discord webhook uri from config let url = toml::parse().discord_webhook.uri; diff --git a/src/config/mod.rs b/src/config/mod.rs index 309490e..010574c 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,8 +1,9 @@ +use serde::Serialize; use serde_derive::Deserialize; pub mod toml; -#[derive(Debug, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct Config { pub imgur: ConfigImgur, pub notification: ConfigNotification, @@ -10,23 +11,23 @@ pub struct Config { pub discord_webhook: ConfigDiscordWebhook, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct ConfigImgur { pub id: String, pub image_cdn: String, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct ConfigNotification { pub enabled: bool, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct ConfigClipboard { pub enabled: bool, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct ConfigDiscordWebhook { pub enabled: bool, pub uri: String,