diff options
author | bptato <nincsnevem662@gmail.com> | 2024-03-17 21:14:51 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2024-03-17 21:20:04 +0100 |
commit | fc8937b53327f99b5809f78e3257e62a05bd1c79 (patch) | |
tree | 64655d81478c9fb8ac93d67259c60cb4fd246b5f /src/types | |
parent | d385d07b197cef65c2d2a800378de9152551e3e6 (diff) | |
download | chawan-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/types')
-rw-r--r-- | src/types/color.nim | 34 |
1 files changed, 10 insertions, 24 deletions
diff --git a/src/types/color.nim b/src/types/color.nim index 2f3f7310..6d46031d 100644 --- a/src/types/color.nim +++ b/src/types/color.nim @@ -517,14 +517,10 @@ proc toJS*(ctx: JSContext, rgb: RGBColor): JSValue = res.pushHex(rgb.b) return toJS(ctx, res) -proc fromJS2*(ctx: JSContext, val: JSValue, o: var JSResult[RGBColor]) = - let s = fromJS[string](ctx, val) - if s.isSome: - o = parseLegacyColor(s.get) - else: - o.err(s.error) +proc fromJSRGBColor*(ctx: JSContext, val: JSValue): JSResult[RGBColor] = + return parseLegacyColor(?fromJS[string](ctx, val)) -proc toJS*(ctx: JSContext, rgba: RGBAColor): JSValue = +proc toJS*(ctx: JSContext; rgba: RGBAColor): JSValue = var res = "#" res.pushHex(rgba.r) res.pushHex(rgba.g) @@ -532,22 +528,12 @@ proc toJS*(ctx: JSContext, rgba: RGBAColor): JSValue = res.pushHex(rgba.a) return toJS(ctx, res) -proc fromJS2*(ctx: JSContext, val: JSValue, o: var JSResult[RGBAColor]) = +proc fromJSRGBAColor*(ctx: JSContext; val: JSValue): JSResult[RGBAColor] = if JS_IsNumber(val): # as hex - let x = fromJS[uint32](ctx, val) - if x.isSome: - o.ok(RGBAColor(x.get)) - else: - o.err(x.error) - else: - # parse - let s = fromJS[string](ctx, val) - if s.isSome: - let x = parseRGBAColor(s.get) - if x.isSome: - o.ok(x.get) - else: - o.err(newTypeError("Unrecognized color")) - else: - o.err(s.error) + return ok(RGBAColor(?fromJS[uint32](ctx, val))) + # parse + let x = parseRGBAColor(?fromJS[string](ctx, val)) + if x.isSome: + return ok(x.get) + return errTypeError("Unrecognized color") |