Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Nov 11, 2022
1 parent a44c9ea commit e3a24e6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,79 +34,7 @@ impl crate::Repository {
}

#[cfg(any(feature = "blocking-network-client", feature = "async-network-client"))]
mod transport {
use crate::bstr::{BStr, ByteVec};
use git_transport::client::http;
use std::any::Any;
use std::convert::{TryFrom, TryInto};

impl crate::Repository {
/// Produce configuration suitable for `url`, as differentiated by its protocol/scheme, to be passed to a transport instance via
/// [configure()][git_transport::client::TransportWithoutIO::configure()].
/// `None` is returned if there is no known configuration.
///
/// Note that the caller may cast the instance themselves to modify it before passing it on.
pub fn transport_config<'a>(
&self,
url: impl Into<&'a BStr>,
) -> Result<Option<Box<dyn Any>>, crate::config::transport::Error> {
let url = git_url::parse(url.into())?;
use git_url::Scheme::*;

match &url.scheme {
Http | Https => {
let mut opts = http::Options::default();
let config = &self.config.resolved;
let mut trusted_only = self.filter_config_section();
opts.extra_headers = config
.strings_filter("http", None, "extraHeader", &mut trusted_only)
.unwrap_or_default()
.into_iter()
.filter_map(|v| Vec::from(v.into_owned()).into_string().ok())
.collect();

if let Some(follow_redirects) =
config.string_filter("http", None, "followRedirects", &mut trusted_only)
{
opts.follow_redirects = if follow_redirects.as_ref() == "initial" {
http::options::FollowRedirects::Initial
} else if git_config::Boolean::try_from(follow_redirects)
.map_err(|err| crate::config::transport::Error::ConfigValue {
source: err,
key: "http.followRedirects",
})?
.0
{
http::options::FollowRedirects::All
} else {
http::options::FollowRedirects::None
};
}

opts.low_speed_time_seconds = {
let integer = config
.integer_filter("http", None, "lowSpeedTime", &mut trusted_only)
.transpose()
.map_err(|err| crate::config::transport::Error::ConfigValue {
source: err,
key: "http.lowSpeedTime",
})?
.unwrap_or_default();
integer
.try_into()
.map_err(|_| crate::config::transport::Error::InvalidInteger {
actual: integer,
key: "http.lowSpeedLimit",
kind: "u64",
})?
};
todo!();
}
File | Git | Ssh | Ext(_) => Ok(None),
}
}
}
}
mod transport;

mod remote {
use std::{borrow::Cow, collections::BTreeSet};
Expand Down
70 changes: 70 additions & 0 deletions git-repository/src/repository/config/transport.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use crate::bstr::{BStr, ByteVec};
use git_transport::client::http;
use std::any::Any;
use std::convert::{TryFrom, TryInto};

impl crate::Repository {
/// Produce configuration suitable for `url`, as differentiated by its protocol/scheme, to be passed to a transport instance via
/// [configure()][git_transport::client::TransportWithoutIO::configure()].
/// `None` is returned if there is no known configuration.
///
/// Note that the caller may cast the instance themselves to modify it before passing it on.
pub fn transport_config<'a>(
&self,
url: impl Into<&'a BStr>,
) -> Result<Option<Box<dyn Any>>, crate::config::transport::Error> {
let url = git_url::parse(url.into())?;
use git_url::Scheme::*;

match &url.scheme {
Http | Https => {
let mut opts = http::Options::default();
let config = &self.config.resolved;
let mut trusted_only = self.filter_config_section();
opts.extra_headers = config
.strings_filter("http", None, "extraHeader", &mut trusted_only)
.unwrap_or_default()
.into_iter()
.filter_map(|v| Vec::from(v.into_owned()).into_string().ok())
.collect();

if let Some(follow_redirects) = config.string_filter("http", None, "followRedirects", &mut trusted_only)
{
opts.follow_redirects = if follow_redirects.as_ref() == "initial" {
http::options::FollowRedirects::Initial
} else if git_config::Boolean::try_from(follow_redirects)
.map_err(|err| crate::config::transport::Error::ConfigValue {
source: err,
key: "http.followRedirects",
})?
.0
{
http::options::FollowRedirects::All
} else {
http::options::FollowRedirects::None
};
}

opts.low_speed_time_seconds = {
let integer = config
.integer_filter("http", None, "lowSpeedTime", &mut trusted_only)
.transpose()
.map_err(|err| crate::config::transport::Error::ConfigValue {
source: err,
key: "http.lowSpeedTime",
})?
.unwrap_or_default();
integer
.try_into()
.map_err(|_| crate::config::transport::Error::InvalidInteger {
actual: integer,
key: "http.lowSpeedLimit",
kind: "u64",
})?
};
todo!();
}
File | Git | Ssh | Ext(_) => Ok(None),
}
}
}

0 comments on commit e3a24e6

Please sign in to comment.