From e9c6d32445f231ae92fdbd99efc0632a0e28fee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Tue, 17 Jan 2023 18:46:38 +0100 Subject: [PATCH] Don't convert XML to minidom::Element anymore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- Cargo.toml | 1 - src/lib.rs | 5 ++++- src/parsers.rs | 52 +++++++++++++++----------------------------------- src/types.rs | 13 ++++++------- 4 files changed, 25 insertions(+), 46 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8a7ab1b..49e731e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,6 @@ description = "Parser for the Scansion DSL" [dependencies] nom = "7.1" jid = "0.9" -minidom = "0.15.1" nom_locate = "4.0.0" [dev-dependencies] diff --git a/src/lib.rs b/src/lib.rs index 7b3d579..c02d1dc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,5 +7,8 @@ mod parsers; mod types; +pub static DEFAULT_NS: &str = "jabber:client"; +pub static SCANSION_NS: &str = "https://matthewwild.co.uk/projects/scansion"; + pub use parsers::parse_spec; -pub use types::{Spec, Action, Client, Metadata}; +pub use types::{Action, Client, Metadata, Spec}; diff --git a/src/parsers.rs b/src/parsers.rs index 5a3821b..b712f20 100644 --- a/src/parsers.rs +++ b/src/parsers.rs @@ -4,13 +4,12 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. -use crate::types::{Spec, Action, Client, ClientName, Metadata}; +use crate::types::{Action, Client, ClientName, Metadata, Spec}; -use std::collections::{BTreeMap, HashMap}; +use std::collections::HashMap; use std::str::FromStr; use jid::Jid; -use minidom::Element; use nom::{ self, branch::alt, @@ -42,9 +41,6 @@ impl<'a> From>>> for Token<'a> { } } -pub static DEFAULT_NS: &str = "jabber:client"; -pub static SCANSION_NS: &str = "https://matthewwild.co.uk/projects/scansion"; - fn allspaces(s: Span) -> IResult { let (s, (pos, _, comments)) = tuple((position, multispace0, opt(comment)))(s)?; @@ -247,21 +243,20 @@ fn parse_action_subline(s: Span) -> IResult { Ok((s, line.fragment())) } -fn parse_send_receive<'a>(tagname: &str, name: String, s: Span<'a>) -> IResult, Action> { +fn parse_send_receive<'a>( + tagname: &str, + name: String, + s: Span<'a>, +) -> IResult, Action<'a>> { let (s, (_, lines)) = tuple(( take_while(|c| c == ' ' || c == '\r' || c == '\n'), // Spaces but \t recognize(many1(parse_action_subline)), ))(s)?; let lines = lines.trim(); - // Namespaces - let mut prefixes = BTreeMap::new(); - prefixes.insert(None, String::from(DEFAULT_NS)); - prefixes.insert(Some(String::from("scansion")), String::from(SCANSION_NS)); - let elem: Element = Element::from_reader_with_prefixes(lines.as_bytes(), prefixes).unwrap(); Ok(match tagname { - "sends:" => (s, Action::Send(name, elem)), - "receives:" => (s, Action::Receive(name, elem)), + "sends:" => (s, Action::Send(name, lines.clone())), + "receives:" => (s, Action::Receive(name, lines)), _ => unreachable!(), }) } @@ -493,11 +488,9 @@ mod tests { /> "#; - let xml = b""; - let send = Action::Send( String::from("rosa"), - Element::from_reader_with_prefixes(&xml[..], String::from(DEFAULT_NS)).unwrap(), + "", ); assert_eq!(parse_action(buf.into()).unwrap().1, send); } @@ -511,11 +504,9 @@ mod tests { /> "#; - let xml = b""; - let receive = Action::Receive( String::from("rosa"), - Element::from_reader_with_prefixes(&xml[..], String::from(DEFAULT_NS)).unwrap(), + "", ); assert_eq!(parse_action(buf.into()).unwrap().1, receive); } @@ -523,16 +514,9 @@ mod tests { #[test] fn test_actions_take_until() { let buf = "Rosa receives:\n\t\n\n\n# Comment\n\nPeter sends:\n\t\n"; - let xml = b""; let actions = vec![ - Action::Receive( - String::from("Rosa"), - Element::from_reader_with_prefixes(&xml[..], String::from(DEFAULT_NS)).unwrap(), - ), - Action::Send( - String::from("Peter"), - Element::from_reader_with_prefixes(&xml[..], String::from(DEFAULT_NS)).unwrap(), - ), + Action::Receive(String::from("Rosa"), ""), + Action::Send(String::from("Peter"), ""), ]; assert_eq!(parse_actions(buf.into()).unwrap().1, actions); } @@ -576,18 +560,12 @@ louise receives: let mut clients: HashMap = HashMap::new(); clients.insert(String::from("louise"), get_client("louise")); - let xml1 = b"\n\n"; - let xml2 = b"\n\n"; - let actions = vec![ Action::Connect(String::from("louise")), - Action::Send( - String::from("louise"), - Element::from_reader_with_prefixes(&xml1[..], String::from(DEFAULT_NS)).unwrap(), - ), + Action::Send(String::from("louise"), ""), Action::Receive( String::from("louise"), - Element::from_reader_with_prefixes(&xml2[..], String::from(DEFAULT_NS)).unwrap(), + "", ), ]; diff --git a/src/types.rs b/src/types.rs index 0bf8e0f..c701b9e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -4,9 +4,8 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. -use std::collections::HashMap; use jid::Jid; -use minidom::Element; +use std::collections::HashMap; #[derive(Debug, Clone, PartialEq)] pub struct Metadata { @@ -62,17 +61,17 @@ impl Client { } #[derive(Debug, Clone, PartialEq)] -pub enum Action { +pub enum Action<'a> { Connect(ClientName), - Send(ClientName, Element), - Receive(ClientName, Element), + Send(ClientName, &'a str), + Receive(ClientName, &'a str), ReceiveNone(ClientName), Disconnect(ClientName), } #[derive(Debug, Clone, PartialEq)] -pub struct Spec { +pub struct Spec<'a> { pub metadata: Option, pub clients: HashMap, - pub actions: Vec, + pub actions: Vec>, }