Remove dependency on Regex

This commit is contained in:
kageru 2020-07-24 15:00:20 +02:00
parent 0fb1912c71
commit bc2b5c3034
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
3 changed files with 11 additions and 64 deletions

49
Cargo.lock generated
View File

@ -1,14 +1,5 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # 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]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.0.0" version = "1.0.0"
@ -51,24 +42,12 @@ dependencies = [
"either", "either",
] ]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.73" version = "0.2.73"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd7d4bd64732af4bf3a67f367c27df8520ad7e230c5817b8ff485864d80242b9" checksum = "bd7d4bd64732af4bf3a67f367c27df8520ad7e230c5817b8ff485864d80242b9"
[[package]]
name = "memchr"
version = "2.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
[[package]] [[package]]
name = "mparsed" name = "mparsed"
version = "0.1.0" version = "0.1.0"
@ -76,7 +55,6 @@ dependencies = [
"chrono", "chrono",
"envy", "envy",
"itertools", "itertools",
"regex",
"serde", "serde",
] ]
@ -117,24 +95,6 @@ dependencies = [
"proc-macro2", "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]] [[package]]
name = "serde" name = "serde"
version = "1.0.114" version = "1.0.114"
@ -166,15 +126,6 @@ dependencies = [
"unicode-xid", "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]] [[package]]
name = "time" name = "time"
version = "0.1.43" version = "0.1.43"

View File

@ -14,6 +14,5 @@ serde = { version = "1.0.114", features = ["derive"] }
itertools = "0.9.0" itertools = "0.9.0"
envy = "0.4.1" envy = "0.4.1"
chrono = { version = "0.4.13", features = ["serde"] } chrono = { version = "0.4.13", features = ["serde"] }
regex = "1.3.9"
[lib] [lib]

View File

@ -4,9 +4,6 @@ mod error;
use error::{Error, MpdResult}; use error::{Error, MpdResult};
use itertools::Itertools; use itertools::Itertools;
mod structs; 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}; pub use structs::{File, Position, State, Stats, Status, Track, UnitResponse};
// some unprintable character to separate repeated keys // some unprintable character to separate repeated keys
@ -60,15 +57,14 @@ pub fn parse_response<'a, I: Iterator<Item = &'a str>, T: de::DeserializeOwned>(
Ok(envy::from_iter(map).unwrap()) 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 /// 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. /// playlist, where the `file: ` key denotes the start of a new item.
/// ///
/// # `first_key` as Regex /// ## Multiple values for `first_key`
/// In some cases, like the `listfiles` command, there are multiple options for `first_key`, so a /// In some cases, like the `listfiles` command, there are multiple possible values for `first_key`,
/// proper regex must be specified. /// so a vector can be specified.
/// ``` /// ```
/// # use regex::Regex;
/// # use mparsed::{parse_response_vec, File}; /// # use mparsed::{parse_response_vec, File};
/// let response = "file: A track.flac /// let response = "file: A track.flac
/// size: 123456 /// size: 123456
@ -76,12 +72,13 @@ pub fn parse_response<'a, I: Iterator<Item = &'a str>, T: de::DeserializeOwned>(
/// directory: A directory /// directory: A directory
/// Last-Modified: 2015-01-30T14:53:03Z /// Last-Modified: 2015-01-30T14:53:03Z
/// OK"; /// OK";
/// let files: Vec<File> = parse_response_vec(response.lines(), Regex::new("^(file|directory): ").unwrap()).unwrap(); /// let files: Vec<File> = 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_eq!(files[0].name, String::from("A track.flac"));
/// assert!(files[1].is_directory()); /// assert!(files[1].is_directory());
/// ``` /// ```
pub fn parse_response_vec<'a, 'b, I: Iterator<Item = &'a str>, T: de::DeserializeOwned>(input: I, first_key: Regex) -> MpdResult<Vec<T>> { pub fn parse_response_vec<'a, I: Iterator<Item = &'a str>, T: de::DeserializeOwned>(input: I, first_keys: &[&str]) -> MpdResult<Vec<T>> {
input input
.peekable() .peekable()
.batching(|it| { .batching(|it| {
@ -95,7 +92,7 @@ pub fn parse_response_vec<'a, 'b, I: Iterator<Item = &'a str>, 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. // 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() { 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); return Some(v);
} }
if l.starts_with("OK") { if l.starts_with("OK") {
@ -303,7 +300,7 @@ duration: 50.155
Pos: 1002 Pos: 1002
Id: 6367 Id: 6367
OK"; 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 { let first_track = Track {
file: "137 A New World.mp3".into(), file: "137 A New World.mp3".into(),
title: Some("A New World".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::<Track>::new())); assert_eq!(queue, Ok(Vec::<Track>::new()));
} }
@ -424,7 +421,7 @@ file: 15 風ハ旅スル(スマートフォンゲーム「風パズル 黒猫
size: 13058417 size: 13058417
Last-Modified: 2019-12-17T08:51:41Z Last-Modified: 2019-12-17T08:51:41Z
OK"; OK";
let parsed: Vec<File> = parse_response_vec(input.lines(), Regex::new("^(file:|directory:)").unwrap()).unwrap(); let parsed: Vec<File> = parse_response_vec(input.lines(), &vec!["file: ", "directory: "]).unwrap();
assert_eq!( assert_eq!(
parsed, parsed,
vec![ vec![