|
| 1 | +use std::fmt::Display; |
| 2 | +use std::str::FromStr; |
| 3 | + |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | + |
| 6 | +#[derive(thiserror::Error, Debug, PartialEq, Eq)] |
| 7 | +pub enum Error { |
| 8 | + #[error("The method '{0}' is malformed or otherwise invalid.")] |
| 9 | + BadMethod(String), |
| 10 | + #[error("The method '{0}' is invalid.")] |
| 11 | + InvalidMethodName(String), |
| 12 | +} |
| 13 | + |
| 14 | +#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] |
| 15 | +pub enum Method { |
| 16 | + Project(ProjectMethod), |
| 17 | + Package(PackageMethod), |
| 18 | +} |
| 19 | + |
| 20 | +impl Display for Method { |
| 21 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 22 | + panic!("Invalid use, Method cannot be serialized (despite it being possible to do so)."); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +/// Method namespace registration. |
| 27 | +impl FromStr for Method { |
| 28 | + type Err = Error; |
| 29 | + |
| 30 | + fn from_str(value: &str) -> Result<Self, Self::Err> { |
| 31 | + let mut split = value.split('/'); |
| 32 | + let (namespace, name) = ( |
| 33 | + split.next().ok_or_else(|| Error::BadMethod(value.into()))?, |
| 34 | + split.next().ok_or_else(|| Error::BadMethod(value.into()))?, |
| 35 | + ); |
| 36 | + |
| 37 | + // Route namespaces to the appropriate enum variants for construction. |
| 38 | + Ok(match namespace { |
| 39 | + "project" => Self::Project(ProjectMethod::from_str(&name)?), |
| 40 | + "package" => Self::Package(PackageMethod::from_str(&name)?), |
| 41 | + x => Err(Error::InvalidMethodName(x.into()))?, |
| 42 | + }) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] |
| 47 | +pub enum ProjectMethod { |
| 48 | + /// Set the project directory context. This locks the project, if it exists. |
| 49 | + SetContext, |
| 50 | + /// Release the project directory context. This unlocks the project, if a lock exists. |
| 51 | + ReleaseContext, |
| 52 | + /// Get project metadata. |
| 53 | + GetMetadata, |
| 54 | + /// Add one or more packages to the project. |
| 55 | + AddPackages, |
| 56 | + /// Remove one or more packages from the project. |
| 57 | + RemovePackages, |
| 58 | + /// Get a list of currently installed packages. |
| 59 | + GetPackages, |
| 60 | + /// Determine if the current context is a valid project. |
| 61 | + IsValid, |
| 62 | +} |
| 63 | + |
| 64 | +impl FromStr for ProjectMethod { |
| 65 | + type Err = Error; |
| 66 | + |
| 67 | + fn from_str(value: &str) -> Result<Self, Self::Err> { |
| 68 | + Ok(match value { |
| 69 | + "set_context" => Self::SetContext, |
| 70 | + "release_context" => Self::ReleaseContext, |
| 71 | + "get_metadata" => Self::GetMetadata, |
| 72 | + "add_packages" => Self::AddPackages, |
| 73 | + "remove_packages" => Self::RemovePackages, |
| 74 | + "get_packages" => Self::GetPackages, |
| 75 | + "is_valid" => Self::IsValid, |
| 76 | + x => Err(Error::InvalidMethodName(x.into()))?, |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] |
| 82 | +pub enum PackageMethod { |
| 83 | + /// Get metadata about this package. |
| 84 | + GetMetadata, |
| 85 | + /// Determine if the package exists within the cache. |
| 86 | + IsCached, |
| 87 | +} |
| 88 | + |
| 89 | +impl FromStr for PackageMethod { |
| 90 | + type Err = Error; |
| 91 | + |
| 92 | + fn from_str(value: &str) -> Result<Self, Self::Err> { |
| 93 | + Ok(match value { |
| 94 | + "get_metadata" => Self::GetMetadata, |
| 95 | + "is_cached" => Self::IsCached, |
| 96 | + x => Err(Error::InvalidMethodName(x.into()))?, |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +#[cfg(test)] |
| 102 | +mod test { |
| 103 | + use super::*; |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn test_namespace_resolve() { |
| 107 | + let method = "project/set_context"; |
| 108 | + let resolved = Method::from_str(method).unwrap(); |
| 109 | + assert_eq!(resolved, Method::Project(ProjectMethod::SetContext)); |
| 110 | + |
| 111 | + let method = "project/release_context"; |
| 112 | + let resolved = Method::from_str(method).unwrap(); |
| 113 | + assert_eq!(resolved, Method::Project(ProjectMethod::ReleaseContext)); |
| 114 | + |
| 115 | + // Assert that methods with invalid structure are caught. |
| 116 | + let method = "null"; |
| 117 | + let resolved = Method::from_str(method); |
| 118 | + assert!(resolved.is_err()); |
| 119 | + assert!(matches!(resolved.err().unwrap(), Error::BadMethod(..))); |
| 120 | + |
| 121 | + // Assert that invalid methods with correct structure are caught. |
| 122 | + let method = "null/null"; |
| 123 | + let resolved = Method::from_str(method); |
| 124 | + assert!(resolved.is_err()); |
| 125 | + assert!(matches!( |
| 126 | + resolved.err().unwrap(), |
| 127 | + Error::InvalidMethodName(..), |
| 128 | + )); |
| 129 | + |
| 130 | + // Assert that name resolution can handle bad names. |
| 131 | + let name = "null"; |
| 132 | + let resolved = ProjectMethod::from_str(name); |
| 133 | + assert!(resolved.is_err()); |
| 134 | + assert!(matches!( |
| 135 | + resolved.err().unwrap(), |
| 136 | + Error::InvalidMethodName(..), |
| 137 | + )); |
| 138 | + |
| 139 | + let name = "null"; |
| 140 | + let resolved = PackageMethod::from_str(name); |
| 141 | + assert!(resolved.is_err()); |
| 142 | + assert!(matches!( |
| 143 | + resolved.err().unwrap(), |
| 144 | + Error::InvalidMethodName(..), |
| 145 | + )); |
| 146 | + } |
| 147 | +} |
0 commit comments