Skip to content

Instantly share code, notes, and snippets.

@holyshared
Last active September 21, 2016 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holyshared/3df850d7ffa9c589c40cf92f142d7ac8 to your computer and use it in GitHub Desktop.
Save holyshared/3df850d7ffa9c589c40cf92f142d7ac8 to your computer and use it in GitHub Desktop.

Command Chain

use std::string::{ String };
trait Executor<T> {
fn exec(&self, cmd: &T);
}
struct ShellCommandExecutor;
impl Executor<ShellCommand> for ShellCommandExecutor {
fn exec(&self, cmd: &ShellCommand) {
println!("{}", cmd);
}
}
type ShellCommand = String;
trait Command<T> {
fn send_to(&self, &T);
}
impl<T> Command<T> for ShellCommand where T: Executor<ShellCommand> {
fn send_to(&self, executor: &T) {
executor.exec(&self)
}
}
fn main() {
let executor = ShellCommandExecutor {};
executor.exec(&ShellCommand::from("abc"));
}
use std::fmt:: { Display, Formatter, Result };
use std::borrow::Borrow;
#[derive(Debug)]
struct ShellCommand<T> {
prog: T,
args: Vec<T>
}
impl<T: AsRef<str> + Borrow<str>> ShellCommand<T> {
fn new(program: T, program_args: Vec<T>) -> Self {
ShellCommand {
prog: program,
args: program_args
}
}
}
trait Command<T> {
fn program(&self) -> &T;
fn program_args(&self) -> &[T];
}
impl<T: AsRef<str> + Borrow<str> + Display> Command<T> for ShellCommand<T> {
fn program(&self) -> &T {
&self.prog
}
fn program_args(&self) -> &[T] {
self.args.as_slice()
}
}
impl<T: AsRef<str> + Borrow<str> + Display> Display for ShellCommand<T> {
fn fmt(&self, f: &mut Formatter) -> Result {
let args = self.args.join(" ");
write!(f, "{} {}", self.program(), args)
}
}
fn main() {
let a = ShellCommand::new("ls", vec!("-l"));
println!("{}", a);
}
use std::io:: { Result };
trait Executor {
fn exec(&self, prog: &str, args: &[&str]) -> Result<()>;
}
trait MultiExecutor {
fn exec(&self) -> Result<()>;
}
struct CommandExecutor;
impl Executor for CommandExecutor {
fn exec(&self, prog: &str, args: &[&str]) -> Result<()> {
println!("{} {}", prog, args.join(" "));
Ok(())
}
}
struct SequentialExecutor<'a> {
commands: &'a [(&'a str, &'a [&'a str])]
}
impl<'a> SequentialExecutor<'a> {
fn new(commands: &'a [(&'a str, &'a [&'a str])]) -> SequentialExecutor<'a> {
SequentialExecutor {
commands: commands
}
}
}
impl<'a> MultiExecutor for SequentialExecutor<'a> {
fn exec(&self) -> Result<()> {
let executor = CommandExecutor {};
for cmd in self.commands.iter() {
match executor.exec(cmd.0, cmd.1) {
Ok(_) => println!("ok!!"),
Err(err) => {
return Err(err);
}
}
}
Ok(())
}
}
fn main() {
let executor = CommandExecutor {};
match executor.exec("ls", &[ "-la" ]) {
Ok(_) => println!("ok!!"),
Err(err) => panic!("{:?}", err)
};
let commands = &[ ("ls", &[ "-ls" ][..]) ];
let chain = SequentialExecutor::new(&commands[..]);
match chain.exec() {
Ok(_) => println!("ok!!"),
Err(err) => panic!("{:?}", err)
}
}
use std::fmt::{ Display, Formatter, Result };
type Argument<'a> = &'a str;
#[derive(Debug)]
struct ShellCommand<'a>(Argument<'a>, Vec<Argument<'a>>);
impl<'a> Display for ShellCommand<'a> {
fn fmt(&self, f: &mut Formatter) -> Result {
let args = self.1.join(" ");
let command = format!("{}, {}", self.0, args);
write!(f, "{}", command)
}
}
fn command<'a>(name: Argument<'a>, args: Vec<Argument<'a>>) -> ShellCommand<'a> {
ShellCommand(name, args)
}
fn main() {
let a = ShellCommand("ls", vec!("-l"));
println!("{}", a);
println!("{}", a.to_string());
let b = command("ls", vec!("-l"));
println!("{}", b);
}
fn command(name: &str, args: &[&str]) -> String {
// let v = args.to_vec();
// println!("{:?}", v.join(" "));
println!("{:?}", args.join(" "));
println!("{:?}", name);
String::from(name)//.push(v.join(" "))
}
fn main() {
let a = command("ls", &[ "-l" ]);
println!("{:?}", a);
let b = command("ls", &[ "-l", "-a" ]);
println!("{:?}", b);
}
use std::process::{ Command as PCommand };
type Command = str;
trait ToCommand<'a> {
fn to_command(&self) -> &'a Command;
}
#[derive(Debug)]
struct ListCommand<'a>(&'a str);
impl<'a> ToCommand<'a> for ListCommand<'a> {
fn to_command(&self) -> &'a Command {
self.0
}
}
trait Executor<T> {
fn exec(&self, cmd: T);
}
struct CommandExecutor;
impl<'a, T: ToCommand<'a>> Executor<T> for CommandExecutor {
fn exec(&self, cmd: T) {
let command = cmd.to_command();
let mut args = command.split_whitespace();
let program = args.next().unwrap();
let mut program_args = vec!();
loop {
match args.next() {
Some(v) => { program_args.push(v); },
None => { break; }
};
}
let output = PCommand::new(program)
.args(program_args.as_slice())
.output()
.expect("oops!!");
println!("{:?}", cmd.to_command());
println!("{:?}", output);
}
}
fn main() {
let list = ListCommand("ls -la");
println!("{:?}", list);
let a = CommandExecutor{};
a.exec(list);
}
use std::fmt::{ Display, Formatter, Result };
struct Command<'a>(&'a str, &'a [&'a str]);
fn command<'a>(name: &'a str, args: &'a [&'a str]) -> Command<'a> {
Command(name, args)
}
impl<'a> Display for Command<'a> {
fn fmt(&self, f: &mut Formatter) -> Result {
let args = self.1.join(" ");
let command = format!("{} {}", self.0, args);
write!(f, "{}", command)
}
}
fn main() {
let args = [ "-la" ];
let ls = command("ls", &args);
println!("{}", ls);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment