-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
globset: serde
feature - cannot deserialize String
into Glob
#2386
Comments
I don't think perf is relevant here, since building a glob from a string is quite expensive. Here are the things that will make a PR easier to merge:
@LPGhatguy could you please review this? You're the one who originally added Serde support to |
Thanks for the quick reply! After comparing with what is done in the I created a runnable example that illustrates the error, and how using a different deserialize function can solve it: see https://www.rustexplorer.com/b/qagwmp. The code is reproduced here: /*
[dependencies]
serde = { version = "1", features = ["derive"] }
globset = { version = "0.4.9", features = ["serde1"] }
toml = "0.5.10"
*/
use globset::Glob;
use serde::{Deserialize, Deserializer};
#[derive(Debug, Deserialize)]
struct S {
// (un)comment the following line to see the error
//#[serde(deserialize_with = "deserialize_glob")]
#[allow(unused)]
glob: Glob,
}
#[allow(unused)]
fn deserialize_glob<'de, D: Deserializer<'de>>(
deserializer: D,
) -> std::result::Result<Glob, D::Error> {
use serde::de::Error;
use std::borrow::Cow;
let glob = <Cow<str> as Deserialize>::deserialize(deserializer)?;
Glob::new(&glob).map_err(D::Error::custom)
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let string = "glob = '*.md'";
// Parse toml from borrowed string into S
let _: S = toml::from_str(string)?;
// Parse toml from borrowed string into intermediate toml::Value
let v: toml::Value = toml::from_str(string)?;
// Convert toml::Value into S
// will fail without custom deserialize function
// because v owns the String
let _: S = v.try_into()?;
Ok(())
} |
Based on how I think the correct solution here is to implement our own visitor that handles both the borrowed and owned case. Always using the owned case (using |
Hi @LPGhatguy, I have updated the PR accordingly :-) |
Hi, I hope that posting issues about subcrates is ok.
What version of globset are you using?
globset = { version = "0.4.9", features = ["serde1"] }
.Describe your bug.
Because the implementation of trait
Deserialize
uses&str
, it does not accept deserializing ownedString
.ripgrep/crates/globset/src/serde_impl.rs
Lines 15 to 22 in 6110128
What are the steps to reproduce the behavior?
This problem occurred to me when I tried to parse
Glob
s from atoml::Value
object (already obtained from a file). Astoml::Value
owns strings, it caused a problem.If needed, I can include a MWE (but quite large).
What is the actual behavior?
When parsing some globs, here is the error obtained:
What is the expected behavior?
Not sure if it is interesting performance-wise, but using a custom
deserialize_with
function, withString
instead of&str
, fixes the problem (glob.as_str()
) has to be used afterward).I can provide tests and make a PR if you are ok with this solution. Otherwise, I am also open to discussions :-)
The text was updated successfully, but these errors were encountered: