use query_builder::{QueryBuilder, BuildQueryResult};
use query_source::QuerySource;

pub trait AsChangeset {
    type Changeset: Changeset;

    fn as_changeset(self) -> Self::Changeset;
}

pub trait Changeset {
    type Target: QuerySource;

    fn to_sql(&self, out: &mut QueryBuilder) -> BuildQueryResult;
}

impl<T> AsChangeset for T where
    T: Changeset,
{
    type Changeset = Self;

    fn as_changeset(self) -> Self::Changeset {
        self
    }
}

use query_builder::{QueryBuilder, BuildQueryResult};
use query_source::QuerySource;

pub trait AsChangeset {
    type Changeset: Changeset;

    fn as_changeset(self) -> Self::Changeset;
}

pub trait Changeset {
    type Target: QuerySource;

    fn to_sql(&self, out: &mut QueryBuilder) -> BuildQueryResult;
}

impl<T> AsChangeset for T where
    T: Changeset,
{
    type Changeset = Self;

    fn as_changeset(self) -> Self::Changeset {
        self
    }
}

impl<T: Changeset> Changeset for Vec<T> {
    type Target = T::Target;

    fn to_sql(&self, out: &mut QueryBuilder) -> BuildQueryResult {
        for (i, changeset) in self.iter().enumerate() {
            if i != 0 {
                out.push_sql(", ");
            }
            try!(changeset.to_sql(out))
        }
        Ok(())
    }
}

impl<T: Changeset> Changeset for Box<T> {
    type Target = T::Target;

    fn to_sql(&self, out: &mut QueryBuilder) -> BuildQueryResult {
        (&**self).to_sql(out)
    }
}

Implement `Changeset` for `Vec<T>` and `Box<T>`
Implement `Changeset` for `Vec<T>` and `Box<T>`

A valid use case has arisen in #26 that is impossible to work around at
the moment. We're going to eventually add a standard API to handle this,
so this implementation has no tests. It will get tested through that
API, once we've figured out what the API will look like. But I want to
get this through so it's possible to work around this by implementing
`AsChangeset` manually.
