gitoxide_core/
net.rs

1use std::str::FromStr;
2
3#[cfg(feature = "async-client")]
4use gix::protocol::transport::client::async_io as io_mode;
5#[cfg(feature = "blocking-client")]
6use gix::protocol::transport::client::blocking_io as io_mode;
7
8#[derive(Default, Clone, Eq, PartialEq, Debug)]
9pub enum Protocol {
10    V1,
11    #[default]
12    V2,
13}
14
15impl FromStr for Protocol {
16    type Err = String;
17
18    fn from_str(s: &str) -> Result<Self, Self::Err> {
19        Ok(match s {
20            "1" => Protocol::V1,
21            "2" => Protocol::V2,
22            _ => return Err(format!("Unsupported protocol version '{s}', choose '1' or '2'")),
23        })
24    }
25}
26
27#[cfg(any(feature = "blocking-client", feature = "async-client"))]
28mod impls {
29    use gix::protocol::transport;
30
31    use super::Protocol;
32
33    impl From<Protocol> for transport::Protocol {
34        fn from(v: Protocol) -> Self {
35            match v {
36                Protocol::V1 => transport::Protocol::V1,
37                Protocol::V2 => transport::Protocol::V2,
38            }
39        }
40    }
41}
42
43#[cfg(any(feature = "async-client", feature = "blocking-client"))]
44#[gix::protocol::maybe_async::maybe_async]
45pub async fn connect<Url, E>(
46    url: Url,
47    options: io_mode::connect::Options,
48) -> Result<gix::protocol::SendFlushOnDrop<Box<dyn io_mode::Transport + Send>>, io_mode::connect::Error>
49where
50    Url: TryInto<gix::url::Url, Error = E>,
51    gix::url::parse::Error: From<E>,
52{
53    Ok(gix::protocol::SendFlushOnDrop::new(
54        io_mode::connect::connect(url, options).await?,
55        false,
56    ))
57}