about summary refs log tree commit diff
path: root/src/rustbook/subcommand.rs
blob: 473739c919d64ee27fc4e3e3e40a85e3b0e29426 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Common API for all rust-book subcommands.

use error::CliResult;
use error::CommandResult;
use term::Term;

use help;
use build;
use serve;
use test;

pub trait Subcommand {
    /// Mutate the subcommand by parsing its arguments.
    ///
    /// Returns `Err` on a parsing error.
    fn parse_args(&mut self, args: &[String]) -> CliResult<()>;
    /// Print the CLI usage information.
    fn usage(&self);
    /// Actually execute the subcommand.
    fn execute(&mut self, term: &mut Term) -> CommandResult<()>;
}

/// Create a Subcommand object based on its name.
pub fn parse_name(name: &str) -> Option<Box<Subcommand>> {
    for parser in [
        help::parse_cmd as fn(&str) -> Option<Box<Subcommand>>,
        build::parse_cmd as fn(&str) -> Option<Box<Subcommand>>,
        serve::parse_cmd as fn(&str) -> Option<Box<Subcommand>>,
        test::parse_cmd as fn(&str) -> Option<Box<Subcommand>>].iter() {
        let parsed = (*parser)(name);
        if parsed.is_some() { return parsed }
    }
    None
}