Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clipboard: add error handling based on clipboard::Error #1970

Merged
merged 4 commits into from
Jul 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions src/cmd/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,49 @@ 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(
"The clipboard may not be supported for the current environment.".to_string(),
),
arboard::Error::ConversionFailure => CliError::Other(

Check failure

Code scanning / clippy

mismatched types Error

mismatched types
"The content that was about the be transferred to/from the clipboard could not be \
converted to the appropriate format."
.to_string(),
),
arboard::Error::ClipboardOccupied => CliError::Other(

Check failure

Code scanning / clippy

mismatched types Error

mismatched types
"The clipboard was unaccessible when attempting to access/use it. It may have \
been held by another process/thread."
.to_string(),
),
arboard::Error::ContentNotAvailable => CliError::Other(

Check failure

Code scanning / clippy

mismatched types Error

mismatched types
"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."
.to_string(),
),
arboard::Error::Unknown { description: _ } => CliError::Other(
"An unknown error occurred while attempting to use the clipboard.".to_string(),
),
_ => CliError::Other(
"An unexpected error occurred while using the clipboard.".to_string(),
),
}
}
}

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
Loading