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
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use argh::FromArgs;
use log;
use std::error::Error;
use std::path::PathBuf;

pub mod grpc;
pub mod record;
pub mod take;

pub type BoxError = Box<dyn Error>;

pub struct Logger;

impl log::Log for Logger {
    fn enabled(&self, metadata: &log::Metadata) -> bool {
        metadata.level() <= log::Level::Info
    }

    fn log(&self, record: &log::Record) {
        if self.enabled(record.metadata()) {
            println!("{}", record.args());
        }
    }

    fn flush(&self) {}
}

/// Top-level command.
#[derive(FromArgs, PartialEq, Debug)]
pub struct Command {
    /// enable verbose output
    #[argh(switch, short = 'v')]
    verbose: bool,

    #[argh(subcommand)]
    pub nested: SubCommand,
}

/// Additional options such as verbosity
pub struct Opts {
    pub verbose: bool,
}

impl Opts {
    pub fn new(cmd: &Command) -> Self {
        Self {
            verbose: cmd.verbose,
        }
    }
}

#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand)]
pub enum SubCommand {
    Take(Take),
    Record(Record),
}

/// Takes a single frame, sends the request and compares the returned response
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "take")]
pub struct Take {
    /// frame to process
    #[argh(positional)]
    frame: PathBuf,

    /// args passed to grpcurl
    #[argh(option, short = 'H')]
    header: String,

    /// pass proto files used for payload forming
    #[argh(option)]
    proto: Vec<PathBuf>,

    /// address passed to grpcurl
    #[argh(option, short = 'a')]
    addr: String,

    /// filepath of cut file
    #[argh(option, short = 'c')]
    cut: PathBuf,

    /// output of take file
    #[argh(option, short = 'o')]
    output: Option<PathBuf>,
}

/// Attemps to play through an entire Reel sequence
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "record")]
pub struct Record {
    /// directory where frames and cut register is located
    #[argh(positional)]
    path: PathBuf,

    /// name of the reel
    #[argh(positional)]
    name: String,

    /// header string passed to grpcurl
    #[argh(option, short = 'H')]
    header: String,

    /// pass proto files used for payload forming
    #[argh(option)]
    proto: Vec<PathBuf>,

    /// address passed to grpcurl
    #[argh(option, short = 'a')]
    addr: String,

    /// filepath of output cut file
    #[argh(option, short = 'c')]
    cut: Option<PathBuf>,

    /// output directory for successful takes
    #[argh(option, short = 'o')]
    output: Option<PathBuf>,

    /// interactive frame sequence transitions
    #[argh(switch, short = 'i')]
    interactive: bool,
}

impl Take {
    pub fn validate(&self) -> Result<(), &str> {
        if !self.frame.is_file() {
            return Err("<frame> must be a valid file");
        }
        if !self.cut.is_file() {
            return Err("<cut> must be a valid file");
        }
        Ok(())
    }
}
impl Record {
    pub fn validate(&self) -> Result<(), &str> {
        if !self.path.is_dir() {
            return Err("<path> must be a valid directory");
        }
        if let Some(cut) = &self.cut {
            if !cut.is_file() {
                return Err("<cut> must be a valid file");
            }
        } else {
            // check existence of implicit cut file in the same directory
            if !self.get_cut_file().is_file() {
                return Err("unable to find a matching cut file in the given directory");
            }
        }

        if let Some(output) = &self.output {
            if !output.is_dir() {
                return Err("<output> must be a valid directory");
            }
        }
        Ok(())
    }

    /// Returns expected cut filename in the given directory with the provided reel name
    pub fn get_cut_file(&self) -> PathBuf {
        if let Some(cut) = &self.cut {
            cut.clone()
        } else {
            self.path.join(format!("{}.cut.json", self.name))
        }
    }

    /// Checks for the existence of a copy cut file in the given directory with the provided reel name
    pub fn get_cut_copy(&self) -> PathBuf {
        self.path.join(format!(".{}.cut.json", self.name))
    }
}