Skip to content

Add serde support - #66

Merged
chipsenkbeil merged 2 commits into
chipsenkbeil:mainfrom
russellbanks:serde
Aug 28, 2026
Merged

chipsenkbeil merged 2 commits into
chipsenkbeil:mainfrom
russellbanks:serde

Conversation

@russellbanks

Copy link
Copy Markdown
Contributor

This pull request adds support for serde gated behind a serde feature.

This uses serde_core as recommended in order to be quick to compile since it doesn't rely on any proc macros and can compile in parallel with serde_derive.

The actual implementations are effectively the same as what serde defines for Path and PathBuf in the standard library.

Resolves #54.

@chipsenkbeil chipsenkbeil left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting this together! I've read through and added comments on parts.

I think a big open question for me is how serde works with non-UTF8 bytes as I can see in part of the code the use of str::from_utf8 even when we are using Path<T>, PathBuf<T>, TypedPath, and TypedPathBuf which all support bytes that are not UTF-8.

Another thing of note is that the use of str::from_utf8 seems to be failing some tests because it's a 1.87.0 function, although there's some pre-existing function that's an alias to it. I don't want to bump this library that far up as there are other users of this project that are locked to older versions of Rust.

Final question is whether or not we can test without serde_json as it feels like some of the tests are json-specific, and I'd rather us verify the serde logic works and not be bolted to whether it works for serde_json.

where
S: Serializer,
{
match self.to_str() {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can visit bytes, but serializing has to convert to valid UTF-8 characters? Do we not support serializing to bytes?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an exact mirror of serde's Serialize implementation for std::path::Path. I figured it would make sense to copy that seeing as typed_path::NativePath, and therefore typed_path::Path, is intended to mirror the design of Rust’s standard library.

where
D: Deserializer<'de>,
{
deserializer.deserialize_str(PathVisitor(PhantomData))

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how serde works. This is deserializing into a string, but what about if we have a path that contains non-UTF8 bytes? The non-UTF8 path can have that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This matches serde's deserialization implementation for std::path::Path.

Comment thread src/common/non_utf8/pathbuf.rs Outdated
where
E: de::Error,
{
str::from_utf8(v)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we converting to UTF8 here? I think this function is one of the newer ones that's causing the Rust version bump, but also our PathBuf type explicitly supports non-UTF8 characters within it.

where
E: de::Error,
{
String::from_utf8(v)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question as earlier.

Why are we converting to UTF8 here? I think this function is one of the newer ones that's causing the Rust version bump, but also our PathBuf type explicitly supports non-UTF8 characters within it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This matches serde's visit_byte_buf implementation for std::path::PathBuf.

I think this function is one of the newer ones that's causing the Rust version bump

I've replaced all the uses of str::from_utf8 with core::str::from_utf8 which is available from Rust 1.63.0. The alias in str was added in 1.87.0.

where
D: Deserializer<'de>,
{
deserializer.deserialize_string(PathBufVisitor(PhantomData))

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to Path, is this explicitly only supporting a path buf that can be converted to a string? I don't know enough about serde to answer myself. PathBuf supports non-UTF8 characters.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This matches serde's implementation.

#[test]
fn deserialize_root_windows_path() {
// Windows paths can't be borrowed from JSON since the backslashes need to be unescaped
assert!(serde_json::from_str::<TypedPath<'_>>(r#""\\some\\path\\to\\file.txt""#).is_err());

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we testing JSON-specific limitations here? Can we do tests that just validate that serde serialization/deserialization works without being bolted to serde_json?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do tests that just validate that serde serialization/deserialization works without being bolted to serde_json?

No, serde is just the framework - you need a type that implements Deserializer. You can of course use a different format than JSON but as #66 (comment), serde_json is effectively the default for testing.

where
D: Deserializer<'de>,
{
deserializer.deserialize_string(TypedPathBufVisitor)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question as others without me understanding serde: do we need to support byte types here alongside this string deserialize call? I don't know if serde offers that, but want to make sure that TypedPath and TypedPathBuf support both strings and bytes including non-UTF8 bytes.

Comment thread src/typed/non_utf8/pathbuf.rs Outdated
where
E: de::Error,
{
str::from_utf8(v)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as others, we support non-UTF8 bytes in TypedPathBuf, so I would expect that we derive directly from those bytes. Why are we forcing UTF-8 here?

where
E: de::Error,
{
String::from_utf8(v)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as others, we support non-UTF8 bytes in TypedPathBuf, so I would expect that we derive directly from those bytes. Why are we forcing UTF-8 here?

Comment thread src/typed/utf8/path.rs
fn deserialize_root_windows_path() {
// Windows paths can't be borrowed from JSON since the backslashes need to be unescaped
assert!(
serde_json::from_str::<Utf8TypedPath<'_>>(r#""\\some\\path\\to\\file.txt""#).is_err()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment elsewhere. This feels like we're testing serde_json and not how our serialize/deserialize works.

@chipsenkbeil

Copy link
Copy Markdown
Owner

Not sure why the CI didn't run again. Looks good to me, so I'll merge this in. :) Thanks for the contribution!

@chipsenkbeil
chipsenkbeil merged commit 5dba860 into chipsenkbeil:main Aug 28, 2026
@russellbanks
russellbanks deleted the serde branch August 30, 2026 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

serde support?

2 participants