Help with structuring an application

#1

Hi!
As an experiment, I started writing something akin to Ansible to manage my dotfiles.
The reason I think rust is awesome is that could make it fully static and as such self-boostrapping.
The goal is to keep a bunch of files and homebrew packages between two laptops.

Here is the repo: https://github.com/felipesere/dotter

I am wondering about the design though.
For example: how should I deal with errors? I have a Command trait where methods return Result.
What is the right way to deal with multiple errors?
I also don’t really know how to structure my app? At the moment I’ve just dropped files where I thought they make sense, but without much consideration.
I’m also not really sure how to test things.

If someone could point out ways in which I could make the code more idiomatic and testable I’d much appreciate the feedback/advice.

#2

On the topic of errors, the standard way to deal with them is to do something like this

struct Error {
    err_type: ErrorType
    // stuff common to all
}

enum ErrorType {
    // stuff specific to each error type
}

Some crates to help deal with errors are error-chain and failure

to test things, at least for unit tests, just make module for tests, and in it put a bunch of unit test functions. Each function should be marked with the #[test] macro.

// in main file
#[cfg(test)]
mod tests;
// in tests module

#[test]
fn check_if_1_plus_1_is_2() {
    assert_eq!(1 + 1, 2);
}

#[test]
#[should_panic]
fn check_if_1_plus_2_is_not_4() {
    assert_eq!(1 + 2, 4);
}

You can then use the cargo test command to run all of the test
You can use asserts to check things, and if an assert fails then the test fails. When you run cargo test you will see which tests passed and failed.

playground link for testing code

#3

Hi Krishna that is very helpful.

Do you have any good advice on how to structure the Rust app to make it easier to test?
I assume the easiest is to make things that interact with the operating system “injectable” to verify their behaviour?

#4

Unfortunately no, I don’t have much advice to give, I am still new at creating significant applications and I mostly only make small libraries where the structure doesn’t matter too much.