diff --git a/src/outline_tool.py b/src/outline_tool.py index 49cef487..41d94236 100644 --- a/src/outline_tool.py +++ b/src/outline_tool.py @@ -34,12 +34,14 @@ import ast from pathlib import Path +from src.result_types import Result, ErrorInfo, ErrorKind + class CodeOutliner: def __init__(self) -> None: pass - def outline(self, code: str) -> str: + def outline(self, code: str) -> Result[str]: """ [C: tests/test_outline_tool.py:test_code_outliner_imgui_scopes, tests/test_outline_tool.py:test_code_outliner_nested_ifs, tests/test_outline_tool.py:test_code_outliner_type_hints] """ @@ -47,8 +49,9 @@ class CodeOutliner: try: tree = ast.parse(code) except SyntaxError as e: - return f"ERROR parsing code: {e}" - output = [] + return Result(data=f"ERROR parsing code: {e}", errors=[ErrorInfo(kind=ErrorKind.INVALID_INPUT, message=str(e), source="outline_tool.outline", original=e)]) + output: list[str] = [] + parse_errors: list[ErrorInfo] = [] def get_docstring(node: ast.AST) -> str | None: if isinstance(node, (ast.AsyncFunctionDef, ast.FunctionDef, ast.ClassDef, ast.Module)): @@ -87,8 +90,8 @@ class CodeOutliner: if getattr(node, "returns", None): try: returns = f" -> {ast.unparse(node.returns)}" - except (ValueError, TypeError): - pass + except (ValueError, TypeError) as e: + parse_errors.append(ErrorInfo(kind=ErrorKind.INTERNAL, message=f"ast.unparse failed for {node.name}.returns: {e}", source="outline_tool.walk", original=e)) output.append(f"{' ' * indent}{prefix} {node.name}{returns} (Lines {start_line}-{end_line})") doc = get_docstring(node) if doc: @@ -106,8 +109,8 @@ class CodeOutliner: output.append(f"{' ' * indent}[ImGui Scope] {ctx_str} (Lines {start_line}-{end_line})") is_imgui = True break - except (ValueError, TypeError, AttributeError): - pass + except (ValueError, TypeError, AttributeError) as e: + parse_errors.append(ErrorInfo(kind=ErrorKind.INTERNAL, message=f"ast.unparse failed for ImGui context: {e}", source="outline_tool.walk", original=e)) for item in node.body: walk(item, indent + 1 if is_imgui else indent) else: @@ -119,12 +122,12 @@ class CodeOutliner: for node in tree.body: walk(node) - return "\n".join(output) + return Result(data="\n".join(output), errors=parse_errors) -def get_outline(path: Path, code: str) -> str: +def get_outline(path: Path, code: str) -> Result[str]: suffix = path.suffix.lower() if suffix == ".py": outliner = CodeOutliner() return outliner.outline(code) else: - return f"Outlining not supported for {suffix} files yet." \ No newline at end of file + return Result(data=f"Outlining not supported for {suffix} files yet.") \ No newline at end of file diff --git a/tests/test_outline_tool.py b/tests/test_outline_tool.py index 034ff0be..5bc8a7ff 100644 --- a/tests/test_outline_tool.py +++ b/tests/test_outline_tool.py @@ -8,14 +8,15 @@ def test_code_outliner_type_hints(): class Test: def my_method(self, x: int) -> str: return "hello" - + def my_func(a: bool) -> None: pass """ outliner = CodeOutliner() result = outliner.outline(code) - assert "[Method] my_method -> str (Lines 3-4)" in result - assert "[Func] my_func -> None (Lines 6-7)" in result + assert result.ok, f"outline failed: {result.errors}" + assert "[Method] my_method -> str (Lines 3-4)" in result.data + assert "[Func] my_func -> None (Lines 6-7)" in result.data def test_code_outliner_imgui_scopes(): code = """ @@ -27,8 +28,9 @@ def render_gui() -> None: """ outliner = CodeOutliner() result = outliner.outline(code) - assert "[ImGui Scope] imscope.window('My Window') (Lines 3-6)" in result - assert "[ImGui Scope] imscope.child('My Child') (Lines 5-6)" in result + assert result.ok, f"outline failed: {result.errors}" + assert "[ImGui Scope] imscope.window('My Window') (Lines 3-6)" in result.data + assert "[ImGui Scope] imscope.child('My Child') (Lines 5-6)" in result.data def test_code_outliner_nested_ifs(): code = """ @@ -39,4 +41,5 @@ def render_gui() -> None: """ outliner = CodeOutliner() result = outliner.outline(code) - assert "[ImGui Scope] imscope.window('My Window') (Lines 4-5)" in result + assert result.ok, f"outline failed: {result.errors}" + assert "[ImGui Scope] imscope.window('My Window') (Lines 4-5)" in result.data