use iter::games::GamesIter;
use *;
pub struct TournamentMatchesIter<'a> {
client: &'a Toornament,
tournament_id: TournamentId,
with_games: bool,
}
impl<'a> TournamentMatchesIter<'a> {
pub fn new(client: &'a Toornament, tournament_id: TournamentId) -> TournamentMatchesIter {
TournamentMatchesIter {
client,
tournament_id,
with_games: false,
}
}
}
impl<'a> TournamentMatchesIter<'a> {
pub fn with_games(mut self, with_games: bool) -> Self {
self.with_games = with_games;
self
}
pub fn of_tournament(mut self, id: TournamentId) -> Self {
self.tournament_id = id;
self
}
}
impl<'a> TournamentMatchesIter<'a> {
pub fn with_id(self, match_id: MatchId) -> TournamentMatchIter<'a> {
TournamentMatchIter {
client: self.client,
tournament_id: self.tournament_id,
match_id,
with_games: self.with_games,
}
}
}
impl<'a> TournamentMatchesIter<'a> {
pub fn collect<T: From<Matches>>(self) -> Result<T> {
Ok(T::from(self.client.matches(
self.tournament_id,
None,
self.with_games,
)?))
}
}
pub struct TournamentMatchIter<'a> {
client: &'a Toornament,
tournament_id: TournamentId,
match_id: MatchId,
with_games: bool,
}
impl<'a> TournamentMatchIter<'a> {
pub fn new(
client: &'a Toornament,
tournament_id: TournamentId,
match_id: MatchId,
with_games: bool,
) -> TournamentMatchIter<'a> {
TournamentMatchIter {
client,
tournament_id,
match_id,
with_games,
}
}
}
impl<'a> TournamentMatchIter<'a> {
pub fn edit<F: 'static + FnMut(Match) -> Match>(self, editor: F) -> TournamentMatchEditor<'a> {
TournamentMatchEditor {
client: self.client,
tournament_id: self.tournament_id,
match_id: self.match_id,
with_games: self.with_games,
editor: Box::new(editor),
}
}
pub fn result(self) -> TournamentMatchResultIter<'a> {
TournamentMatchResultIter {
client: self.client,
tournament_id: self.tournament_id,
match_id: self.match_id,
}
}
pub fn games(self) -> GamesIter<'a> {
GamesIter::new(self.client, self.tournament_id, self.match_id)
}
}
impl<'a> TournamentMatchIter<'a> {
pub fn collect<T: From<Match>>(self) -> Result<T> {
let matches = self.client.matches(
self.tournament_id.clone(),
Some(self.match_id.clone()),
self.with_games,
)?;
match matches.0.first() {
Some(m) => Ok(T::from(m.to_owned())),
None => Err(Error::Iter(IterError::NoSuchMatch(
self.tournament_id,
self.match_id,
))),
}
}
}
pub struct TournamentMatchResultIter<'a> {
client: &'a Toornament,
tournament_id: TournamentId,
match_id: MatchId,
}
impl<'a> TournamentMatchResultIter<'a> {
pub fn edit<F: 'static + FnMut(MatchResult) -> MatchResult>(
self,
editor: F,
) -> TournamentMatchResultEditor<'a> {
TournamentMatchResultEditor {
client: self.client,
tournament_id: self.tournament_id,
match_id: self.match_id,
editor: Box::new(editor),
}
}
}
impl<'a> TournamentMatchResultIter<'a> {
pub fn collect<T: From<MatchResult>>(self) -> Result<T> {
Ok(T::from(
self.client
.match_result(self.tournament_id, self.match_id)?,
))
}
}
pub struct TournamentMatchResultEditor<'a> {
client: &'a Toornament,
tournament_id: TournamentId,
match_id: MatchId,
editor: Box<dyn FnMut(MatchResult) -> MatchResult>,
}
impl<'a> TournamentMatchResultEditor<'a> {
pub fn update(mut self) -> Result<MatchResult> {
let original = self
.client
.match_result(self.tournament_id.clone(), self.match_id.clone())?;
self.client
.set_match_result(self.tournament_id, self.match_id, (self.editor)(original))
}
}
pub struct TournamentMatchEditor<'a> {
client: &'a Toornament,
tournament_id: TournamentId,
match_id: MatchId,
with_games: bool,
editor: Box<dyn FnMut(Match) -> Match>,
}
impl<'a> TournamentMatchEditor<'a> {
pub fn update(mut self) -> Result<Match> {
let matches = self.client.matches(
self.tournament_id.clone(),
Some(self.match_id.clone()),
self.with_games,
)?;
let original = match matches.0.first() {
Some(m) => m.to_owned(),
None => {
return Err(Error::Iter(IterError::NoSuchMatch(
self.tournament_id,
self.match_id,
)))
}
};
self.client
.update_match(self.tournament_id, self.match_id, (self.editor)(original))
}
}