diff --git a/crates/arti-client/examples/hyper.rs b/crates/arti-client/examples/hyper.rs index c548503b8..598fd4144 100644 --- a/crates/arti-client/examples/hyper.rs +++ b/crates/arti-client/examples/hyper.rs @@ -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(); diff --git a/crates/arti-client/examples/readme.rs b/crates/arti-client/examples/readme.rs index 361a06e0a..bab40a3fd 100644 --- a/crates/arti-client/examples/readme.rs +++ b/crates/arti-client/examples/readme.rs @@ -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()?; diff --git a/crates/arti-client/src/config.rs b/crates/arti-client/src/config.rs index 23ff2469e..4d9a2042d 100644 --- a/crates/arti-client/src/config.rs +++ b/crates/arti-client/src/config.rs @@ -156,7 +156,7 @@ impl From 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::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(state_dir: P, cache_dir: Q) -> Result - where - P: AsRef, - Q: AsRef, - { - 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 { 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(state_dir: P, cache_dir: Q) -> Self + where + P: AsRef, + Q: AsRef, + { + 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()); } } diff --git a/crates/arti-client/src/lib.rs b/crates/arti-client/src/lib.rs index ef1a6035b..98043b298 100644 --- a/crates/arti-client/src/lib.rs +++ b/crates/arti-client/src/lib.rs @@ -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()?; diff --git a/crates/arti-config/src/options.rs b/crates/arti-config/src/options.rs index b7f67d8a2..0449ce505 100644 --- a/crates/arti-config/src/options.rs +++ b/crates/arti-config/src/options.rs @@ -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); } diff --git a/crates/tor-config/src/path.rs b/crates/tor-config/src/path.rs index d02c27c19..589b4dac1 100644 --- a/crates/tor-config/src/path.rs +++ b/crates/tor-config/src/path.rs @@ -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> = Lazy::new(|| ProjectDirs::from("org", "torproject", "Arti"));