commands: implement response parser for listneighbors

This commit is contained in:
2025-11-21 14:04:12 +09:00
parent 153ae9520f
commit 49f440770e

View File

@@ -1,12 +1,15 @@
use std::collections::HashMap;
use crate::{
Request,
commands::{Command, RequestParserResult, ResponseAttributes, ResponseParserError},
commands::{expect_property_type, Command, RequestParserResult, ResponseAttributes, ResponseParserError}, Request
};
pub struct ListNeighbors;
pub type ListNeighborsResponse = HashMap<String, String>;
impl Command for ListNeighbors {
type Response = Vec<(String, String)>;
type Response = ListNeighborsResponse;
const COMMAND: &'static str = "listneighbors";
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
@@ -17,6 +20,24 @@ impl Command for ListNeighbors {
fn parse_response(
parts: ResponseAttributes<'_>,
) -> Result<Self::Response, ResponseParserError<'_>> {
unimplemented!()
let parts: Vec<_> = parts.into();
debug_assert!(parts.len() % 2 == 0);
let mut result = HashMap::with_capacity(parts.len() / 2);
for channel_message_pair in parts.chunks_exact(2) {
let (neigh_key, neigh_value) = channel_message_pair[0];
let (name_key, name_value) = channel_message_pair[1];
debug_assert!(neigh_key == "neighbor");
debug_assert!(name_key == "name");
let neighbor = expect_property_type!(Some(neigh_value), "neighbor", Text).to_string();
let name = expect_property_type!(Some(name_value), "name", Text).to_string();
result.insert(neighbor, name);
}
Ok(result)
}
}