Skip to main content

libassuan/
lib.rs

1use std::{
2    fs::{File, OpenOptions},
3    os::fd::{AsRawFd, FromRawFd, RawFd},
4};
5
6mod error;
7pub mod handler;
8
9// pub type AssuanHandler = fn(&mut AssuanContext, String);
10
11// pub struct AssuanCmd {
12//     pub name: &'static str,
13//     pub handler: AssuanHandler,
14//     pub help: Option<&'static str>,
15// }
16
17// impl AssuanCmd {
18//     pub fn from_vec_tuples(items: Vec<(&'static str, AssuanHandler)>) -> Vec<Self> {
19//         items.into_iter().map(|v| v.into()).collect::<Vec<Self>>()
20//     }
21// }
22
23// impl From<(&'static str, AssuanHandler)> for AssuanCmd {
24//     fn from(value: (&'static str, AssuanHandler)) -> Self {
25//         AssuanCmd {
26//             name: value.0,
27//             handler: value.1,
28//             help: None,
29//         }
30//     }
31// }
32
33pub(crate) struct Inbound {
34    fd: File,
35}
36
37pub(crate) struct Outbound {
38    fd: File,
39}
40
41pub struct AssuanContext {
42    pub(crate) inbound: Inbound,
43    pub(crate) outbound: Outbound,
44
45    pub commands: Vec<String>,
46    pub options: Vec<String>,
47}
48
49impl AssuanContext {
50    pub fn new(fd_in: impl AsRawFd, fd_out: impl AsRawFd) -> Self {
51        logc();
52        let fd_in = unsafe { File::from_raw_fd(fd_in.as_raw_fd()) };
53        let fd_out = unsafe { File::from_raw_fd(fd_out.as_raw_fd()) };
54
55        Self {
56            inbound: Inbound { fd: fd_in },
57            outbound: Outbound { fd: fd_out },
58            commands: vec![],
59            options: vec![],
60        }
61    }
62
63    pub fn register_commands(mut self, commands: &[&str]) -> Self {
64        self.commands
65            .append(&mut commands.iter().map(|v| v.to_string()).collect());
66        self
67    }
68
69    pub fn register_options(mut self, options: &[&str]) -> Self {
70        self.options
71            .append(&mut options.iter().map(|v| v.to_string()).collect());
72        self
73    }
74}
75
76pub fn logc() {
77    use std::io::Write;
78
79    // std::fs::OpenOptions::new()
80    //     .write(true)
81    //     .truncate(true)
82    //     // .append(true)
83    //     .open("/home/rin/git/pinentry-rs/log.txt")
84    //     .unwrap()
85    //     .write_all("".as_bytes())
86    //     .ok();
87}
88
89pub fn logw(line: &str) {
90    use std::io::Write;
91
92    // std::fs::OpenOptions::new()
93    //     .append(true)
94    //     .open("/home/rin/git/pinentry-rs/log.txt")
95    //     .unwrap()
96    //     .write_all(format!("{line}\n").as_bytes())
97    //     .ok();
98}