globset: Implement serde::{Serialize, Deserialize} for Glob
Before adoption: this commit came in with the history the project adopted, so no landing records it.
PR #1492
authored by Lucien Greathouse · committed by GitHub · parent eef7a7e · Browse files · 5 files·+53 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+53 added−0 removed
- other:+53 added−0 removed
Provenance
- Commit
db7a8cd- Tree
c418a6e- Author
Lucien Greathouse <me@lpghatguy.com>- Committer
GitHub <noreply@github.com>- Parent
eef7a7e
5 files changed+53 added−0 removed
modified:crates/globset/src/lib.rs+6 added
108 unmodified lines109 #[macro_use]110 extern crate log;111 extern crate regex;112 113+#[cfg(feature = "serde1")]114+extern crate serde;115+116 use std::borrow::Cow;117 use std::collections::{BTreeMap, HashMap};118 use std::error::Error as StdError;119 use std::fmt;11 unmodified lines131 132 mod glob;133 mod pathutil;134 135+#[cfg(feature = "serde1")]136+mod serde_impl;137+138 /// Represents an error that can occur when parsing a glob pattern.139 #[derive(Clone, Debug, Eq, PartialEq)]140 pub struct Error {141 /// The original glob provided by the caller.725 unmodified linesadded:crates/globset/src/serde_impl.rs+38 added
1+use serde::de::Error;2+use serde::{Deserialize, Deserializer, Serialize, Serializer};3+4+use Glob;5+6+impl Serialize for Glob {7+ fn serialize<S: Serializer>(8+ &self,9+ serializer: S,10+ ) -> Result<S::Ok, S::Error> {11+ serializer.serialize_str(self.glob())12+ }13+}14+15+impl<'de> Deserialize<'de> for Glob {16+ fn deserialize<D: Deserializer<'de>>(17+ deserializer: D,18+ ) -> Result<Self, D::Error> {19+ let glob = <&str as Deserialize>::deserialize(deserializer)?;20+ Glob::new(glob).map_err(D::Error::custom)21+ }22+}23+24+#[cfg(test)]25+mod tests {26+ use Glob;27+28+ #[test]29+ fn glob_json_works() {30+ let test_glob = Glob::new("src/**/*.rs").unwrap();31+32+ let ser = serde_json::to_string(&test_glob).unwrap();33+ assert_eq!(ser, "\"src/**/*.rs\"");34+35+ let de: Glob = serde_json::from_str(&ser).unwrap();36+ assert_eq!(test_glob, de);37+ }38+}modified:crates/globset/Cargo.toml+3 added
22 unmodified lines232425262728293031323334353622 unmodified linesbstr = { version = "0.2.0", default-features = false, features = ["std"] }fnv = "1.0.6"log = "0.4.5"regex = "1.1.5"serde = { version = "1.0.104", optional = true }
[dev-dependencies]glob = "0.3.0"lazy_static = "1"serde_json = "1.0.45"
[features]simd-accel = []serde1 = ["serde"]modified:crates/globset/README.md+4 added
27 unmodified lines28293031323334353637383987 unmodified lines27 unmodified lines```rustextern crate globset;```
### Features
* `serde1`: Enables implementing Serde traits on the `Glob` type.
### Example: one glob
This example shows how to match a single glob against a single file path.
87 unmodified linesmodified:Cargo.lock+2 added
133 unmodified lines134 "glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",135 "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",136 "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",137 "regex 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)",138+ "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",139+ "serde_json 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)",140 ]141 142 [[package]]143 name = "grep"449 unmodified lines