diff --git a/norminette/errors.py b/norminette/errors.py new file mode 100644 index 0000000..0038a37 --- /dev/null +++ b/norminette/errors.py @@ -0,0 +1,36 @@ +from functools import cmp_to_key +from typing import Union, Literal + +from norminette.norm_error import NormError, NormWarning + + +def sort_errs(a, b): + if a.col == b.col and a.line == b.line: + return 1 if a.errno > b.errno else -1 + return a.col - b.col if a.line == b.line else a.line - b.line + + +class Errors: + __slots__ = "_inner" + + def __init__(self) -> None: + self._inner = [] + + def __len__(self) -> int: + return len(self._inner) + + def __iter__(self): + self._inner.sort(key=cmp_to_key(sort_errs)) + return iter(self._inner) + + @property + def status(self) -> Literal["OK", "Error"]: + if not self: + return "OK" + if all(isinstance(it, NormWarning) for it in self): + return "OK" + return "Error" + + def append(self, value: Union[NormError, NormWarning]) -> None: + assert isinstance(value, (NormError, NormWarning)) + self._inner.append(value) diff --git a/norminette/file.py b/norminette/file.py index 5d12be2..ac44a11 100644 --- a/norminette/file.py +++ b/norminette/file.py @@ -1,40 +1,7 @@ import os -from functools import cmp_to_key -from typing import Optional, Union, Literal +from typing import Optional -from norminette.norm_error import NormError, NormWarning - - -def sort_errs(a, b): - if a.col == b.col and a.line == b.line: - return 1 if a.errno > b.errno else -1 - return a.col - b.col if a.line == b.line else a.line - b.line - - -class Errors: - __slots__ = "_inner" - - def __init__(self) -> None: - self._inner = [] - - def __len__(self) -> int: - return len(self._inner) - - def __iter__(self): - self._inner.sort(key=cmp_to_key(sort_errs)) - return iter(self._inner) - - @property - def status(self) -> Literal["OK", "Error"]: - if not self: - return "OK" - if all(isinstance(it, NormWarning) for it in self): - return "OK" - return "Error" - - def append(self, value: Union[NormError, NormWarning]) -> None: - assert isinstance(value, (NormError, NormWarning)) - self._inner.append(value) +from norminette.errors import Errors class File: