Skip to content

Commit

Permalink
feat: implement iteration for the WorkingSet
Browse files Browse the repository at this point in the history
  • Loading branch information
illya-laifu committed Sep 11, 2024
1 parent 29fba96 commit fc84959
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ no_implicit_optional = True
check_untyped_defs = True
warn_unused_ignores = True
show_error_codes = True
disable_error_code = assignment
disable_error_code = assignment
29 changes: 25 additions & 4 deletions src/working_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ use taskchampion::WorkingSet as TCWorkingSet;
#[pyclass]
pub struct WorkingSet(pub(crate) TCWorkingSet);

#[pyclass]
struct WorkingSetIter {
iter: std::vec::IntoIter<(usize, String)>,
}

#[pymethods]
impl WorkingSetIter {
fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
slf
}

fn __next__(mut slf: PyRefMut<'_, Self>) -> Option<(usize, String)> {
slf.iter.next()
}
}
#[pymethods]
impl WorkingSet {
pub fn __len__(&self) -> usize {
Expand All @@ -28,9 +43,15 @@ impl WorkingSet {
self.0.by_uuid(Uuid::parse_str(&uuid).unwrap())
}

fn __iter__(_slf: PyRef<'_, Self>) -> PyResult<Py<WorkingSet>> {
todo!("Figure way to propertly implement iterator for python")
// Usability-wise we want it to hold the reference to the iterator, so that
// with each iteration the state persists.
fn __iter__(slf: PyRef<'_, Self>) -> PyResult<Py<WorkingSetIter>> {
let iter = slf
.0
.iter()
.map(|(i, id)| (i, id.to_string()))
.collect::<Vec<_>>()
.into_iter();
let iter = WorkingSetIter { iter };

Py::new(slf.py(), iter)
}
}
4 changes: 3 additions & 1 deletion taskchampion.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional
from datetime import datetime
from enum import Enum
from typing import Optional, Iterator


class Replica:
Expand Down Expand Up @@ -110,6 +110,8 @@ class WorkingSet:
def is_empty(self) -> bool: ...
def by_index(self, index: int) -> Optional[str]: ...
def by_uuid(self, uuid: str) -> Optional[int]: ...
def __iter__(self) -> Iterator[tuple[int, str]]: ...
def __next__(self) -> tuple[int, str]: ...


class Annotation:
Expand Down
14 changes: 7 additions & 7 deletions tests/test_working_set.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from taskchampion import Replica, WorkingSet, Status, Operation
from taskchampion import Replica, WorkingSet, Status
from pathlib import Path
import pytest
import uuid
Expand Down Expand Up @@ -39,14 +39,14 @@ def test_by_index(working_set: WorkingSet):
assert working_set.by_index(1) is not None


@pytest.mark.skip()
def test_iter(working_set: WorkingSet):
assert iter(working_set)


@pytest.mark.skip()
def test_next(working_set: WorkingSet):
assert next(working_set)[0] == 1
assert next(working_set)[0] == 2
with pytest.raises(OSError):
next(working_set)
working_set_iterator = iter(working_set)

assert next(working_set_iterator)[0] == 1
assert next(working_set_iterator)[0] == 2
with pytest.raises(StopIteration):
next(working_set_iterator)

0 comments on commit fc84959

Please sign in to comment.