Skip to content

Commit

Permalink
chore: change String to &str
Browse files Browse the repository at this point in the history
  • Loading branch information
M3DZIK committed May 18, 2022
1 parent 6f9559c commit f4e0044
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 33 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/cli/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(':') {
Expand Down Expand Up @@ -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();
}
3 changes: 2 additions & 1 deletion src/cli/credits.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/cli/info_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
23 changes: 10 additions & 13 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,42 @@ 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);
let timestamp_str = datetime.format("%Y-%m-%d %H:%M:%S").to_string();

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

Expand Down
16 changes: 8 additions & 8 deletions src/cli/upload_image.rs
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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");
}
Expand Down
4 changes: 2 additions & 2 deletions src/cli/webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 6 additions & 5 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
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,
pub clipboard: ConfigClipboard,
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,
Expand Down

0 comments on commit f4e0044

Please sign in to comment.