From 625f0343cbdc5a7bf11b649b98e239d9dcf53f6e Mon Sep 17 00:00:00 2001 From: sigoden Date: Sun, 5 Mar 2023 16:45:42 +0800 Subject: chore: refactor error encapsulation --- src/utils.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/utils.rs') diff --git a/src/utils.rs b/src/utils.rs index b248d92..5bc1237 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -9,14 +9,15 @@ pub fn dump(text: T, newlines: usize) { } pub fn copy(src: &str) -> Result<()> { - let mut ctx = ClipboardContext::new().map_err(|err| anyhow!("{err}"))?; - ctx.set_contents(src.to_string()) - .map_err(|err| anyhow!("{err}")) + ClipboardContext::new() + .and_then(|mut ctx| ctx.set_contents(src.to_string())) + .map_err(|err| anyhow!("Failed to copy, {err}")) } pub fn paste() -> Result { - let mut ctx = ClipboardContext::new().map_err(|err| anyhow!("{err}"))?; - ctx.get_contents().map_err(|err| anyhow!("{err}")) + ClipboardContext::new() + .and_then(|mut ctx| ctx.get_contents()) + .map_err(|err| anyhow!("Failed to paste, {err}")) } pub fn now() -> String { -- cgit v1.2.3