commercialhaskell / path

Typed filepath
Other
122 stars 45 forks source link

Safely convert a string to a chunk of a path? #177

Closed kindaro closed 2 months ago

kindaro commented 3 years ago

The story is that a user gives my program a name and my program should create a directory with that name to store some files in. I do not think there are any functions in path to help me assure that the input is safe, and I think path is in the best position to host these functions.

By a «chunk» I understand a string that represents exactly one step in a directory hierarchy. path knows what constitutes a valid chunk on a given operating system. For example, on Windows the \ character should be disallowed, while on Linux the / character should be, and path already discerns these cases when parsing and splicing paths.

I imagine three functions might be given:

NorfairKing commented 3 years ago

@kindaro I appreciate the generality of what you're asking, but could you give me an example of what you're doing right now? I'm guessing that resolveDir' will be what you're using, is that right?

kindaro commented 3 years ago

Right now I pass a Text to a function that needs to build a path, then in that function I unpack the text to a String and try to parse as a Path Rel something whenever I need to make a new file or directory. I then use it as if it was a chunk of a path and pray.

It would be better to use resolveFile or resolveDir as you suggest, but I see a few down sides to it:

So, using resolveDir and other similar functions would be better than my current approach, but still far from ideal.

I am also thinking that maybe I should encode the text into a base 64 string — that should always be a valid chunk. But it would not be human readable anymore.

NorfairKing commented 3 years ago

I then use it as if it was a chunk of a path and pray.

Pray for what?

I'm still not very clear on the specifics. Is your code open-source so I can have a look?

NorfairKing commented 2 months ago

Closing because of lack of communication, feel free to reopen.