diff options
author | bptato <nincsnevem662@gmail.com> | 2023-08-26 15:00:06 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-08-26 15:00:06 +0200 |
commit | 2de2e74b954919883178d46ee7ec63fbb1951b58 (patch) | |
tree | ba874de6e86b12c05ab5b7530ba1241ce185306d | |
parent | e5d170aa66a9fdc2ba2258f42d054d7d998e5915 (diff) | |
download | chawan-2de2e74b954919883178d46ee7ec63fbb1951b58.tar.gz |
config: fix overriding default headers
* simplify ActionMap reading * introduce separate case for Table
-rw-r--r-- | src/config/config.nim | 51 | ||||
-rw-r--r-- | src/config/toml.nim | 2 | ||||
-rw-r--r-- | src/main.nim | 2 |
3 files changed, 27 insertions, 28 deletions
diff --git a/src/config/config.nim b/src/config/config.nim index afffdf23..0eb2911b 100644 --- a/src/config/config.nim +++ b/src/config/config.nim @@ -27,7 +27,7 @@ type FormatMode* = set[FormatFlags] - ActionMap = Table[string, string] + ActionMap = distinct Table[string, string] StaticSiteConfig = object url: Opt[string] @@ -142,6 +142,12 @@ type jsDestructor(Config) +proc `[]=`(a: var ActionMap, b, c: string) {.borrow.} +proc `[]`*(a: ActionMap, b: string): string {.borrow.} +proc contains*(a: ActionMap, b: string): bool {.borrow.} +proc getOrDefault(a: ActionMap, b: string): string {.borrow.} +proc hasKeyOrPut(a: var ActionMap, b, c: string): bool {.borrow.} + func getForkServerConfig*(config: Config): ForkServerConfig = return ForkServerConfig( tmpdir: config.external.tmpdir, @@ -255,24 +261,6 @@ func getRealKey(key: string): string = realk &= 'M' return realk -func constructActionTable*(origTable: Table[string, string]): Table[string, string] = - var strs: seq[string] - for k in origTable.keys: - let realk = getRealKey(k) - var teststr = "" - for c in realk: - teststr &= c - strs.add(teststr) - - for k, v in origTable: - let realk = getRealKey(k) - var teststr = "" - for c in realk: - teststr &= c - if strs.contains(teststr): - result[teststr] = "client.feedNext()" - result[realk] = v - proc openFileExpand(dir, file: string): FileStream = if file.len == 0: return nil @@ -370,6 +358,7 @@ proc parseConfigValue(x: var RGBColor, v: TomlValue, k: string) proc parseConfigValue[T](x: var Opt[T], v: TomlValue, k: string) proc parseConfigValue(x: var ActionMap, v: TomlValue, k: string) proc parseConfigValue(x: var CSSConfig, v: TomlValue, k: string) +proc parseConfigValue[U, V](x: var Table[U, V], v: TomlValue, k: string) proc typeCheck(v: TomlValue, vt: ValueType, k: string) = if v.vt != vt: @@ -392,6 +381,15 @@ proc parseConfigValue(x: var object, v: TomlValue, k: string) = fk parseConfigValue(fv, v[kebabk], kkk) +proc parseConfigValue[U, V](x: var Table[U, V], v: TomlValue, k: string) = + typeCheck(v, VALUE_TABLE, k) + x.clear() + for kk, vv in v: + var y: V + let kkk = k & "[" & kk & "]" + parseConfigValue(y, vv, kkk) + x[kk] = y + proc parseConfigValue(x: var bool, v: TomlValue, k: string) = typeCheck(v, VALUE_BOOLEAN, k) x = v.b @@ -495,7 +493,12 @@ proc parseConfigValue(x: var ActionMap, v: TomlValue, k: string) = typeCheck(v, VALUE_TABLE, k) for kk, vv in v: typeCheck(vv, VALUE_STRING, k & "[" & kk & "]") - x[getRealKey(kk)] = vv.s + let rk = getRealKey(kk) + var buf: string + for i in 0 ..< rk.high: + buf &= rk[i] + discard x.hasKeyOrPut(buf, "client.feedNext()") + x[rk] = vv.s var gdir {.compileTime.}: string proc parseConfigValue(x: var CSSConfig, v: TomlValue, k: string) = @@ -560,14 +563,10 @@ proc readConfig(config: Config, dir: string) = config.parseConfig(dir, fs) proc getNormalAction*(config: Config, s: string): string = - if config.page.hasKey(s): - return config.page[s] - return "" + return config.page.getOrDefault(s) proc getLinedAction*(config: Config, s: string): string = - if config.line.hasKey(s): - return config.line[s] - return "" + return config.line.getOrDefault(s) proc readConfig*(): Config = new(result) diff --git a/src/config/toml.nim b/src/config/toml.nim index 28a022c2..6064c3d6 100644 --- a/src/config/toml.nim +++ b/src/config/toml.nim @@ -431,6 +431,8 @@ proc consumeInlineTable(state: var TomlParser): TomlResult = of ' ', '\t': discard of '\n': inc state.line of ',', '}': + if c == '}' and key.len == 0 and val == nil: + return ok(res) # empty, or trailing comma if key.len == 0: return state.err("missing key") if val == nil: diff --git a/src/main.nim b/src/main.nim index be636dcb..f4dcaf08 100644 --- a/src/main.nim +++ b/src/main.nim @@ -147,8 +147,6 @@ if pages.len == 0 and not conf.start.headless: if stdin.isatty: help(1) -conf.page = constructActionTable(conf.page) -conf.line = constructActionTable(conf.line) forks.loadForkServerConfig(conf) let c = newClient(conf, forks, getpid()) |