use core_traits::Binding;
use query_projector_traits::errors::{ProjectorError, Result};
pub trait BindingTuple: Sized {
fn from_binding_vec(expected: usize, vec: Option<Vec<Binding>>) -> Result<Option<Self>>;
}
impl BindingTuple for Vec<Binding> {
fn from_binding_vec(expected: usize, vec: Option<Vec<Binding>>) -> Result<Option<Self>> {
match vec {
None => Ok(None),
Some(vec) => {
if expected != vec.len() {
Err(ProjectorError::UnexpectedResultsTupleLength(
expected,
vec.len(),
))
} else {
Ok(Some(vec))
}
}
}
}
}
impl BindingTuple for (Binding,) {
fn from_binding_vec(expected: usize, vec: Option<Vec<Binding>>) -> Result<Option<Self>> {
if expected != 1 {
return Err(ProjectorError::UnexpectedResultsTupleLength(1, expected));
}
match vec {
None => Ok(None),
Some(vec) => {
if expected != vec.len() {
Err(ProjectorError::UnexpectedResultsTupleLength(
expected,
vec.len(),
))
} else {
let mut iter = vec.into_iter();
Ok(Some((iter.next().unwrap(),)))
}
}
}
}
}
impl BindingTuple for (Binding, Binding) {
fn from_binding_vec(expected: usize, vec: Option<Vec<Binding>>) -> Result<Option<Self>> {
if expected != 2 {
return Err(ProjectorError::UnexpectedResultsTupleLength(2, expected));
}
match vec {
None => Ok(None),
Some(vec) => {
if expected != vec.len() {
Err(ProjectorError::UnexpectedResultsTupleLength(
expected,
vec.len(),
))
} else {
let mut iter = vec.into_iter();
Ok(Some((iter.next().unwrap(), iter.next().unwrap())))
}
}
}
}
}
impl BindingTuple for (Binding, Binding, Binding) {
fn from_binding_vec(expected: usize, vec: Option<Vec<Binding>>) -> Result<Option<Self>> {
if expected != 3 {
return Err(ProjectorError::UnexpectedResultsTupleLength(3, expected));
}
match vec {
None => Ok(None),
Some(vec) => {
if expected != vec.len() {
Err(ProjectorError::UnexpectedResultsTupleLength(
expected,
vec.len(),
))
} else {
let mut iter = vec.into_iter();
Ok(Some((
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
)))
}
}
}
}
}
impl BindingTuple for (Binding, Binding, Binding, Binding) {
fn from_binding_vec(expected: usize, vec: Option<Vec<Binding>>) -> Result<Option<Self>> {
if expected != 4 {
return Err(ProjectorError::UnexpectedResultsTupleLength(4, expected));
}
match vec {
None => Ok(None),
Some(vec) => {
if expected != vec.len() {
Err(ProjectorError::UnexpectedResultsTupleLength(
expected,
vec.len(),
))
} else {
let mut iter = vec.into_iter();
Ok(Some((
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
)))
}
}
}
}
}
impl BindingTuple for (Binding, Binding, Binding, Binding, Binding) {
fn from_binding_vec(expected: usize, vec: Option<Vec<Binding>>) -> Result<Option<Self>> {
if expected != 5 {
return Err(ProjectorError::UnexpectedResultsTupleLength(5, expected));
}
match vec {
None => Ok(None),
Some(vec) => {
if expected != vec.len() {
Err(ProjectorError::UnexpectedResultsTupleLength(
expected,
vec.len(),
))
} else {
let mut iter = vec.into_iter();
Ok(Some((
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
)))
}
}
}
}
}
impl BindingTuple for (Binding, Binding, Binding, Binding, Binding, Binding) {
fn from_binding_vec(expected: usize, vec: Option<Vec<Binding>>) -> Result<Option<Self>> {
if expected != 6 {
return Err(ProjectorError::UnexpectedResultsTupleLength(6, expected));
}
match vec {
None => Ok(None),
Some(vec) => {
if expected != vec.len() {
Err(ProjectorError::UnexpectedResultsTupleLength(
expected,
vec.len(),
))
} else {
let mut iter = vec.into_iter();
Ok(Some((
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
)))
}
}
}
}
}