|
14 | 14 | // You should have received a copy of the GNU General Public License
|
15 | 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16 | 16 |
|
| 17 | +use std::fmt; |
| 18 | +use std::str::FromStr; |
| 19 | + |
17 | 20 | use regex::{Captures, Regex};
|
18 | 21 | use stacks_common::types::net::PeerHost;
|
19 | 22 | use stacks_common::types::StacksEpochId;
|
@@ -45,13 +48,40 @@ pub struct RPCGetHealthResponse {
|
45 | 48 |
|
46 | 49 | const NEIGHBORS_SCOPE_PARAM_NAME: &str = "neighbors";
|
47 | 50 |
|
48 |
| -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] |
49 |
| -#[serde(rename_all = "lowercase")] |
| 51 | +#[derive(Clone, Debug, PartialEq)] |
50 | 52 | pub enum NeighborsScope {
|
51 | 53 | Initial,
|
52 | 54 | All,
|
53 | 55 | }
|
54 | 56 |
|
| 57 | +impl FromStr for NeighborsScope { |
| 58 | + type Err = crate::net::http::Error; |
| 59 | + |
| 60 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 61 | + match s { |
| 62 | + "initial" => Ok(NeighborsScope::Initial), |
| 63 | + "all" => Ok(NeighborsScope::All), |
| 64 | + _ => Err(crate::net::http::Error::Http( |
| 65 | + 400, |
| 66 | + format!( |
| 67 | + "Invalid `neighbors` query parameter: `{}`, allowed values are `initial` or `all`", |
| 68 | + s |
| 69 | + ), |
| 70 | + )), |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl fmt::Display for NeighborsScope { |
| 76 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 77 | + let s = match self { |
| 78 | + NeighborsScope::Initial => "initial", |
| 79 | + NeighborsScope::All => "all", |
| 80 | + }; |
| 81 | + write!(f, "{s}") |
| 82 | + } |
| 83 | +} |
| 84 | + |
55 | 85 | #[derive(Clone)]
|
56 | 86 | /// Empty request handler for the GET /v3/health endpoint
|
57 | 87 | pub struct RPCGetHealthRequestHandler {
|
@@ -97,9 +127,7 @@ impl HttpRequest for RPCGetHealthRequestHandler {
|
97 | 127 |
|
98 | 128 | let req_contents = HttpRequestContents::new().query_string(query);
|
99 | 129 | if let Some(scope) = req_contents.get_query_arg(NEIGHBORS_SCOPE_PARAM_NAME) {
|
100 |
| - self.neighbors_scope = Some(serde_json::from_str(scope.as_str()).map_err(|_e| { |
101 |
| - Error::Http(400, format!("Invalid `neighbors` query parameter: `{}`, allowed values are `initial` or `all`", scope)) |
102 |
| - })?); |
| 130 | + self.neighbors_scope = Some(scope.parse()?); |
103 | 131 | }
|
104 | 132 |
|
105 | 133 | Ok(req_contents)
|
@@ -243,7 +271,7 @@ impl StacksHttpRequest {
|
243 | 271 | format!("/v3/health"),
|
244 | 272 | HttpRequestContents::new().query_arg(
|
245 | 273 | NEIGHBORS_SCOPE_PARAM_NAME.into(),
|
246 |
| - serde_json::to_string(&neighbors_scope).unwrap(), |
| 274 | + neighbors_scope.to_string(), |
247 | 275 | ),
|
248 | 276 | )
|
249 | 277 | .expect("FATAL: failed to construct request from infallible data")
|
|
0 commit comments