Gsoc week 1 report
Here the report of my first week of code for the Google Summer of Code.
Cargo-ebuild
The work on cargo-ebuild has started. The overall design has been redesigned to prepare the projects to receive more features in the next weeks. Some new features like PackageInfo and licences have been ported from cargo-bitbake by the some author of cargo-ebuild.
About writing a cli
Writing a cli in rust is a enjoyable experience. I spent some looking at existing projects and how they manage the typical design problems of writing binaries, like arguments parsing, logs, error handling and more. Most of the most interesting insparations comes from the cli working group and testing with two interesting projects: quicli and ergo. They try to make the life easier. Being very simple all-in-one tools that re-export some well-know crates for common needs. Here I summarize some first thoughts about my work.
Language
- abuse of
impl
- abuse of
newtype
- modules are friends
Errors
I found experimenting with errors a good learning exercise to understand some of the most interesting features of the language. In my case I decided to follow a simple approach, the cli can panic but lib should never unless for std
bad stuff.
But this doesn’t prevent me from prototyping and using unwrap/expect a lot! Unwrapping is natural and because is an explicit action is very easy in the future to just grep all the ugly and dangerous stuff done while experimenting and replace them with a better solution.
To summarize:
unwrap
,panic
andexpect
are perfect for a first touch- play with
failure
andfailure::Error
(instead of explicit use oferror-chain
) - abuse of
?
operator andnewtype
and_then
,map_or_else
,ok_or_else
and relates are your friend more than you can imagine- starting from rust 1.26 the main can return
Result
- in errors like in other subjects of the language rust can seem quite heavy and complex, but because forces the programmer to be explicit is very helpful
- using
Error
is not always the best choice because allocates, but for the most use cases is good. I don’t think that writing in a low-level language means in any case to be crazy about performance. Allocating a string is good price to pay for a simple error-handling workflow. If you need more keep playing with error and define yours ;) - I’ve never missed exceptions…
Using a strongly statically typed language like rust without getting full control of your errors is a shame.
Some resources:
- The rust book and cookbook are (as always) a perfect starting point
- Andrew Gallant wrotes one of the most comprehensive guide about rust’s errors.
- Take a look to failure
- Herman J. Radtke wrotes a guide about
and_then
and relates.
Misc
- The debian team has a good policy for those who wants to package cargo projects.
- On crates.io there is a good mix of rust common patterns.
Gentoo fix
I done a bunch of contributions to the gentoo-rust overlay. During my work I discovered repoman as a tool “used to enforce a minimal level of quality assurance in ebuilds and related metadata added to ebuild repositories”.