#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg))]
#![doc = include_str!("../README.md")]
#![allow(renamed_and_removed_lints)] #![allow(unknown_lints)] #![warn(missing_docs)]
#![warn(noop_method_call)]
#![warn(unreachable_pub)]
#![warn(clippy::all)]
#![deny(clippy::await_holding_lock)]
#![deny(clippy::cargo_common_metadata)]
#![deny(clippy::cast_lossless)]
#![deny(clippy::checked_conversions)]
#![warn(clippy::cognitive_complexity)]
#![deny(clippy::debug_assert_with_mut_call)]
#![deny(clippy::exhaustive_enums)]
#![deny(clippy::exhaustive_structs)]
#![deny(clippy::expl_impl_clone_on_copy)]
#![deny(clippy::fallible_impl_from)]
#![deny(clippy::implicit_clone)]
#![deny(clippy::large_stack_arrays)]
#![warn(clippy::manual_ok_or)]
#![deny(clippy::missing_docs_in_private_items)]
#![warn(clippy::needless_borrow)]
#![warn(clippy::needless_pass_by_value)]
#![warn(clippy::option_option)]
#![deny(clippy::print_stderr)]
#![deny(clippy::print_stdout)]
#![warn(clippy::rc_buffer)]
#![deny(clippy::ref_option_ref)]
#![warn(clippy::semicolon_if_nothing_returned)]
#![warn(clippy::trait_duplication_in_bounds)]
#![deny(clippy::unchecked_duration_subtraction)]
#![deny(clippy::unnecessary_wraps)]
#![warn(clippy::unseparated_literal_suffix)]
#![deny(clippy::unwrap_used)]
#![allow(clippy::let_unit_value)] #![allow(clippy::uninlined_format_args)]
#![allow(clippy::significant_drop_in_scrutinee)] #![allow(clippy::result_large_err)] #![allow(clippy::needless_raw_string_hashes)] use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
#[cfg(feature = "expand-paths")]
use std::borrow::Cow;
#[cfg(feature = "expand-paths")]
use {
directories::{BaseDirs, ProjectDirs},
once_cell::sync::Lazy,
};
use tor_error::{ErrorKind, HasKind};
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(transparent)]
pub struct CfgPath(PathInner);
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(untagged)]
enum PathInner {
Literal(LiteralPath),
Shell(String),
}
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
struct LiteralPath {
literal: PathBuf,
}
#[derive(thiserror::Error, Debug, Clone)]
#[non_exhaustive]
#[cfg_attr(test, derive(PartialEq))]
pub enum CfgPathError {
#[error("Unrecognized variable {0} in path")]
UnknownVar(String),
#[error("Couldn't determine XDG Project Directories, needed to resolve a path; probably, unable to determine HOME directory")]
NoProjectDirs,
#[error("Can't construct base directories to resolve a path element")]
NoBaseDirs,
#[error("Can't find the path to the current binary")]
NoProgramPath,
#[error("Invalid path string: {0:?}")]
InvalidString(String),
#[error("Variable interpolation $ is not supported (tor-config/expand-paths feature disabled)); $ must still be doubled")]
VariableInterpolationNotSupported(String),
#[error("Home dir ~/ is not supported (tor-config/expand-paths feature disabled)")]
HomeDirInterpolationNotSupported(String),
}
impl HasKind for CfgPathError {
fn kind(&self) -> ErrorKind {
use CfgPathError as E;
use ErrorKind as EK;
match self {
E::UnknownVar(_) | E::InvalidString(_) => EK::InvalidConfig,
E::NoProjectDirs | E::NoBaseDirs => EK::NoHomeDirectory,
E::NoProgramPath => EK::InvalidConfig,
E::VariableInterpolationNotSupported(_) | E::HomeDirInterpolationNotSupported(_) => {
EK::FeatureDisabled
}
}
}
}
impl CfgPath {
pub fn new(s: String) -> Self {
CfgPath(PathInner::Shell(s))
}
pub fn new_literal<P: Into<PathBuf>>(path: P) -> Self {
CfgPath(PathInner::Literal(LiteralPath {
literal: path.into(),
}))
}
pub fn path(&self) -> Result<PathBuf, CfgPathError> {
match &self.0 {
PathInner::Shell(s) => expand(s),
PathInner::Literal(LiteralPath { literal }) => Ok(literal.clone()),
}
}
pub fn as_unexpanded_str(&self) -> Option<&str> {
match &self.0 {
PathInner::Shell(s) => Some(s),
PathInner::Literal(_) => None,
}
}
pub fn as_literal_path(&self) -> Option<&Path> {
match &self.0 {
PathInner::Shell(_) => None,
PathInner::Literal(LiteralPath { literal }) => Some(literal),
}
}
}
#[cfg(feature = "expand-paths")]
fn expand(s: &str) -> Result<PathBuf, CfgPathError> {
Ok(shellexpand::path::full_with_context(s, get_home, get_env)
.map_err(|e| e.cause)?
.into_owned())
}
#[cfg(not(feature = "expand-paths"))]
fn expand(input: &str) -> Result<PathBuf, CfgPathError> {
if input.starts_with('~') {
return Err(CfgPathError::HomeDirInterpolationNotSupported(input.into()));
}
let mut out = String::with_capacity(input.len());
let mut s = input;
while let Some((lhs, rhs)) = s.split_once('$') {
if let Some(rhs) = rhs.strip_prefix('$') {
out += lhs;
out += "$";
s = rhs;
} else {
return Err(CfgPathError::VariableInterpolationNotSupported(
input.into(),
));
}
}
out += s;
Ok(out.into())
}
#[cfg(feature = "expand-paths")]
fn get_home() -> Option<&'static Path> {
base_dirs().ok().map(BaseDirs::home_dir)
}
#[cfg(feature = "expand-paths")]
fn get_program_dir() -> Result<Option<PathBuf>, CfgPathError> {
let binary = std::env::current_exe().map_err(|_| CfgPathError::NoProgramPath)?;
Ok(binary.parent().map(ToOwned::to_owned))
}
#[cfg(feature = "expand-paths")]
fn get_env(var: &str) -> Result<Option<Cow<'static, Path>>, CfgPathError> {
match var {
"ARTI_CACHE" => Ok(Some(Cow::Borrowed(project_dirs()?.cache_dir()))),
"ARTI_CONFIG" => Ok(Some(Cow::Borrowed(project_dirs()?.config_dir()))),
"ARTI_SHARED_DATA" => Ok(Some(Cow::Borrowed(project_dirs()?.data_dir()))),
"ARTI_LOCAL_DATA" => Ok(Some(Cow::Borrowed(project_dirs()?.data_local_dir()))),
"PROGRAM_DIR" => Ok(get_program_dir()?.map(Cow::Owned)),
"USER_HOME" => Ok(Some(Cow::Borrowed(base_dirs()?.home_dir()))),
_ => Err(CfgPathError::UnknownVar(var.to_owned())),
}
}
impl std::fmt::Display for CfgPath {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.0 {
PathInner::Literal(LiteralPath { literal }) => write!(fmt, "{:?} [exactly]", literal),
PathInner::Shell(s) => s.fmt(fmt),
}
}
}
#[cfg(feature = "expand-paths")]
fn project_dirs() -> Result<&'static ProjectDirs, CfgPathError> {
static PROJECT_DIRS: Lazy<Option<ProjectDirs>> =
Lazy::new(|| ProjectDirs::from("org", "torproject", "Arti"));
PROJECT_DIRS.as_ref().ok_or(CfgPathError::NoProjectDirs)
}
#[cfg(feature = "expand-paths")]
fn base_dirs() -> Result<&'static BaseDirs, CfgPathError> {
static BASE_DIRS: Lazy<Option<BaseDirs>> = Lazy::new(BaseDirs::new);
BASE_DIRS.as_ref().ok_or(CfgPathError::NoBaseDirs)
}
#[cfg(all(test, feature = "expand-paths"))]
mod test {
#![allow(clippy::unwrap_used)]
use super::*;
#[test]
fn expand_no_op() {
let p = CfgPath::new("Hello/world".to_string());
assert_eq!(p.to_string(), "Hello/world".to_string());
assert_eq!(p.path().unwrap().to_str(), Some("Hello/world"));
let p = CfgPath::new("/usr/local/foo".to_string());
assert_eq!(p.to_string(), "/usr/local/foo".to_string());
assert_eq!(p.path().unwrap().to_str(), Some("/usr/local/foo"));
}
#[cfg(not(target_family = "windows"))]
#[test]
fn expand_home() {
let p = CfgPath::new("~/.arti/config".to_string());
assert_eq!(p.to_string(), "~/.arti/config".to_string());
let expected = dirs::home_dir().unwrap().join(".arti/config");
assert_eq!(p.path().unwrap().to_str(), expected.to_str());
let p = CfgPath::new("${USER_HOME}/.arti/config".to_string());
assert_eq!(p.to_string(), "${USER_HOME}/.arti/config".to_string());
assert_eq!(p.path().unwrap().to_str(), expected.to_str());
}
#[cfg(target_family = "windows")]
#[test]
fn expand_home() {
let p = CfgPath::new("~\\.arti\\config".to_string());
assert_eq!(p.to_string(), "~\\.arti\\config".to_string());
let expected = dirs::home_dir().unwrap().join(".arti\\config");
assert_eq!(p.path().unwrap().to_str(), expected.to_str());
let p = CfgPath::new("${USER_HOME}\\.arti\\config".to_string());
assert_eq!(p.to_string(), "${USER_HOME}\\.arti\\config".to_string());
assert_eq!(p.path().unwrap().to_str(), expected.to_str());
}
#[cfg(not(target_family = "windows"))]
#[test]
fn expand_cache() {
let p = CfgPath::new("${ARTI_CACHE}/example".to_string());
assert_eq!(p.to_string(), "${ARTI_CACHE}/example".to_string());
let expected = project_dirs().unwrap().cache_dir().join("example");
assert_eq!(p.path().unwrap().to_str(), expected.to_str());
}
#[cfg(target_family = "windows")]
#[test]
fn expand_cache() {
let p = CfgPath::new("${ARTI_CACHE}\\example".to_string());
assert_eq!(p.to_string(), "${ARTI_CACHE}\\example".to_string());
let expected = project_dirs().unwrap().cache_dir().join("example");
assert_eq!(p.path().unwrap().to_str(), expected.to_str());
}
#[test]
fn expand_bogus() {
let p = CfgPath::new("${ARTI_WOMBAT}/example".to_string());
assert_eq!(p.to_string(), "${ARTI_WOMBAT}/example".to_string());
assert!(matches!(p.path(), Err(CfgPathError::UnknownVar(_))));
assert_eq!(
&p.path().unwrap_err().to_string(),
"Unrecognized variable ARTI_WOMBAT in path"
);
}
#[test]
fn literal() {
let p = CfgPath::new_literal(PathBuf::from("${ARTI_CACHE}/literally"));
assert_eq!(
p.path().unwrap().to_str().unwrap(),
"${ARTI_CACHE}/literally"
);
assert_eq!(p.to_string(), "\"${ARTI_CACHE}/literally\" [exactly]");
}
}
#[cfg(test)]
mod test_serde {
#![allow(clippy::bool_assert_comparison)]
#![allow(clippy::clone_on_copy)]
#![allow(clippy::dbg_macro)]
#![allow(clippy::mixed_attributes_style)]
#![allow(clippy::print_stderr)]
#![allow(clippy::print_stdout)]
#![allow(clippy::single_char_pattern)]
#![allow(clippy::unwrap_used)]
#![allow(clippy::unchecked_duration_subtraction)]
#![allow(clippy::useless_vec)]
#![allow(clippy::needless_pass_by_value)]
use super::*;
use std::ffi::OsString;
use std::fmt::Debug;
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
struct TestConfigFile {
p: CfgPath,
}
fn non_string_path() -> PathBuf {
#[cfg(target_family = "unix")]
{
use std::os::unix::ffi::OsStringExt;
return PathBuf::from(OsString::from_vec(vec![0x80_u8]));
}
#[cfg(target_family = "windows")]
{
use std::os::windows::ffi::OsStringExt;
return PathBuf::from(OsString::from_wide(&[0xD800_u16]));
}
#[allow(unreachable_code)]
PathBuf::default()
}
fn test_roundtrip_cases<SER, S, DESER, E, F>(ser: SER, deser: DESER)
where
SER: Fn(&TestConfigFile) -> Result<S, E>,
DESER: Fn(&S) -> Result<TestConfigFile, F>,
S: Debug,
E: Debug,
F: Debug,
{
let case = |easy, p| {
let input = TestConfigFile { p };
let s = match ser(&input) {
Ok(s) => s,
Err(e) if easy => panic!("ser failed {:?} e={:?}", &input, &e),
Err(_) => return,
};
dbg!(&input, &s);
let output = deser(&s).expect("deser failed");
assert_eq!(&input, &output, "s={:?}", &s);
};
case(true, CfgPath::new("string".into()));
case(true, CfgPath::new_literal(PathBuf::from("nice path")));
case(true, CfgPath::new_literal(PathBuf::from("path with ✓")));
case(false, CfgPath::new_literal(non_string_path()));
}
#[test]
fn roundtrip_json() {
test_roundtrip_cases(
|input| serde_json::to_string(&input),
|json| serde_json::from_str(json),
);
}
#[test]
fn roundtrip_toml() {
test_roundtrip_cases(|input| toml::to_string(&input), |toml| toml::from_str(toml));
}
#[test]
fn roundtrip_mpack() {
test_roundtrip_cases(
|input| rmp_serde::to_vec(&input),
|mpack| rmp_serde::from_slice(mpack),
);
}
#[test]
#[cfg(feature = "expand-paths")]
fn program_dir() {
let p = CfgPath::new("${PROGRAM_DIR}/foo".to_string());
let mut this_binary = std::env::current_exe().unwrap();
this_binary.pop();
this_binary.push("foo");
let expanded = p.path().unwrap();
assert_eq!(expanded, this_binary);
}
#[test]
#[cfg(not(feature = "expand-paths"))]
fn rejections() {
let chk_err = |s: &str, mke: &dyn Fn(String) -> CfgPathError| {
let p = CfgPath::new(s.to_string());
assert_eq!(p.path().unwrap_err(), mke(s.to_string()));
};
let chk_ok = |s: &str, exp| {
let p = CfgPath::new(s.to_string());
assert_eq!(p.path(), Ok(PathBuf::from(exp)));
};
chk_err(
"some/${PROGRAM_DIR}/foo",
&CfgPathError::VariableInterpolationNotSupported,
);
chk_err("~some", &CfgPathError::HomeDirInterpolationNotSupported);
chk_ok("some$$foo$$bar", "some$foo$bar");
chk_ok("no dollars", "no dollars");
}
}