This could be trash or not, but finished my first rust crate mailcap.

v0.1.0 is pretty much on par (as an example) with the functionality present in the python mailcap library in the stdlib. I noticed that there’s no existing mailcap parsing crate in the rust ecosystem and had plans on actually creating a good custom opener (as my old python one is kinda hacky and well, obviously written when I was very new). So had to make the first step on the parser first before moving onto that.

Mailcap files are honestly much more flexible than .desktop files, as a single configuration file controls everything and it makes creating custom commands much easier. For example, I can specify easily a chain where gifs, despite being images, open in a video player with a custom command, while any other video or image default to something else.

image/gif; mpv --loop-file '%s' --loop=inf; test=test -n "$DISPLAY"
image/*; feh -g 1280x720 --scale-down '%s'; test=test -n "$DISPLAY"
video/*; mpv '%s'; test=test -n "$DISPLAY"

For a quick primer on what the opener would be that justifies this crate, what I use the python one for is setting it as my opener for any software which allows that to be changed (e.g. nnn, alacritty keyboard shortcuts) and overriding XDG_OPENER through a .desktop file. With the above example, any gif will open in mpv and loop infinitely, any other image will open in feh, while any video opens in mpv. The standard ~/.config/mimeapps.list does not support wildcards, and having to create custom .desktop files for variations such as the above is a much greater hassle.

Another plus is specifically for web URLs. The default behavior will be to open in a browser, while using something powered by a mailcap file makes that much more flexible. For example in my current python implementation, seeing the extension for a direct link to a file (e.g. my avi https://lemmygrad.ml/pictrs/image/H7j8YOJVDZ.png) will set this the mime type to image/png, and with the above mailcap example, will default to ‘image/*’ and open up the image in feh instead of a browser. And given that this is a custom opener, I use domain matching for sites such as gfycat to open in mpv.

The above two paragraphs are out of scope for this crate itself, but just wanted to give context on why I began work on it. I’m new to rust, but I hope this initial release is decent!

srht project page: https://sr.ht/~savoy/mailcap/