Lossless path serialization/deserialization

I need to store the original pathname in persistent storage. In cross-platform cases I usually just do the "Your path must be convertible to utf-8" thing, but what I'm working on now requires lossless storage of the original path-and-filename. Moving names between platforms is not a thing here -- I just need Windows names on Windows and unixy names on unixy platforms.

Does anyone know if a crate that can do this? The format of the serialized name does not matter (hex, base64, base85, ..), as long as it can be stuck in a quoted string in some common storage format (RON, Kdl, json).

(Just to (re)emphasize: It's important that the deserialized name exactly matches the original name).

Assuming that you don't want to manually do the conversion, is this helpful? Base64 in serde_with::base64 - Rust

Alternatively, you can do manual conversion with base64 - Rust

All that utf-8 and windows encoding is over my head, but I have to ask if you have seen thread with a possible sollution. I don't understand it all so I could be sending you sideways.

I'd suggest a look at the typed-path crate. It's got a platform-specific "Native" path type, and is pretty good at converting to UTF8. You can also get the underlying bytes directly, for those strange cases when you've got unexpected data in the path.

You can probably Base64 the raw bytes, making sure to store the path type for use when decoding.

You'll have to write platform-specific code for that. Note that the as_encoded_bytes and from_encoded_bytes_unchecked listed there are not suitable for your purpose since you want persistence and those don't guarantee to be stable between Rust versions.