mirror of
https://github.com/sbrow/thor.git
synced 2026-10-25 10:11:55 -04:00
feat: .Warning added to Error_Kind.
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
# TODOs
|
||||
|
||||
## Priority Unclear (Evaluate Later)
|
||||
|
||||
- [ ] instead of `warnings: [dynamic]Error` we should use `warnings: [dynamic; 8]Error`
|
||||
- when limit reached, the template fails and stops rendering.
|
||||
|
||||
## General
|
||||
|
||||
Remember to polish existing features before moving on to new ones.
|
||||
|
||||
+18
-9
@@ -25,6 +25,7 @@ Context_Stack :: [dynamic]any
|
||||
Error_Kind :: enum {
|
||||
Syntax, // parse-time: malformed template
|
||||
Data, // render-time: template fine, data wrong (e.g. filter misuse)
|
||||
Warning, // non-fatal: operation succeeded but user should know
|
||||
}
|
||||
|
||||
Error_Body :: struct {
|
||||
@@ -176,6 +177,7 @@ render :: proc(
|
||||
data: any,
|
||||
partials: map[string]Template = nil,
|
||||
allocator := context.allocator,
|
||||
warnings: ^[dynamic]Error = nil,
|
||||
) -> (
|
||||
result: string,
|
||||
err: Error,
|
||||
@@ -201,7 +203,7 @@ render :: proc(
|
||||
}
|
||||
|
||||
all_nodes := tmpl.nodes[:]
|
||||
err = render_nodes(tmpl, all_nodes, &ctx, partials, &builder)
|
||||
err = render_nodes(tmpl, all_nodes, &ctx, partials, &builder, warnings = warnings)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
@@ -545,6 +547,7 @@ render_template :: proc(
|
||||
b: ^strings.Builder,
|
||||
blocks: map[string]Block_Override,
|
||||
indent: string,
|
||||
warnings: ^[dynamic]Error = nil,
|
||||
) -> Error {
|
||||
if len(indent) > 0 {
|
||||
state := Indent_State {
|
||||
@@ -552,9 +555,9 @@ render_template :: proc(
|
||||
at_line_start = false,
|
||||
}
|
||||
strings.write_string(b, indent) // first line always gets indent
|
||||
return render_nodes(pt, pt.nodes[:], ctx, partials, b, blocks, &state)
|
||||
return render_nodes(pt, pt.nodes[:], ctx, partials, b, blocks, &state, warnings)
|
||||
}
|
||||
return render_nodes(pt, pt.nodes[:], ctx, partials, b, blocks, nil)
|
||||
return render_nodes(pt, pt.nodes[:], ctx, partials, b, blocks, nil, warnings)
|
||||
}
|
||||
|
||||
write_indented :: proc(
|
||||
@@ -598,6 +601,7 @@ render_nodes :: proc(
|
||||
b: ^strings.Builder,
|
||||
blocks: map[string]Block_Override = nil,
|
||||
indent_state: ^Indent_State = nil,
|
||||
warnings: ^[dynamic]Error = nil,
|
||||
) -> Error {
|
||||
i := 0
|
||||
for i < len(nodes) {
|
||||
@@ -621,7 +625,7 @@ render_nodes :: proc(
|
||||
warn_unknown_key(current, ctx[:], node)
|
||||
}
|
||||
if len(node.filters) > 0 {
|
||||
transformed, perr := apply_pipeline(val, node.filters[:], node.pos, ctx[:], current)
|
||||
transformed, perr := apply_pipeline(val, node.filters[:], node.pos, ctx[:], current, warnings)
|
||||
if perr != nil {
|
||||
return tag_error(perr, current)
|
||||
}
|
||||
@@ -640,7 +644,7 @@ render_nodes :: proc(
|
||||
warn_unknown_key(current, ctx[:], node)
|
||||
}
|
||||
if len(node.filters) > 0 {
|
||||
transformed, perr := apply_pipeline(val, node.filters[:], node.pos, ctx[:], current)
|
||||
transformed, perr := apply_pipeline(val, node.filters[:], node.pos, ctx[:], current, warnings)
|
||||
if perr != nil {
|
||||
return tag_error(perr, current)
|
||||
}
|
||||
@@ -655,7 +659,7 @@ render_nodes :: proc(
|
||||
warn_unknown_key(current, ctx[:], node)
|
||||
}
|
||||
if len(node.filters) > 0 {
|
||||
transformed, perr := apply_pipeline(val, node.filters[:], node.pos, ctx[:], current)
|
||||
transformed, perr := apply_pipeline(val, node.filters[:], node.pos, ctx[:], current, warnings)
|
||||
if perr != nil {
|
||||
return tag_error(perr, current)
|
||||
}
|
||||
@@ -677,6 +681,7 @@ render_nodes :: proc(
|
||||
b,
|
||||
blocks,
|
||||
indent_state,
|
||||
warnings,
|
||||
) or_return
|
||||
}
|
||||
} else {
|
||||
@@ -690,6 +695,7 @@ render_nodes :: proc(
|
||||
b,
|
||||
blocks,
|
||||
indent_state,
|
||||
warnings,
|
||||
) or_return
|
||||
}
|
||||
}
|
||||
@@ -701,7 +707,7 @@ render_nodes :: proc(
|
||||
warn_unknown_key(current, ctx[:], node)
|
||||
}
|
||||
if len(node.filters) > 0 {
|
||||
transformed, perr := apply_pipeline(val, node.filters[:], node.pos, ctx[:], current)
|
||||
transformed, perr := apply_pipeline(val, node.filters[:], node.pos, ctx[:], current, warnings)
|
||||
if perr != nil {
|
||||
return tag_error(perr, current)
|
||||
}
|
||||
@@ -716,6 +722,7 @@ render_nodes :: proc(
|
||||
b,
|
||||
blocks,
|
||||
indent_state,
|
||||
warnings,
|
||||
) or_return
|
||||
}
|
||||
i += 1 + len(node.children)
|
||||
@@ -730,7 +737,7 @@ render_nodes :: proc(
|
||||
if !found {
|
||||
warn_missing_partial(current, partials, node, name)
|
||||
} else {
|
||||
render_template(pt, ctx, partials, b, nil, node.indent) or_return
|
||||
render_template(pt, ctx, partials, b, nil, node.indent, warnings) or_return
|
||||
if indent_state != nil {
|
||||
indent_state.at_line_start = false
|
||||
}
|
||||
@@ -765,6 +772,7 @@ render_nodes :: proc(
|
||||
&temp,
|
||||
content_blocks,
|
||||
nil,
|
||||
warnings,
|
||||
) or_return
|
||||
at_ls := true
|
||||
write_indented(b, node.indent, strings.to_string(temp), &at_ls)
|
||||
@@ -777,6 +785,7 @@ render_nodes :: proc(
|
||||
b,
|
||||
content_blocks,
|
||||
indent_state,
|
||||
warnings,
|
||||
) or_return
|
||||
}
|
||||
i += 1 + len(node.children)
|
||||
@@ -789,7 +798,7 @@ render_nodes :: proc(
|
||||
warn_missing_partial(current, partials, node, node.key)
|
||||
} else {
|
||||
warn_unmatched_block_overrides(current, pt, parent_children)
|
||||
render_template(pt, ctx, partials, b, merged, node.indent) or_return
|
||||
render_template(pt, ctx, partials, b, merged, node.indent, warnings) or_return
|
||||
if indent_state != nil {
|
||||
indent_state.at_line_start = false
|
||||
}
|
||||
|
||||
+33
-22
@@ -221,14 +221,26 @@ apply_pipeline :: proc(
|
||||
pos: int,
|
||||
ctx: []any,
|
||||
tmpl: Template,
|
||||
warnings: ^[dynamic]Error = nil,
|
||||
) -> (
|
||||
current: any,
|
||||
err: Error,
|
||||
) {
|
||||
current = value
|
||||
for &filter in filters {
|
||||
current = apply_filter(current, &filter, pos, ctx, tmpl) or_return
|
||||
log.debugf("applied: filter=%v before=%s after=%s pos=%d", filter, value, current, pos)
|
||||
result, ferr := apply_filter(current, &filter, pos, ctx, tmpl)
|
||||
if ferr != nil {
|
||||
b := body(ferr)
|
||||
if b.kind != .Warning {
|
||||
return result, ferr
|
||||
}
|
||||
if warnings != nil {
|
||||
append(warnings, ferr)
|
||||
} else {
|
||||
log.warnf("%s", format_render_error(ferr, tmpl))
|
||||
}
|
||||
}
|
||||
current = result
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -438,6 +450,7 @@ take :: proc(
|
||||
elem_info, count, data := list_info(value)
|
||||
is_list := elem_info != nil
|
||||
from_start := from_start
|
||||
warning: Error = nil
|
||||
|
||||
str: string
|
||||
if !is_list {
|
||||
@@ -457,15 +470,14 @@ take :: proc(
|
||||
|
||||
n := 1
|
||||
if !is_list && len(args) == 0 {
|
||||
path := tmpl.path != "" ? tmpl.path : "<input>"
|
||||
diag := format_tag_error(
|
||||
path,
|
||||
tmpl.source,
|
||||
op_pos,
|
||||
"defaulting to n=1 for string",
|
||||
"consider specifying n explicitly",
|
||||
)
|
||||
log.warnf("%s", diag)
|
||||
warning = Error_Body {
|
||||
msg = "defaulting to n=1 for string",
|
||||
pos = tag_pos,
|
||||
kind = .Warning,
|
||||
source = tmpl.source,
|
||||
path = tmpl.path,
|
||||
hint = "consider specifying n explicitly",
|
||||
}
|
||||
}
|
||||
if len(args) > 0 {
|
||||
parsed, ok := strconv.parse_int(args[0])
|
||||
@@ -494,15 +506,14 @@ take :: proc(
|
||||
if n < 0 {
|
||||
op_name := from_start ? "first" : "last"
|
||||
alt_name := from_start ? "last" : "first"
|
||||
path := tmpl.path != "" ? tmpl.path : "<input>"
|
||||
diag := format_tag_error(
|
||||
path,
|
||||
tmpl.source,
|
||||
op_pos,
|
||||
fmt.tprintf("%s %d is equivalent to %s %d", op_name, n, alt_name, 0 - n),
|
||||
"consider using the clearer form",
|
||||
)
|
||||
log.warnf("%s", diag)
|
||||
warning = Error_Body {
|
||||
msg = fmt.tprintf("%s %d is equivalent to %s %d", op_name, n, alt_name, 0 - n),
|
||||
pos = tag_pos,
|
||||
kind = .Warning,
|
||||
source = tmpl.source,
|
||||
path = tmpl.path,
|
||||
hint = "consider using the clearer form",
|
||||
}
|
||||
from_start = !from_start
|
||||
n = 0 - n
|
||||
}
|
||||
@@ -522,9 +533,9 @@ take :: proc(
|
||||
for j in start ..< end {
|
||||
append(&result, extract_list_element(elem_info, data, j))
|
||||
}
|
||||
return result, nil
|
||||
return any{new_clone(result, context.temp_allocator), typeid_of([dynamic]any)}, warning
|
||||
} else {
|
||||
return str[start:end], nil
|
||||
return any{new_clone(str[start:end], context.temp_allocator), typeid_of(string)}, warning
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user