about summary refs log tree commit diff stats
path: root/src/loader
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-03-17 21:14:51 +0100
committerbptato <nincsnevem662@gmail.com>2024-03-17 21:20:04 +0100
commitfc8937b53327f99b5809f78e3257e62a05bd1c79 (patch)
tree64655d81478c9fb8ac93d67259c60cb4fd246b5f /src/loader
parentd385d07b197cef65c2d2a800378de9152551e3e6 (diff)
downloadchawan-fc8937b53327f99b5809f78e3257e62a05bd1c79.tar.gz
config: clean up/simplify
* Parse the default config at runtime. There's no significant
  performance difference, but this makes it much less painful to write
  config code.
* Add better error reporting
* Make fromJS2 easier to use
* Unquote ChaPaths while parsing config
Diffstat (limited to 'src/loader')
-rw-r--r--src/loader/headers.nim13
-rw-r--r--src/loader/request.nim19
2 files changed, 12 insertions, 20 deletions
diff --git a/src/loader/headers.nim b/src/loader/headers.nim
index f097fa4f..20764760 100644
--- a/src/loader/headers.nim
+++ b/src/loader/headers.nim
@@ -22,18 +22,15 @@ type
 
 jsDestructor(Headers)
 
-proc fromJS2*(ctx: JSContext, val: JSValue, res: var JSResult[HeadersInit]) =
+proc fromJSHeadersInit(ctx: JSContext; val: JSValue): JSResult[HeadersInit] =
   if JS_IsUndefined(val) or JS_IsNull(val):
-    res.err(nil)
-    return
+    return err(nil)
   if isSequence(ctx, val):
     let x = fromJS[seq[(string, string)]](ctx, val)
     if x.isSome:
-      res.ok(HeadersInit(t: HEADERS_INIT_SEQUENCE, s: x.get))
-  else:
-    let x = fromJS[Table[string, string]](ctx, val)
-    if x.isSome:
-      res.ok(HeadersInit(t: HEADERS_INIT_TABLE, tab: x.get))
+      return ok(HeadersInit(t: HEADERS_INIT_SEQUENCE, s: x.get))
+  let x = ?fromJS[Table[string, string]](ctx, val)
+  return ok(HeadersInit(t: HEADERS_INIT_TABLE, tab: x))
 
 proc fill*(headers: Headers, s: seq[(string, string)]) =
   for (k, v) in s:
diff --git a/src/loader/request.nim b/src/loader/request.nim
index f56e1f76..05b02114 100644
--- a/src/loader/request.nim
+++ b/src/loader/request.nim
@@ -176,31 +176,26 @@ type
     proxyUrl: URL
     mode: Opt[RequestMode]
 
-proc fromJS2*(ctx: JSContext, val: JSValue, res: var JSResult[BodyInit]) =
+proc fromJSBodyInit(ctx: JSContext, val: JSValue): JSResult[BodyInit] =
   if JS_IsUndefined(val) or JS_IsNull(val):
-    res.err(nil)
-    return
+    return err(nil)
   block formData:
     let x = fromJS[FormData](ctx, val)
     if x.isSome:
-      res.ok(BodyInit(t: BODY_INIT_FORM_DATA, formData: x.get))
-      return
+      return ok(BodyInit(t: BODY_INIT_FORM_DATA, formData: x.get))
   block blob:
     let x = fromJS[Blob](ctx, val)
     if x.isSome:
-      res.ok(BodyInit(t: BODY_INIT_BLOB, blob: x.get))
-      return
+      return ok(BodyInit(t: BODY_INIT_BLOB, blob: x.get))
   block searchParams:
     let x = fromJS[URLSearchParams](ctx, val)
     if x.isSome:
-      res.ok(BodyInit(t: BODY_INIT_URL_SEARCH_PARAMS, searchParams: x.get))
-      return
+      return ok(BodyInit(t: BODY_INIT_URL_SEARCH_PARAMS, searchParams: x.get))
   block str:
     let x = fromJS[string](ctx, val)
     if x.isSome:
-      res.ok(BodyInit(t: BODY_INIT_STRING, str: x.get))
-      return
-  res.err(newTypeError("Invalid body init type"))
+      return ok(BodyInit(t: BODY_INIT_STRING, str: x.get))
+  return err(newTypeError("Invalid body init type"))
 
 func newRequest*[T: string|Request](ctx: JSContext, resource: T,
     init = none(RequestInit)): JSResult[Request] {.jsctor.} =