chore: change `String` to `&str`

This commit is contained in:
MedzikUser 2022-05-18 20:52:47 +02:00
parent 6f9559c6d8
commit f4e0044678
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
8 changed files with 42 additions and 33 deletions

View File

@ -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
<!-- next-url -->
[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

View File

@ -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();
}

View File

@ -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

View File

@ -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);
}

View File

@ -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::<Utc>::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()
);
}

View File

@ -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");
}

View File

@ -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<dyn Error + Send + Sync>> {
// get discord webhook uri from config
let url = toml::parse().discord_webhook.uri;

View File

@ -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,