<commit_before>//! The Actual RethinkDB Commands

use errors::*;
use conn::{Connection, ConnectOpts};
use serde_json;
use ql2::proto;
use std::io::Write;
use byteorder::{WriteBytesExt, LittleEndian};
use std::io::Read;
use std::str;
use protobuf::ProtobufEnum;
use byteorder::ReadBytesExt;
use super::session::Client;
use super::{Result, r};

pub struct RootCommand(Result<String>);
pub struct Command;
pub struct Query;

pub trait IntoCommandArg {
    fn to_arg(&self) -> Result<String>;
}

impl<'a> IntoCommandArg for &'a str {
    fn to_arg(&self) -> Result<String> {
        Ok(format!("{:?}", self))
    }
}

impl IntoCommandArg for String {
    fn to_arg(&self) -> Result<String> {
        Ok(format!("{:?}", self))
    }
}

impl IntoCommandArg for serde_json::Value {
    fn to_arg(&self) -> Result<String> {
        let arg = try!(serde_json::to_string(self).map_err(|e| DriverError::Json(e)));
        Ok(format!("{}", arg))
    }
}

impl Client {
    pub fn connection(&self) -> ConnectOpts {
        Self::config().read().clone()
    }

    pub fn db_create<T>(&self, name: T) -> RootCommand
        where T: IntoCommandArg
    {
        RootCommand(Command::wrap(proto::Term_TermType::DB_CREATE,
                                     Some(name),
                                     None,
                                     None))
    }

    pub fn db_drop<T>(&self, name: T) -> RootCommand
        where T: IntoCommandArg
    {
        RootCommand(Command::wrap(proto::Term_TermType::DB_DROP,<commit_msg>Allow passing RootCommand as argument<commit_after>//! The Actual RethinkDB Commands

use errors::*;
use conn::{Connection, ConnectOpts};
use serde_json;
use ql2::proto;
use std::io::Write;
use byteorder::{WriteBytesExt, LittleEndian};
use std::io::Read;
use std::str;
use protobuf::ProtobufEnum;
use byteorder::ReadBytesExt;
use super::session::Client;
use super::{Result, r};
use std::fmt;
use std::error::Error as StdError;

pub struct RootCommand(Result<String>);

impl fmt::Display for RootCommand {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.0 {
            Ok(ref cmd) => write!(f, "{}", cmd),
            Err(_) => Err(fmt::Error),
        }
    }
}

pub struct Command;
pub struct Query;

pub trait IntoCommandArg : fmt::Display {
    fn to_arg(&self) -> Result<String> {
        Ok(format!("{}", self))
    }
}

impl<'a> IntoCommandArg for &'a str {
    fn to_arg(&self) -> Result<String> {
        Ok(format!("{:?}", self))
    }
}

impl IntoCommandArg for String {
    fn to_arg(&self) -> Result<String> {
        Ok(format!("{:?}", self))
    }
}

impl IntoCommandArg for serde_json::Value {
    fn to_arg(&self) -> Result<String> {
        match serde_json::to_string(self) {
            Ok(cmd) => Ok(cmd),
            Err(e) => Err(From::from(DriverError::Json(e))),
        }
    }
}

impl IntoCommandArg for RootCommand {
    fn to_arg(&self) -> Result<String> {
        match self.0 {
            Ok(ref cmd) => Ok(cmd.to_string()),
            Err(ref e) => Err(From::from(DriverError::Other(e.description().to_string()))),
        }
    }
}

impl Client {
    pub fn connection(&self) -> ConnectOpts {
        Self::config().read().clone()
    }

    pub fn db_create<T>(&self, name: T) -> RootCommand
        where T: IntoCommandArg
    {
        RootCommand(Command::wrap(proto::Term_TermType::DB_CREATE,
                                     Some(name),
                                     None,
                                     None))
    }

    pub fn db_drop<T>(&self, name: T) -> RootCommand
        where T: IntoCommandArg
    {
        RootCommand(Command::wrap(proto::Term_TermType::DB_DROP,
{'fraction_non_alphanumeric': 0.1552052188094591, 'fraction_numerical': 0.0010872519706441968, 'mean_word_length': 2.6616915422885574, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5167940', 'n_tokens_mistral': 1179, 'n_tokens_neox': 1185, 'n_words': 263}