digital code extractor
commit
4eb0830117
|
@ -0,0 +1,6 @@
|
|||
.idea/**
|
||||
.idea
|
||||
*.iml
|
||||
target/**
|
||||
target
|
||||
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,12 @@
|
|||
[package]
|
||||
name = "ClipboardListener"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
actix-web = "4"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
clipboard = "0.5.0"
|
||||
regex = "1.8.4"
|
|
@ -0,0 +1,3 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
cargo run --package ClipboardListener --bin ClipboardListener
|
|
@ -0,0 +1,2 @@
|
|||
pub mod Api;
|
||||
pub mod dto;
|
|
@ -0,0 +1,42 @@
|
|||
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());
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
pub mod ClipboardRequest;
|
|
@ -0,0 +1,6 @@
|
|||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Body {
|
||||
pub text: String,
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
use actix_web::{App, HttpServer};
|
||||
use crate::api::Api;
|
||||
|
||||
pub mod api;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.service(Api::hello)
|
||||
.service(Api::to_clipboard)
|
||||
})
|
||||
.bind(("192.168.3.55", 19099))?
|
||||
.run()
|
||||
.await
|
||||
}
|
Loading…
Reference in New Issue