Skip to content
Myr

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

5 files+53 added−0 removed

modified:crates/globset/src/lib.rs+6 added
108 unmodified lines
109#[macro_use]110extern crate log;111extern crate regex;112113#[cfg(feature = "serde1")]114extern crate serde;115116use std::borrow::Cow;117use std::collections::{BTreeMap, HashMap};118use std::error::Error as StdError;119use std::fmt;
11 unmodified lines
131132mod glob;133mod pathutil;134135#[cfg(feature = "serde1")]136mod serde_impl;137138/// Represents an error that can occur when parsing a glob pattern.139#[derive(Clone, Debug, Eq, PartialEq)]140pub struct Error {141 /// The original glob provided by the caller.
725 unmodified lines
added:crates/globset/src/serde_impl.rs+38 added
1use serde::de::Error;2use serde::{Deserialize, Deserializer, Serialize, Serializer};34use Glob;56impl 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}1415impl<'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}2324#[cfg(test)]25mod tests {26 use Glob;2728 #[test]29 fn glob_json_works() {30 let test_glob = Glob::new("src/**/*.rs").unwrap();3132 let ser = serde_json::to_string(&test_glob).unwrap();33 assert_eq!(ser, "\"src/**/*.rs\"");3435 let de: Glob = serde_json::from_str(&ser).unwrap();36 assert_eq!(test_glob, de);37 }38}
modified:crates/globset/Cargo.toml+3 added
modified:crates/globset/README.md+4 added
modified:Cargo.lock+2 added
133 unmodified lines
134 "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]141142[[package]]143name = "grep"
449 unmodified lines