Skip to content

Commit f64e1f0

Browse files
committed
Shortened path
1 parent b5567d9 commit f64e1f0

1 file changed

Lines changed: 34 additions & 2 deletions

File tree

src/decimo/errors.mojo

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,38 @@ comptime RuntimeError = DecimoError[error_type="RuntimeError"]
8080
failures, missing native libraries)."""
8181

8282

83+
def _shorten_path(full_path: String) -> String:
84+
"""Shorten an absolute file path to a relative path.
85+
86+
Looks for known directory markers (``src/``, ``tests/``, ``benches/``) and
87+
returns a ``./``-prefixed relative path from the last marker found.
88+
If no marker is found, returns just the filename.
89+
90+
Uses ``rfind`` (reverse search) to handle paths that contain a marker more
91+
than once, e.g. ``/home/user/src/projects/decimo/src/decimo/bigint.mojo``
92+
correctly shortens to ``./src/decimo/bigint.mojo``.
93+
94+
Args:
95+
full_path: The absolute file path to shorten.
96+
97+
Returns:
98+
A shortened relative path string.
99+
"""
100+
var idx = full_path.rfind("src/")
101+
if idx >= 0:
102+
return "./" + String(full_path[byte=idx:])
103+
idx = full_path.rfind("tests/")
104+
if idx >= 0:
105+
return "./" + String(full_path[byte=idx:])
106+
idx = full_path.rfind("benches/")
107+
if idx >= 0:
108+
return "./" + String(full_path[byte=idx:])
109+
var last_slash = full_path.rfind("/")
110+
if last_slash >= 0:
111+
return String(full_path[byte = last_slash + 1 :])
112+
return full_path
113+
114+
83115
struct DecimoError[error_type: StringLiteral = "DecimoError"](Writable):
84116
"""Base type for all Decimo errors.
85117
@@ -127,8 +159,8 @@ struct DecimoError[error_type: StringLiteral = "DecimoError"](Writable):
127159
function: The function name where the error occurred.
128160
previous_error: An optional previous error that caused this one.
129161
"""
130-
var loc = call_location()
131-
self.file = String(loc.file_name)
162+
var loc = call_location() # Comptime evaluated
163+
self.file = _shorten_path(String(loc.file_name))
132164
self.line = loc.line
133165
self.function = function
134166
self.message = message

0 commit comments

Comments
 (0)