42 lines
1.1 KiB
Rust
42 lines
1.1 KiB
Rust
extern crate clipboard;
|
|
|
|
use actix_web::{get, post, web, HttpResponse, Responder, Result};
|
|
use serde::Deserialize;
|
|
use crate::api::dto::ClipboardRequest::Body;
|
|
|
|
use clipboard::ClipboardProvider;
|
|
use clipboard::ClipboardContext;
|
|
use regex::Regex;
|
|
|
|
#[get("/")]
|
|
pub async fn hello() -> impl Responder {
|
|
HttpResponse::Ok().body("Running!")
|
|
}
|
|
|
|
#[post("/cb")]
|
|
pub async fn to_clipboard(body: web::Json<Body>) -> Result<String> {
|
|
let text = body.0.text;
|
|
let digits = get_digits(text.clone());
|
|
match digits {
|
|
None => Ok(format!("digits: none")),
|
|
Some(code) => {
|
|
put_in_clipboard(code.clone());
|
|
Ok(format!("digits: {}", code))
|
|
}
|
|
}
|
|
}
|
|
|
|
fn get_digits(text: String) -> Option<String> {
|
|
let re = Regex::new(r"^(?P<digits>\d+).*").unwrap();
|
|
let Some(result) = re.captures(text.as_str()) else {
|
|
println!("no digits in: {}", text);
|
|
return None
|
|
};
|
|
Some(String::from(&result["digits"]))
|
|
}
|
|
|
|
fn put_in_clipboard(text: String) {
|
|
let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
|
|
ctx.set_contents(text.clone().to_owned()).unwrap();
|
|
println!("digits: {}", text.clone());
|
|
} |