From da2b6097e9b13f73b02c9156ab509529259ac9b7 Mon Sep 17 00:00:00 2001 From: Steppy Date: Mon, 3 Feb 2025 00:29:23 +0100 Subject: [PATCH] Add relative local path ankers, when parsing servers --- src/main.rs | 4 ++-- src/server.rs | 41 +++++++++++++++++++++++++++++------------ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index e9831ba..16095a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ use crate::command::{ExecutionError, LogRunnable, SpecificExecutionError}; use crate::file::{FileMatcher, FileNameInfo}; use crate::logger::{LogLevel, Logger}; use crate::os_string_builder::ReplaceWithOsStr; -use crate::server::ServerAddress; +use crate::server::{RelativeLocalPathAnker, ServerAddress}; use clap::{Parser, Subcommand, ValueEnum}; use lazy_regex::{lazy_regex, Lazy, Regex}; use server::{Server, ServerReference}; @@ -614,7 +614,7 @@ fn parse_server_configuration(config_str: &str) -> Result, String> { config_str .split(',') .map(|server_entry| { - Server::from_str(server_entry) + Server::from_str(server_entry, RelativeLocalPathAnker::Home) .map_err(|e| format!("Invalid server entry '{server_entry}': {e}")) }) .collect() diff --git a/src/server.rs b/src/server.rs index b6a8a43..2d1ebdc 100644 --- a/src/server.rs +++ b/src/server.rs @@ -2,6 +2,7 @@ use crate::get_home_directory; use std::cell::LazyCell; use std::error::Error; use std::fmt::{Display, Formatter}; +use std::fs; use std::hash::{Hash, Hasher}; use std::ops::Deref; use std::path::PathBuf; @@ -71,7 +72,7 @@ impl FromStr for ServerReference { type Err = ServerReferenceParseError; fn from_str(s: &str) -> Result { - Server::from_str(s) + Server::from_str(s, RelativeLocalPathAnker::CurrentDirectory) .map(Self::Resolved) .or_else(|_| Ok(Self::Identifier(s.to_string()))) } @@ -124,22 +125,28 @@ impl Server { ServerAddress::Localhost => "this computer", } } -} -impl FromStr for Server { - type Err = ServerParseError; - - fn from_str(s: &str) -> Result { + pub fn from_str( + s: &str, + relative_local_path_anker: RelativeLocalPathAnker, + ) -> Result { s.split_once(':') .ok_or(ServerParseError::MissingServerDirectory) .and_then(|(identifier, server_directory)| { let address = ServerAddress::from_str(identifier); - let mut server_directory_path = PathBuf::from(server_directory); - if let ServerAddress::Localhost = &address { - let home_directory = get_home_directory() - .map_err(|e| ServerParseError::HomeDirectoryRequired { detail_message: e })?; - server_directory_path = home_directory.join(&server_directory_path); - } + let server_directory_path = match &address { + ServerAddress::Ssh { .. } => PathBuf::from(server_directory), + ServerAddress::Localhost => fs::canonicalize(match relative_local_path_anker { + RelativeLocalPathAnker::Home => { + let home_directory = get_home_directory() + .map_err(|e| ServerParseError::HomeDirectoryRequired { detail_message: e })?; + home_directory.join(server_directory) + } + RelativeLocalPathAnker::CurrentDirectory => PathBuf::from(server_directory), + }) + .map_err(|_| ServerParseError::ServerDirectoryNonExistent)?, + }; + Ok(Self { address, server_directory_path, @@ -162,6 +169,12 @@ impl Display for Server { } } +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +pub enum RelativeLocalPathAnker { + Home, + CurrentDirectory, +} + #[derive(Debug, Clone, Eq, PartialEq, Hash)] pub enum ServerAddress { Ssh { ssh_address: String }, @@ -201,6 +214,7 @@ impl ServerAddress { #[derive(Debug)] pub enum ServerParseError { MissingServerDirectory, + ServerDirectoryNonExistent, HomeDirectoryRequired { detail_message: String }, } @@ -218,6 +232,9 @@ impl Display for ServerParseError { f, "localhost requires home directory, but: {detail_message}" ), + ServerParseError::ServerDirectoryNonExistent => { + write!(f, "The specified server directory doesn't exist") + } } } }