Use rust implment a copyclipbord CLI

[package]
name = "copyclip"
version = "0.1.0"
edition = "2021"


[dependencies]
atty = "0.2.14"
copypasta = "0.8.2"
use atty::Stream;
use copypasta::{ClipboardContext, ClipboardProvider};
use std::env::args;
use std::fs;
use std::io::{stdin, Read};
use std::path::PathBuf;

fn write_to_clipboard<T: AsRef<str>>(content: T) {
    let mut ctx: ClipboardContext = ClipboardContext::new().unwrap();
    ctx.set_contents(content.as_ref().to_owned()).unwrap();
}

fn print_to_terminal() {
    let mut ctx: ClipboardContext = ClipboardContext::new().unwrap();
    if let Ok(r) = ctx.get_contents(){
        if !r.is_empty(){
            println!("{}",r);
        }
    }
}

fn main() {
    // Handle `copyclip ...` stdin cases
    // echo 'foo' | copyclip
    if atty::isnt(Stream::Stdin) {
        let mut buffer = String::new();
        stdin().read_to_string(&mut buffer).unwrap();
        write_to_clipboard(buffer.trim());
        return;
    }
    // Handle `copyclip ...` stdout cases
    match args().len() {
        // Case of `copyclip`
        1 => return print_to_terminal(),
        // Case of `copyclip my-file`
        // Case of `copyclip my-file`
        2 => {
            if let Some(path) = args().nth(1) {
                let abs_path = PathBuf::from(path)
                    .canonicalize()
                    .expect("Failed to canonicalize path");
                let res = fs::read_to_string(abs_path).expect("File does not exist");
                return write_to_clipboard(res);
            }
        }
        _ => panic!("unexpected number of args"),
    }
}