Add tests for ServerReference::from_str

This commit is contained in:
Leonard Steppy 2024-12-12 09:45:26 +01:00
parent 0f52824970
commit 0ed1b03e68

View File

@ -3,6 +3,7 @@ use std::cell::LazyCell;
use std::env;
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::path::PathBuf;
use std::str::FromStr;
@ -38,6 +39,9 @@ enum Command {
/// How to handle older versions of the file
#[arg(short = 'a', long, default_value = "delete", default_missing_value = "archive", num_args = 0..1)]
old_version_policy: OldVersionPolicy,
/// The directory where to upload to
#[arg(short, long, default_value = "plugins")]
directory: PathBuf,
},
/// Execute a command on the servers
#[command(visible_short_flag_alias = 'c')]
@ -72,12 +76,15 @@ impl ServerReference {
}
}
#[allow(dead_code)]
pub fn resolve(self, configured_servers: &[Server]) -> Option<Server> {
match self {
ServerReference::Resolved(server) => Some(server),
ServerReference::Name(name) => Self::resolve_server_name(&name, configured_servers),
}
}
#[allow(dead_code)]
pub fn resolve_lazy<S, F>(self, provider: &mut LazyCell<S, F>) -> Option<Server>
where
S: Deref<Target = [Server]>,
@ -116,7 +123,7 @@ impl ServerReference {
}
impl FromStr for ServerReference {
type Err = ServerParseError;
type Err = ServerReferenceParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Server::from_str(s)
@ -125,6 +132,40 @@ impl FromStr for ServerReference {
}
}
impl PartialEq for ServerReference {
fn eq(&self, other: &Self) -> bool {
self.get_name() == other.get_name()
}
}
impl Eq for ServerReference {}
impl Hash for ServerReference {
fn hash<H: Hasher>(&self, state: &mut H) {
self.get_name().hash(state);
}
}
impl Display for ServerReference {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ServerReference::Resolved(server) => write!(f, "{}", server),
ServerReference::Name(name) => write!(f, "{}", name),
}
}
}
#[derive(Debug)]
enum ServerReferenceParseError {}
impl Display for ServerReferenceParseError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self) //replace that with an actual implementation if there ever are any variants
}
}
impl Error for ServerReferenceParseError {}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
struct Server {
pub ssh_name: String,
@ -222,8 +263,9 @@ fn parse_server_configuration(config_str: &str) -> Result<Vec<Server>, String> {
#[cfg(test)]
mod test {
use crate::{parse_server_configuration, Server};
use crate::{parse_server_configuration, Server, ServerReference};
use std::path::PathBuf;
use std::str::FromStr;
#[test]
fn test_parse_server_configuration() {
@ -243,4 +285,19 @@ mod test {
servers
);
}
#[test]
fn test_server_reference_from_str() {
assert_eq!(
ServerReference::Name("foo".to_string()),
ServerReference::from_str("foo").unwrap()
);
assert_eq!(
ServerReference::Resolved(Server {
ssh_name: "crea".to_string(),
server_directory_path: PathBuf::from("server/creative2")
}),
ServerReference::from_str("crea:server/creative2").unwrap()
);
}
}