From bc2b5c3034913b71e4135a092195ef61705441f9 Mon Sep 17 00:00:00 2001 From: kageru Date: Fri, 24 Jul 2020 15:00:20 +0200 Subject: [PATCH] Remove dependency on Regex --- Cargo.lock | 49 ------------------------------------------------- Cargo.toml | 1 - src/lib.rs | 25 +++++++++++-------------- 3 files changed, 11 insertions(+), 64 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0270eaa..2174157 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,14 +1,5 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -[[package]] -name = "aho-corasick" -version = "0.7.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "043164d8ba5c4c3035fec9bbee8647c0261d788f3474306f93bb65901cae0e86" -dependencies = [ - "memchr", -] - [[package]] name = "autocfg" version = "1.0.0" @@ -51,24 +42,12 @@ dependencies = [ "either", ] -[[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" - [[package]] name = "libc" version = "0.2.73" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd7d4bd64732af4bf3a67f367c27df8520ad7e230c5817b8ff485864d80242b9" -[[package]] -name = "memchr" -version = "2.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" - [[package]] name = "mparsed" version = "0.1.0" @@ -76,7 +55,6 @@ dependencies = [ "chrono", "envy", "itertools", - "regex", "serde", ] @@ -117,24 +95,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "regex" -version = "1.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", - "thread_local", -] - -[[package]] -name = "regex-syntax" -version = "0.6.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" - [[package]] name = "serde" version = "1.0.114" @@ -166,15 +126,6 @@ dependencies = [ "unicode-xid", ] -[[package]] -name = "thread_local" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" -dependencies = [ - "lazy_static", -] - [[package]] name = "time" version = "0.1.43" diff --git a/Cargo.toml b/Cargo.toml index 555ee9f..9ccbf9b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,5 @@ serde = { version = "1.0.114", features = ["derive"] } itertools = "0.9.0" envy = "0.4.1" chrono = { version = "0.4.13", features = ["serde"] } -regex = "1.3.9" [lib] diff --git a/src/lib.rs b/src/lib.rs index f0fc316..50698bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,9 +4,6 @@ mod error; use error::{Error, MpdResult}; use itertools::Itertools; mod structs; -// TODO: If std::str::pattern::Pattern ever gets stabilized, -// use that instead of depending on the Regex crate -use regex::Regex; pub use structs::{File, Position, State, Stats, Status, Track, UnitResponse}; // some unprintable character to separate repeated keys @@ -60,15 +57,14 @@ pub fn parse_response<'a, I: Iterator, T: de::DeserializeOwned>( Ok(envy::from_iter(map).unwrap()) } -/// Parse an iterator of string slices into a vector of `T`, splitting at `first_key`. +/// Parse an iterator of string slices into a vector of `T`, splitting at any occurence of `first_key`. /// One possible use for this is the `playlistinfo` command which returns all items in the current /// playlist, where the `file: ` key denotes the start of a new item. /// -/// # `first_key` as Regex -/// In some cases, like the `listfiles` command, there are multiple options for `first_key`, so a -/// proper regex must be specified. +/// ## Multiple values for `first_key` +/// In some cases, like the `listfiles` command, there are multiple possible values for `first_key`, +/// so a vector can be specified. /// ``` -/// # use regex::Regex; /// # use mparsed::{parse_response_vec, File}; /// let response = "file: A track.flac /// size: 123456 @@ -76,12 +72,13 @@ pub fn parse_response<'a, I: Iterator, T: de::DeserializeOwned>( /// directory: A directory /// Last-Modified: 2015-01-30T14:53:03Z /// OK"; -/// let files: Vec = parse_response_vec(response.lines(), Regex::new("^(file|directory): ").unwrap()).unwrap(); +/// let files: Vec = parse_response_vec(response.lines(), &vec!["file: ", "directory: "]).unwrap(); /// +/// assert_eq!(files.len(), 2); /// assert_eq!(files[0].name, String::from("A track.flac")); /// assert!(files[1].is_directory()); /// ``` -pub fn parse_response_vec<'a, 'b, I: Iterator, T: de::DeserializeOwned>(input: I, first_key: Regex) -> MpdResult> { +pub fn parse_response_vec<'a, I: Iterator, T: de::DeserializeOwned>(input: I, first_keys: &[&str]) -> MpdResult> { input .peekable() .batching(|it| { @@ -95,7 +92,7 @@ pub fn parse_response_vec<'a, 'b, I: Iterator, T: de::Deserializ }; // Only peek here in case we encounter the first key (e.g. `file:`) line which we still need for the next track. while let Some(l) = it.peek() { - if first_key.is_match(l) { + if first_keys.iter().any(|s| l.starts_with(s)) { return Some(v); } if l.starts_with("OK") { @@ -303,7 +300,7 @@ duration: 50.155 Pos: 1002 Id: 6367 OK"; - let queue = parse_response_vec(input_str.lines(), Regex::new("^file:").unwrap()); + let queue = parse_response_vec(input_str.lines(), &vec!["file: "]); let first_track = Track { file: "137 A New World.mp3".into(), title: Some("A New World".into()), @@ -338,7 +335,7 @@ OK"; ]) ); - let queue = parse_response_vec("OK".lines(), Regex::new("^file:").unwrap()); + let queue = parse_response_vec("OK".lines(), &vec!["file: "]); assert_eq!(queue, Ok(Vec::::new())); } @@ -424,7 +421,7 @@ file: 15 風ハ旅スル(スマートフォンゲーム「風パズル 黒猫 size: 13058417 Last-Modified: 2019-12-17T08:51:41Z OK"; - let parsed: Vec = parse_response_vec(input.lines(), Regex::new("^(file:|directory:)").unwrap()).unwrap(); + let parsed: Vec = parse_response_vec(input.lines(), &vec!["file: ", "directory: "]).unwrap(); assert_eq!( parsed, vec![