From 46887129040109229e1dcc4377049e94b7502607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Fri, 21 Apr 2023 18:30:41 +0200 Subject: [PATCH] parser: test_context MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/parsers.rs | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/src/parsers.rs b/src/parsers.rs index a6d241e..e3269a6 100644 --- a/src/parsers.rs +++ b/src/parsers.rs @@ -189,26 +189,15 @@ fn parse_client(s: Span) -> IResult { Ok((s, (String::from(name), client))) } -fn parse_clients(s: Span) -> IResult> { +fn parse_context(s: Span) -> IResult { let (s, clients) = many0(parse_client)(s)?; - let mut map: HashMap = HashMap::new(); + let mut map: Context = HashMap::new(); for (name, client) in clients { - map.insert(name, client); + map.insert(name, Entity::Client(client)); } Ok((s, map)) } -fn parse_context(s: Span) -> IResult { - let (s, clients) = parse_clients(s)?; - Ok(( - s, - clients - .into_iter() - .map(|(name, client)| (name, Entity::Client(client))) - .collect::>(), - )) -} - fn parse_sep(s: Span) -> IResult { let (s, (pos, _)) = delimited(allspaces, tuple((position, many1(tag("-")))), allspaces)(s)?; Ok((s, Token { position: pos })) @@ -458,7 +447,7 @@ mod tests { } #[test] - fn test_clients() { + fn test_context() { let buf1 = r#" [Client] louise jid: louise@localhost @@ -469,20 +458,26 @@ mod tests { password: password "#; - let mut clients: HashMap = HashMap::new(); - clients.insert( + let mut context: HashMap = HashMap::new(); + context.insert( String::from("louise"), - Client::new(Jid::from_str("louise@localhost").unwrap(), "password"), + Entity::Client(Client::new( + Jid::from_str("louise@localhost").unwrap(), + "password", + )), ); - clients.insert( + context.insert( String::from("須賀子"), - Client::new(Jid::from_str("sugako@localhost").unwrap(), "password"), + Entity::Client(Client::new( + Jid::from_str("sugako@localhost").unwrap(), + "password", + )), ); assert_eq!( - parse_clients(buf1.into()), + parse_context(buf1.into()), Ok(( unsafe { LocatedSpan::new_from_raw_offset(123, 9, "", ()) }, - clients + context )) ); }