use *;
pub struct PermissionsIter<'a> {
client: &'a Toornament,
tournament_id: TournamentId,
}
impl<'a> PermissionsIter<'a> {
pub fn new(client: &'a Toornament, tournament_id: TournamentId) -> PermissionsIter {
PermissionsIter {
client,
tournament_id,
}
}
}
impl<'a> PermissionsIter<'a> {
pub fn with_id(self, permission_id: PermissionId) -> PermissionIter<'a> {
PermissionIter {
client: self.client,
tournament_id: self.tournament_id,
permission_id,
}
}
pub fn create<F: 'static + FnMut() -> Permission>(self, creator: F) -> PermissionCreator<'a> {
PermissionCreator {
client: self.client,
tournament_id: self.tournament_id,
creator: Box::new(creator),
}
}
}
impl<'a> PermissionsIter<'a> {
pub fn collect<T: From<Permissions>>(self) -> Result<T> {
Ok(T::from(
self.client.tournament_permissions(self.tournament_id)?,
))
}
}
pub struct PermissionIter<'a> {
client: &'a Toornament,
tournament_id: TournamentId,
permission_id: PermissionId,
}
impl<'a> PermissionIter<'a> {
pub fn new(
client: &'a Toornament,
tournament_id: TournamentId,
permission_id: PermissionId,
) -> PermissionIter {
PermissionIter {
client,
tournament_id,
permission_id,
}
}
}
impl<'a> PermissionIter<'a> {
pub fn with_id(self, permission_id: PermissionId) -> PermissionIter<'a> {
PermissionIter {
client: self.client,
tournament_id: self.tournament_id,
permission_id,
}
}
pub fn attributes(self) -> PermissionAttributesIter<'a> {
PermissionAttributesIter {
client: self.client,
tournament_id: self.tournament_id,
permission_id: self.permission_id,
}
}
}
impl<'a> PermissionIter<'a> {
pub fn collect<T: From<Permission>>(self) -> Result<T> {
Ok(T::from(self.client.tournament_permission(
self.tournament_id,
self.permission_id,
)?))
}
pub fn delete(self) -> Result<()> {
self.client
.delete_tournament_permission(self.tournament_id, self.permission_id)
}
}
pub struct PermissionCreator<'a> {
client: &'a Toornament,
tournament_id: TournamentId,
creator: Box<dyn FnMut() -> Permission>,
}
impl<'a> PermissionCreator<'a> {
pub fn update(mut self) -> Result<Permission> {
self.client
.create_tournament_permission(self.tournament_id, (self.creator)())
}
pub fn update_iter(mut self) -> Result<PermissionIter<'a>> {
let created = self
.client
.create_tournament_permission(self.tournament_id.clone(), (self.creator)())?;
match created.id {
Some(id) => Ok(PermissionIter::new(self.client, self.tournament_id, id)),
None => Err(Error::Iter(IterError::NoPermissionId)),
}
}
}
pub struct PermissionAttributesIter<'a> {
client: &'a Toornament,
tournament_id: TournamentId,
permission_id: PermissionId,
}
impl<'a> PermissionAttributesIter<'a> {
pub fn collect<T: From<PermissionAttributes>>(self) -> Result<T> {
Ok(T::from(
self.client
.tournament_permission(self.tournament_id, self.permission_id)?
.attributes,
))
}
pub fn edit<F: 'static + FnMut(PermissionAttributes) -> PermissionAttributes>(
self,
editor: F,
) -> PermissionAttributesEditor<'a> {
PermissionAttributesEditor {
client: self.client,
tournament_id: self.tournament_id,
permission_id: self.permission_id,
editor: Box::new(editor),
}
}
pub fn permission(self) -> PermissionIter<'a> {
PermissionIter {
client: self.client,
tournament_id: self.tournament_id,
permission_id: self.permission_id,
}
}
}
pub struct PermissionAttributesEditor<'a> {
client: &'a Toornament,
tournament_id: TournamentId,
permission_id: PermissionId,
editor: Box<dyn FnMut(PermissionAttributes) -> PermissionAttributes>,
}
impl<'a> PermissionAttributesEditor<'a> {
pub fn update(mut self) -> Result<Permission> {
let original = self
.client
.tournament_permission(self.tournament_id.clone(), self.permission_id.clone())?
.attributes;
let edited = (self.editor)(original);
self.client.update_tournament_permission_attributes(
self.tournament_id,
self.permission_id,
edited,
)
}
pub fn update_iter(mut self) -> Result<PermissionAttributesIter<'a>> {
let original = self
.client
.tournament_permission(self.tournament_id.clone(), self.permission_id.clone())?
.attributes;
let edited = (self.editor)(original);
let _ = self.client.update_tournament_permission_attributes(
self.tournament_id.clone(),
self.permission_id.clone(),
edited,
)?;
Ok(PermissionAttributesIter {
client: self.client,
tournament_id: self.tournament_id,
permission_id: self.permission_id,
})
}
}