Skip to content

Commit

Permalink
clipboard: add error handling based on clipboard::Error
Browse files Browse the repository at this point in the history
  • Loading branch information
rzmk committed Jul 14, 2024
1 parent a7fc6db commit 0ee2de9
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/cmd/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,42 @@ use std::io::Read;
use arboard::Clipboard;
use serde::Deserialize;

use crate::{util, CliResult};
use crate::{util, CliError, CliResult};

#[allow(dead_code)]
#[derive(Deserialize)]
struct Args {
flag_save: bool,
}

impl From<arboard::Error> for CliError {
fn from(err: arboard::Error) -> Self {
match err {
arboard::Error::ClipboardNotSupported => {
CliError::Other(format!("The clipboard may not be supported for the current environment."))

Check warning

Code scanning / clippy

useless use of format! Warning

useless use of format!
},
arboard::Error::ConversionFailure => {
CliError::Other(format!("The content that was about the be transferred to/from the clipboard could not be converted to the appropriate format."))

Check warning

Code scanning / clippy

useless use of format! Warning

useless use of format!
},
arboard::Error::ClipboardOccupied => {
CliError::Other(format!("The clipboard was unaccessible when attempting to access/use it. It may have been held by another process/thread."))

Check warning

Code scanning / clippy

useless use of format! Warning

useless use of format!
},
arboard::Error::ContentNotAvailable => {
CliError::Other(format!("The clipboard contents were not available in the requested format. This could either be due to the clipboard being empty or the clipboard contents having an incompatible format to the requested one."))

Check warning

Code scanning / clippy

useless use of format! Warning

useless use of format!
},
arboard::Error::Unknown { description: _ } => {
CliError::Other(format!("An unknown error occurred while attempting to use the clipboard."))

Check warning

Code scanning / clippy

useless use of format! Warning

useless use of format!
},
_ => {
CliError::Other(format!("An unexpected error occurred while using the clipboard."))

Check warning

Code scanning / clippy

useless use of format! Warning

useless use of format!
}
}
}
}

pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;
let mut clipboard = Clipboard::new().unwrap();
let mut clipboard = Clipboard::new()?;
if args.flag_save {
let mut buffer = String::new();
std::io::stdin().read_to_string(&mut buffer)?;
Expand Down

0 comments on commit 0ee2de9

Please sign in to comment.