Change sane_defaults() and with_directories()

The sane_defaults() call is now the same as you get from a default
builder: by convention, we just call that method Default::default().

The with_directories() constructor makes more sense as a constructor
for the TorClientConfigBuilder than for TorClientConfig.
This commit is contained in:
Nick Mathewson 2021-11-29 14:20:34 -05:00
parent eb861b7edd
commit f107794b74
6 changed files with 34 additions and 41 deletions

View File

@ -133,10 +133,10 @@ async fn main() -> Result<()> {
eprintln!("starting Arti...");
// The client config includes things like where to store persistent Tor network state.
// The "sane defaults" provided are the same as the Arti standalone application, and save data
// The defaults provided are the same as the Arti standalone application, and save data
// to a conventional place depending on operating system (for example, ~/.local/share/arti
// on Linux platforms)
let config = TorClientConfig::sane_defaults()?;
let config = TorClientConfig::default();
// Arti needs an async runtime handle to spawn async tasks.
let rt: TokioRuntimeHandle = tokio_crate::runtime::Handle::current().into();

View File

@ -10,10 +10,10 @@ async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
// The client config includes things like where to store persistent Tor network state.
// The "sane defaults" provided are the same as the Arti standalone application, and save data
// The defaults provided are the same as the Arti standalone application, and save data
// to a conventional place depending on operating system (for example, ~/.local/share/arti
// on Linux platforms)
let config = TorClientConfig::sane_defaults()?;
let config = TorClientConfig::default();
// Arti needs an async runtime handle to spawn async tasks.
let rt = tor_rtcompat::tokio::current_runtime()?;

View File

@ -156,7 +156,7 @@ impl From<StorageConfig> for StorageConfigBuilder {
/// for default directories.)
///
/// Most users will create a TorClientConfig by running
/// [`TorClientConfig::sane_defaults`].
/// [`TorClientConfig::default`].
///
/// If you need to override the locations where Arti stores its information,
/// you can make a TorClientConfig with [`TorClientConfig::with_directories`].
@ -191,41 +191,20 @@ pub struct TorClientConfig {
pub(crate) address_filter: ClientAddrConfig,
}
impl Default for TorClientConfig {
fn default() -> Self {
Self::builder()
.build()
.expect("Could not build TorClientConfig from default configuration.")
}
}
impl TorClientConfig {
/// Return a new TorClientConfigBuilder.
pub fn builder() -> TorClientConfigBuilder {
TorClientConfigBuilder::default()
}
/// Returns a `TorClientConfig` using reasonably sane defaults.
///
/// This gives the same result as the default builder, which
/// uses `ARTI_LOCAL_DATA` and `ARTI_CACHE` for the state and cache
/// directories respectively.
///
/// (On unix, this usually works out to `~/.local/share/arti` and
/// `~/.cache/arti`, depending on your environment. We use the
/// `directories` crate for reasonable defaults on other platforms.)
pub fn sane_defaults() -> Result<Self, ConfigBuildError> {
Self::builder().build()
}
/// Returns a `TorClientConfig` using the specified state and cache directories.
///
/// All other configuration options are set to their defaults.
pub fn with_directories<P, Q>(state_dir: P, cache_dir: Q) -> Result<Self, ConfigBuildError>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
let mut builder = Self::builder();
builder
.storage()
.cache_dir(CfgPath::from_path(cache_dir))
.state_dir(CfgPath::from_path(state_dir));
builder.build()
}
/// Build a DirMgrConfig from this configuration.
pub(crate) fn get_dirmgr_config(&self) -> Result<dir::DirMgrConfig, ConfigBuildError> {
let mut dircfg = dir::DirMgrConfigBuilder::default();
@ -308,6 +287,22 @@ impl TorClientConfigBuilder {
})
}
/// Returns a `TorClientConfigBuilder` using the specified state and cache directories.
///
/// All other configuration options are set to their defaults.
pub fn from_directories<P, Q>(state_dir: P, cache_dir: Q) -> Self
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
let mut builder = Self::default();
builder
.storage()
.cache_dir(CfgPath::from_path(cache_dir))
.state_dir(CfgPath::from_path(state_dir));
builder
}
/// Return a mutable reference to a
/// [`NetworkConfigBuilder`](dir::NetworkConfigBuilder)
/// to use in configuring the underlying Tor network.
@ -409,7 +404,7 @@ mod test {
#[test]
fn defaults() {
let dflt = TorClientConfig::sane_defaults().unwrap();
let dflt = TorClientConfig::default();
let b2 = TorClientConfigBuilder::from(dflt.clone());
let dflt2 = b2.build().unwrap();
assert_eq!(&dflt, &dflt2);
@ -461,6 +456,6 @@ mod test {
let val2 = bld2.build().unwrap();
assert_eq!(val, val2);
assert_ne!(val, TorClientConfig::sane_defaults().unwrap());
assert_ne!(val, TorClientConfig::default());
}
}

View File

@ -57,7 +57,7 @@
//! # async fn main() -> Result<()> {
//! // The client configuration describes how to connect to the Tor network,
//! // and what directories to use for storing persistent state.
//! let config = TorClientConfig::sane_defaults()?;
//! let config = TorClientConfig::default();
//! // Arti needs a handle to an async runtime in order to spawn tasks and use the
//! // network. (See "Multiple runtime support" below.)
//! let rt = tor_rtcompat::tokio::current_runtime()?;

View File

@ -400,7 +400,7 @@ mod test {
// Make sure that the client configuration this gives us is the default one.
let client_config = parsed.tor_client_config().unwrap();
let dflt_client_config = TorClientConfig::sane_defaults().unwrap();
let dflt_client_config = TorClientConfig::default();
assert_eq!(&client_config, &dflt_client_config);
}

View File

@ -23,7 +23,7 @@ use serde::Deserialize;
///
/// These variables are implemented using the `directories` crate, and
/// so should use appropriate system-specific overrides under the
/// hood.
/// hood. For more information, see that crate's documentation.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
#[serde(transparent)]
pub struct CfgPath(PathInner);
@ -140,8 +140,6 @@ impl std::fmt::Display for CfgPath {
#[cfg(feature = "expand-paths")]
fn project_dirs() -> Result<&'static ProjectDirs, CfgPathError> {
/// lazy cell holding the ProjectDirs object.
// Note: this must stay in sync with sane_defaults() in the
// arti-client crate.
static PROJECT_DIRS: Lazy<Option<ProjectDirs>> =
Lazy::new(|| ProjectDirs::from("org", "torproject", "Arti"));