#Code Review Request: My First Rust Project - "cosmic-ctl"

4 messages · Page 1 of 1 (latest)

dire torrent
#

Looks good!
I think the only real complaint I have is that the main function is really long. I'd say at least split the subcommands into their own functions, like you did with the first three.

#

Then for further niceties, I think a custom Error type may be nice. I'm personally not a huge fan of having println scattered everywhere. I prefer having a custom Error/Result type and then do all the strings in a display implementation. But here you could definitely argue for preference either way.

pulsar prairie
#

main.rs needs to be split up.
I'd suggest creating commands.rs, and config.rs.

I'd also suggest encapsulating the commands into their own distinct structs, and implementing a common actionable trait for each - i.e. each command has an associated action. You can put this trait in an action.rs.

// ---------------------------------- //
// action.rs
// ---------------------------------- //

trait Action {
    // The error type to be returned on failure.
    type Err;

    // Perform the action.
    fn action(&self) -> Result<(), Self::Err>;
}

// ---------------------------------- //
// command/mod.rs
// ---------------------------------- //

enum Command {
    Read(ReadCommand),
    // etc...
}

impl Action for Command {
    // Use string errors for now.
    type Err = String;

    fn action(&self) -> Result<(), Self::Err> {
        match self {
            Self::Read(read_command) => read_command.action(),
            // etc...
        }
    }
}

// ---------------------------------- //
// command/read.rs
// ---------------------------------- //

struct ReadCommand {
    // version: u64,
    // component: String,
    // entry: String,
}

impl Action for ReadCommand {
    // Use string errors for now.
    type Err = String;

    fn action(&self) -> Result<(), Self::Err> {
        println!("Action Read");
        // etc...
        Ok(())
    }
}

// ---------------------------------- //
// main.rs
// ---------------------------------- //

fn main() {
    let command = Command::Read(ReadCommand{ /* ... */ });
    command.action();
}
#

Then, all your functions at the bottom (like read_configuration) can go in the file with it's command and impl action, or some common actions file.