digital code extractor

master
vy.boyko 2023-08-16 13:42:50 +03:00
commit 4eb0830117
9 changed files with 1442 additions and 0 deletions

6
.gitignore vendored 100644
View File

@ -0,0 +1,6 @@
.idea/**
.idea
*.iml
target/**
target

1354
Cargo.lock generated 100644

File diff suppressed because it is too large Load Diff

12
Cargo.toml 100644
View File

@ -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"

3
run.sh 100644
View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
cargo run --package ClipboardListener --bin ClipboardListener

2
src/api.rs 100644
View File

@ -0,0 +1,2 @@
pub mod Api;
pub mod dto;

42
src/api/Api.rs 100644
View File

@ -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());
}

1
src/api/dto.rs 100644
View File

@ -0,0 +1 @@
pub mod ClipboardRequest;

View File

@ -0,0 +1,6 @@
use serde::Deserialize;
#[derive(Deserialize)]
pub struct Body {
pub text: String,
}

16
src/main.rs 100644
View File

@ -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
}