1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use std::io;
use std::sync::Arc;

use async_io::Async;
use ssh2::{Agent, PublicKey};

pub struct AsyncAgent<S> {
    inner: Agent,
    async_io: Arc<Async<S>>,
}

impl<S> AsyncAgent<S> {
    pub(crate) fn from_parts(inner: Agent, async_io: Arc<Async<S>>) -> Self {
        Self { inner, async_io }
    }
}

impl<S> AsyncAgent<S> {
    pub async fn connect(&mut self) -> io::Result<()> {
        let inner = &mut self.inner;

        self.async_io
            .write_with(|_| inner.connect().map_err(|err| err.into()))
            .await
    }

    pub async fn disconnect(&mut self) -> io::Result<()> {
        let inner = &mut self.inner;

        self.async_io
            .write_with(|_| inner.disconnect().map_err(|err| err.into()))
            .await
    }

    pub async fn list_identities(&mut self) -> io::Result<()> {
        let inner = &mut self.inner;

        self.async_io
            .write_with(|_| inner.list_identities().map_err(|err| err.into()))
            .await
    }

    pub fn identities(&self) -> io::Result<Vec<PublicKey>> {
        self.inner.identities().map_err(|err| err.into())
    }

    pub async fn userauth(&self, username: &str, identity: &PublicKey) -> io::Result<()> {
        let inner = &self.inner;

        self.async_io
            .write_with(|_| inner.userauth(username, identity).map_err(|err| err.into()))
            .await
    }
}