B2MSSEJB4GIBFA6F2HRMK7FXUHJBZIZFK6NOHY7YZUSHRB53URGQC condense_wildcard_suffixes = trueformat_strings = trueimports_indent = "Block"imports_layout = "Vertical"merge_imports = truenormalize_comments = truereorder_imports = trueuse_try_shorthand = truewrap_comments = true
[[package]]name = "anyhow"version = "1.0.32"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "6b602bfe940d21c130f3895acd65221e8a61270debe89d628b9cb4e3ccb8569b"[[package]]name = "autocfg"version = "1.0.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"[[package]]name = "cfg-if"version = "0.1.10"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"[[package]]name = "chrono"version = "0.4.15"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "942f72db697d8767c22d46a598e01f2d3b475501ea43d0db4f16d90259182d0b"dependencies = ["num-integer","num-traits","serde","time",][[package]]name = "getrandom"version = "0.1.15"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6"dependencies = ["cfg-if","libc","wasi 0.9.0+wasi-snapshot-preview1",]
dependencies = ["anyhow","chrono","thiserror","uuid","xdg",][[package]]name = "libc"version = "0.2.77"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "f2f96b10ec2560088a8e76961b00d47107b3a625fecb76dedb29ee7ccbf98235"[[package]]name = "num-integer"version = "0.1.43"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b"dependencies = ["autocfg","num-traits",][[package]]name = "num-traits"version = "0.2.12"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611"dependencies = ["autocfg",][[package]]name = "ppv-lite86"version = "0.2.9"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "c36fa947111f5c62a733b652544dd0016a43ce89619538a8ef92724a6f501a20"[[package]]name = "proc-macro2"version = "1.0.21"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "36e28516df94f3dd551a587da5357459d9b36d945a7c37c3557928c1c2ff2a2c"dependencies = ["unicode-xid",][[package]]name = "quote"version = "1.0.7"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"dependencies = ["proc-macro2",][[package]]name = "rand"version = "0.7.3"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"dependencies = ["getrandom","libc","rand_chacha","rand_core","rand_hc",][[package]]name = "rand_chacha"version = "0.2.2"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"dependencies = ["ppv-lite86","rand_core",]
[[package]]name = "rand_core"version = "0.5.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"dependencies = ["getrandom",][[package]]name = "rand_hc"version = "0.2.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"dependencies = ["rand_core",][[package]]name = "serde"version = "1.0.116"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "96fe57af81d28386a513cbc6858332abc6117cfdb5999647c6444b8f43a370a5"dependencies = ["serde_derive",][[package]]name = "serde_derive"version = "1.0.116"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "f630a6370fd8e457873b4bd2ffdae75408bc291ba72be773772a4c2a065d9ae8"dependencies = ["proc-macro2","quote","syn",][[package]]name = "syn"version = "1.0.41"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "6690e3e9f692504b941dc6c3b188fd28df054f7fb8469ab40680df52fdcc842b"dependencies = ["proc-macro2","quote","unicode-xid",][[package]]name = "thiserror"version = "1.0.20"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "7dfdd070ccd8ccb78f4ad66bf1982dc37f620ef696c6b5028fe2ed83dd3d0d08"dependencies = ["thiserror-impl",][[package]]name = "thiserror-impl"version = "1.0.20"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "bd80fc12f73063ac132ac92aceea36734f04a1d93c1240c6944e23a3b8841793"dependencies = ["proc-macro2","quote","syn",][[package]]name = "time"version = "0.1.44"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"dependencies = ["libc","wasi 0.10.0+wasi-snapshot-preview1","winapi",][[package]]name = "unicode-xid"version = "0.2.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"[[package]]name = "uuid"version = "0.8.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11"dependencies = ["rand","serde",][[package]]name = "wasi"version = "0.9.0+wasi-snapshot-preview1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"[[package]]name = "wasi"version = "0.10.0+wasi-snapshot-preview1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"[[package]]name = "winapi"version = "0.3.9"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"dependencies = ["winapi-i686-pc-windows-gnu","winapi-x86_64-pc-windows-gnu",][[package]]name = "winapi-i686-pc-windows-gnu"version = "0.4.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"[[package]]name = "winapi-x86_64-pc-windows-gnu"version = "0.4.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"[[package]]name = "xdg"version = "2.2.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "d089681aa106a86fade1b0128fb5daf07d5867a509ab036d99988dec80429a57"
chrono = { version = "0.4", features = ["serde"] }uuid = { version = "0.8", features = ["serde", "v4"] }anyhow = "1.0.32"thiserror = "1.0.20"xdg = "2.2.0"
use chrono::{DateTime,Utc,};use std::{env,path::PathBuf,};use thiserror::Error;use uuid::Uuid;#[derive(Debug)]pub struct Entry {pub command: String,pub pwd: PathBuf,pub result: Option<usize>,pub session_id: Uuid,pub time_end: Option<DateTime<Utc>>,pub time_start: DateTime<Utc>,pub user: String,}#[derive(Error, Debug)]pub enum Error {#[error("can not get current directory: {0}")]GetCurrentDir(std::io::Error),#[error("can not get current user: {0}")]GetUser(env::VarError),#[error("invalid session id in environment variable: {0}")]InvalidSessionIDEnvVar(env::VarError),#[error("invalid session id: {0}")]InvalidSessionID(uuid::Error),#[error("session id is missing")]MissingSessionID,#[error("invalid result: {0}")]InvalidResult(std::num::ParseIntError),}impl Entry {pub fn from_env() -> Result<Self, Error> {let command = env::args().into_iter().skip(2).next().unwrap_or_default();let pwd = env::current_dir().map_err(Error::GetCurrentDir)?;let time_start = Utc::now();let time_end = None;let user = env::var("USER").map_err(Error::GetUser)?;let result = env::var("HISTDB_RS_RETVAL").ok().map(|s| s.parse().map_err(Error::InvalidResult)).transpose()?;let session_id = match env::var("HISTDB_RS_SESSION_ID") {Err(err) => match err {env::VarError::NotPresent => Err(Error::MissingSessionID),_ => Err(Error::InvalidSessionIDEnvVar(err)),},Ok(s) => Uuid::parse_str(&s).map_err(Error::InvalidSessionID),}?;Ok(Self {command,pwd,result,session_id,time_end,time_start,user,})}}
mod entry;mod server;use anyhow::{anyhow,Result,};use entry::Entry;use server::Server;use std::os::unix::net::UnixDatagram;use uuid::Uuid;fn main() -> Result<()> {let command = std::env::args().into_iter().skip(1).next().unwrap_or_default();match command.as_str() {"zshaddhistory" => {eprintln!("zshaddhistory");let entry = Entry::from_env().unwrap();dbg!(&entry);let xdg_dirs = xdg::BaseDirectories::with_prefix("histdb-rs").unwrap();let socket_path = xdg_dirs.find_runtime_file("socket").unwrap();dbg!(&socket_path);let socket = UnixDatagram::unbound().unwrap();socket.connect(socket_path).unwrap();socket.send(format!("{:?}", entry).as_bytes()).unwrap();Ok(())}"precmd" => {println!("precmd");let entry = Entry::from_env().unwrap();dbg!(&entry);Ok(())}"session_id" => {eprintln!("session_id");println!("{}", Uuid::new_v4());Ok(())}"server" => {eprintln!("server");Server::start()?;Ok(())}_ => Err(anyhow!("unkown {}", command)),}}
use std::os::unix::net::UnixDatagram;use thiserror::Error;const BUFFER_SIZE: usize = 65_527;pub struct Server {}#[derive(Error, Debug)]pub enum Error {}impl Server {pub fn start() -> Result<(), Error> {let xdg_dirs = xdg::BaseDirectories::with_prefix("histdb-rs").unwrap();let socket_path = xdg_dirs.place_runtime_file("socket").unwrap();dbg!(&socket_path);let socket = UnixDatagram::bind(&socket_path).unwrap();loop {if let Err(err) = Self::receive(&socket) {eprintln!("{}", err)}}// std::fs::remove_file(&socket_path).unwrap();}fn receive(socket: &UnixDatagram) -> Result<(), Error> {let mut buffer = [0u8; BUFFER_SIZE];let (written, _) = socket.recv_from(&mut buffer).unwrap();dbg!(String::from_utf8_lossy(&buffer[0..written]));Ok(())}}