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
use super::Connector;
use crate::common::WindowsPipeTransport;
use async_trait::async_trait;
use std::ffi::OsString;
use std::io;

/// Implementation of [`Connector`] to support connecting via a Windows named pipe.
pub struct WindowsPipeConnector {
    addr: OsString,
    pub(crate) local: bool,
}

impl WindowsPipeConnector {
    /// Creates a new connector for a non-local pipe using the given `addr`.
    pub fn new(addr: impl Into<OsString>) -> Self {
        Self {
            addr: addr.into(),
            local: false,
        }
    }

    /// Creates a new connector for a local pipe using the given `name`.
    pub fn local(name: impl Into<OsString>) -> Self {
        Self {
            addr: name.into(),
            local: true,
        }
    }
}

impl<T: Into<OsString>> From<T> for WindowsPipeConnector {
    fn from(addr: T) -> Self {
        Self::new(addr)
    }
}

#[async_trait]
impl Connector for WindowsPipeConnector {
    type Transport = WindowsPipeTransport;

    async fn connect(self) -> io::Result<Self::Transport> {
        if self.local {
            let mut full_addr = OsString::from(r"\\.\pipe\");
            full_addr.push(self.addr);
            WindowsPipeTransport::connect(full_addr).await
        } else {
            WindowsPipeTransport::connect(self.addr).await
        }
    }
}