summaryrefslogtreecommitdiffstats
path: root/vendor/gix-transport/src/client/async_io/connect.rs
blob: fe2a5808ebe9fbb168cdadda4cfcebe210b90c7b (plain)
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
pub use crate::client::non_io_types::connect::{Error, Options};

#[cfg(any(feature = "async-std"))]
pub(crate) mod function {
    use std::convert::TryInto;

    use crate::client::{git, non_io_types::connect::Error};

    /// A general purpose connector connecting to a repository identified by the given `url`.
    ///
    /// This includes connections to
    /// [git daemons][crate::client::git::connect()] only at the moment.
    ///
    /// Use `options` to further control specifics of the transport resulting from the connection.
    pub async fn connect<Url, E>(
        url: Url,
        options: super::Options,
    ) -> Result<Box<dyn crate::client::Transport + Send>, Error>
    where
        Url: TryInto<gix_url::Url, Error = E>,
        gix_url::parse::Error: From<E>,
    {
        let mut url = url.try_into().map_err(gix_url::parse::Error::from)?;
        Ok(match url.scheme {
            gix_url::Scheme::Git => {
                if url.user().is_some() {
                    return Err(Error::UnsupportedUrlTokens {
                        url: url.to_bstring(),
                        scheme: url.scheme,
                    });
                }
                let path = std::mem::take(&mut url.path);
                Box::new(
                    git::Connection::new_tcp(
                        url.host().expect("host is present in url"),
                        url.port,
                        path,
                        options.version,
                    )
                    .await
                    .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?,
                )
            }
            scheme => return Err(Error::UnsupportedScheme(scheme)),
        })
    }
}