diff options
author | bptato <nincsnevem662@gmail.com> | 2024-03-26 20:46:36 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2024-03-26 20:46:36 +0100 |
commit | 845445b4c4ab69cc2ffc564c7ab71eeaf8cc3c5e (patch) | |
tree | 3da08fcb6e68f5bd90f9e16c02663dd2990868ff /src/config | |
parent | 2391d5f3e4a599fc300030b6db892637de2dacc4 (diff) | |
download | chawan-845445b4c4ab69cc2ffc564c7ab71eeaf8cc3c5e.tar.gz |
config, toml: rename enums
Diffstat (limited to 'src/config')
-rw-r--r-- | src/config/config.nim | 94 | ||||
-rw-r--r-- | src/config/toml.nim | 108 |
2 files changed, 101 insertions, 101 deletions
diff --git a/src/config/config.nim b/src/config/config.nim index 962ede7f..ebf36d49 100644 --- a/src/config/config.nim +++ b/src/config/config.nim @@ -25,7 +25,7 @@ import chagashi/charset type ColorMode* = enum - MONOCHROME, ANSI, EIGHT_BIT, TRUE_COLOR + cmMonochrome, cmANSI, cmEightBit, cmTrueColor FormatMode* = set[FormatFlags] @@ -332,19 +332,19 @@ proc parseConfigValue(ctx: var ConfigParser; x: var Mailcap; v: TomlValue; proc parseConfigValue(ctx: var ConfigParser; x: var URIMethodMap; v: TomlValue; k: string) -proc typeCheck(v: TomlValue, vt: ValueType, k: string) = - if v.vt != vt: +proc typeCheck(v: TomlValue; t: TomlValueType; k: string) = + if v.t != t: raise newException(ValueError, "invalid type for key " & k & - " (got " & $v.vt & ", expected " & $vt & ")") + " (got " & $v.t & ", expected " & $t & ")") -proc typeCheck(v: TomlValue, vt: set[ValueType], k: string) = - if v.vt notin vt: +proc typeCheck(v: TomlValue; t: set[TomlValueType]; k: string) = + if v.t notin t: raise newException(ValueError, "invalid type for key " & k & - " (got " & $v.vt & ", expected " & $vt & ")") + " (got " & $v.t & ", expected " & $t & ")") proc parseConfigValue(ctx: var ConfigParser; x: var object; v: TomlValue; k: string) = - typeCheck(v, VALUE_TABLE, k) + typeCheck(v, tvtTable, k) for fk, fv in x.fieldPairs: when typeof(fv) isnot JSContext: let kebabk = snakeToKebabCase(fk) @@ -357,7 +357,7 @@ proc parseConfigValue(ctx: var ConfigParser; x: var object; v: TomlValue; proc parseConfigValue[U, V](ctx: var ConfigParser; x: var Table[U, V]; v: TomlValue; k: string) = - typeCheck(v, VALUE_TABLE, k) + typeCheck(v, tvtTable, k) x.clear() for kk, vv in v: var y: V @@ -367,7 +367,7 @@ proc parseConfigValue[U, V](ctx: var ConfigParser; x: var Table[U, V]; proc parseConfigValue[U, V](ctx: var ConfigParser; x: var TableRef[U, V]; v: TomlValue; k: string) = - typeCheck(v, VALUE_TABLE, k) + typeCheck(v, tvtTable, k) x = TableRef[U, V]() for kk, vv in v: var y: V @@ -377,23 +377,23 @@ proc parseConfigValue[U, V](ctx: var ConfigParser; x: var TableRef[U, V]; proc parseConfigValue(ctx: var ConfigParser; x: var bool; v: TomlValue; k: string) = - typeCheck(v, VALUE_BOOLEAN, k) + typeCheck(v, tvtBoolean, k) x = v.b proc parseConfigValue(ctx: var ConfigParser; x: var string; v: TomlValue; k: string) = - typeCheck(v, VALUE_STRING, k) + typeCheck(v, tvtString, k) x = v.s proc parseConfigValue(ctx: var ConfigParser; x: var ChaPath; v: TomlValue; k: string) = - typeCheck(v, VALUE_STRING, k) + typeCheck(v, tvtString, k) x = ChaPath(v.s) proc parseConfigValue[T](ctx: var ConfigParser; x: var seq[T]; v: TomlValue; k: string) = - typeCheck(v, {VALUE_STRING, VALUE_ARRAY}, k) - if v.vt != VALUE_ARRAY: + typeCheck(v, {tvtString, tvtArray}, k) + if v.t != tvtArray: var y: T ctx.parseConfigValue(y, v, k) x = @[y] @@ -407,12 +407,12 @@ proc parseConfigValue[T](ctx: var ConfigParser; x: var seq[T]; v: TomlValue; proc parseConfigValue(ctx: var ConfigParser; x: var TomlTable; v: TomlValue; k: string) = - typeCheck(v, {VALUE_TABLE}, k) - x = v.t + typeCheck(v, {tvtTable}, k) + x = v.tab proc parseConfigValue(ctx: var ConfigParser; x: var Charset; v: TomlValue; k: string) = - typeCheck(v, VALUE_STRING, k) + typeCheck(v, tvtString, k) x = getCharset(v.s) if x == CHARSET_UNKNOWN: raise newException(ValueError, "unknown charset '" & v.s & "' for key " & @@ -420,31 +420,31 @@ proc parseConfigValue(ctx: var ConfigParser; x: var Charset; v: TomlValue; proc parseConfigValue(ctx: var ConfigParser; x: var int32; v: TomlValue; k: string) = - typeCheck(v, VALUE_INTEGER, k) + typeCheck(v, tvtInteger, k) x = int32(v.i) proc parseConfigValue(ctx: var ConfigParser; x: var int64; v: TomlValue; k: string) = - typeCheck(v, VALUE_INTEGER, k) + typeCheck(v, tvtInteger, k) x = v.i proc parseConfigValue(ctx: var ConfigParser; x: var Option[ColorMode]; v: TomlValue; k: string) = - typeCheck(v, VALUE_STRING, k) + typeCheck(v, tvtString, k) case v.s of "auto": x = none(ColorMode) - of "monochrome": x = some(MONOCHROME) - of "ansi": x = some(ANSI) - of "8bit", "eight-bit": x = some(EIGHT_BIT) - of "24bit", "true-color": x = some(TRUE_COLOR) + of "monochrome": x = some(cmMonochrome) + of "ansi": x = some(cmANSI) + of "8bit", "eight-bit": x = some(cmEightBit) + of "24bit", "true-color": x = some(cmTrueColor) else: raise newException(ValueError, "unknown color mode '" & v.s & "' for key " & k) proc parseConfigValue(ctx: var ConfigParser; x: var Option[FormatMode]; v: TomlValue; k: string) = - typeCheck(v, {VALUE_STRING, VALUE_ARRAY}, k) - if v.vt == VALUE_STRING and v.s == "auto": + typeCheck(v, {tvtString, tvtArray}, k) + if v.t == tvtString and v.s == "auto": x = none(FormatMode) else: var y: FormatMode @@ -453,11 +453,11 @@ proc parseConfigValue(ctx: var ConfigParser; x: var Option[FormatMode]; proc parseConfigValue(ctx: var ConfigParser; x: var FormatMode; v: TomlValue; k: string) = - typeCheck(v, VALUE_ARRAY, k) + typeCheck(v, tvtArray, k) for i in 0 ..< v.a.len: let kk = k & "[" & $i & "]" let vv = v.a[i] - typeCheck(vv, VALUE_STRING, kk) + typeCheck(vv, tvtString, kk) case vv.s of "bold": x.incl(ffBold) of "italic": x.incl(ffItalic) @@ -472,7 +472,7 @@ proc parseConfigValue(ctx: var ConfigParser; x: var FormatMode; v: TomlValue; proc parseConfigValue(ctx: var ConfigParser; x: var RGBAColor; v: TomlValue; k: string) = - typeCheck(v, VALUE_STRING, k) + typeCheck(v, tvtString, k) let c = parseRGBAColor(v.s) if c.isNone: raise newException(ValueError, "invalid color '" & v.s & @@ -481,7 +481,7 @@ proc parseConfigValue(ctx: var ConfigParser; x: var RGBAColor; v: TomlValue; proc parseConfigValue(ctx: var ConfigParser; x: var RGBColor; v: TomlValue; k: string) = - typeCheck(v, VALUE_STRING, k) + typeCheck(v, tvtString, k) let c = parseLegacyColor(v.s) if c.isNone: raise newException(ValueError, "invalid color '" & v.s & @@ -490,7 +490,7 @@ proc parseConfigValue(ctx: var ConfigParser; x: var RGBColor; v: TomlValue; proc parseConfigValue[T](ctx: var ConfigParser; x: var Option[T]; v: TomlValue; k: string) = - if v.vt == VALUE_STRING and v.s == "auto": + if v.t == tvtString and v.s == "auto": x = none(T) else: var y: T @@ -499,9 +499,9 @@ proc parseConfigValue[T](ctx: var ConfigParser; x: var Option[T]; v: TomlValue; proc parseConfigValue(ctx: var ConfigParser; x: var ActionMap; v: TomlValue; k: string) = - typeCheck(v, VALUE_TABLE, k) + typeCheck(v, tvtTable, k) for kk, vv in v: - typeCheck(vv, VALUE_STRING, k & "[" & kk & "]") + typeCheck(vv, tvtString, k & "[" & kk & "]") let rk = getRealKey(kk) var buf: string for i in 0 ..< rk.high: @@ -511,7 +511,7 @@ proc parseConfigValue(ctx: var ConfigParser; x: var ActionMap; v: TomlValue; proc parseConfigValue[T: enum](ctx: var ConfigParser; x: var T; v: TomlValue; k: string) = - typeCheck(v, VALUE_STRING, k) + typeCheck(v, tvtString, k) let e = strictParseEnum[T](v.s) if e.isNone: raise newException(ValueError, "invalid value '" & v.s & "' for key " & k) @@ -519,8 +519,8 @@ proc parseConfigValue[T: enum](ctx: var ConfigParser; x: var T; v: TomlValue; proc parseConfigValue[T](ctx: var ConfigParser; x: var set[T]; v: TomlValue; k: string) = - typeCheck(v, {VALUE_STRING, VALUE_ARRAY}, k) - if v.vt == VALUE_STRING: + typeCheck(v, {tvtString, tvtArray}, k) + if v.t == tvtString: var xx: T xx.parseConfigValue(v, k) x = {xx} @@ -534,7 +534,7 @@ proc parseConfigValue[T](ctx: var ConfigParser; x: var set[T]; v: TomlValue; proc parseConfigValue(ctx: var ConfigParser; x: var CSSConfig; v: TomlValue; k: string) = - typeCheck(v, VALUE_TABLE, k) + typeCheck(v, tvtTable, k) for kk, vv in v: let kkk = if k != "": k & "." & kk @@ -542,21 +542,21 @@ proc parseConfigValue(ctx: var ConfigParser; x: var CSSConfig; v: TomlValue; kk case kk of "include": - typeCheck(vv, {VALUE_STRING, VALUE_ARRAY}, kkk) - case vv.vt - of VALUE_STRING: + typeCheck(vv, {tvtString, tvtArray}, kkk) + case vv.t + of tvtString: x.stylesheet &= readUserStylesheet(ctx.dir, vv.s) - of VALUE_ARRAY: + of tvtArray: for child in vv.a: x.stylesheet &= readUserStylesheet(ctx.dir, vv.s) else: discard of "inline": - typeCheck(vv, VALUE_STRING, kkk) + typeCheck(vv, tvtString, kkk) x.stylesheet &= vv.s proc parseConfigValue(ctx: var ConfigParser; x: var Regex; v: TomlValue; k: string) = - typeCheck(v, VALUE_STRING, k) + typeCheck(v, tvtString, k) let y = compileMatchRegex(v.s) if y.isNone: raise newException(ValueError, "invalid regex " & k & " : " & y.error) @@ -564,7 +564,7 @@ proc parseConfigValue(ctx: var ConfigParser; x: var Regex; v: TomlValue; proc parseConfigValue(ctx: var ConfigParser; x: var URL; v: TomlValue; k: string) = - typeCheck(v, VALUE_STRING, k) + typeCheck(v, tvtString, k) let y = parseURL(v.s) if y.isNone: raise newException(ValueError, "invalid URL " & k) @@ -572,13 +572,13 @@ proc parseConfigValue(ctx: var ConfigParser; x: var URL; v: TomlValue; proc parseConfigValue[T](ctx: var ConfigParser; x: var proc(x: T): JSResult[T]; v: TomlValue; k: string) = - typeCheck(v, VALUE_STRING, k) + typeCheck(v, tvtString, k) let fun = ctx.config.jsctx.eval(v.s, "<config>", JS_EVAL_TYPE_GLOBAL) x = getJSFunction[T, T](ctx.config.jsctx, fun) proc parseConfigValue(ctx: var ConfigParser; x: var ChaPathResolved; v: TomlValue; k: string) = - typeCheck(v, VALUE_STRING, k) + typeCheck(v, tvtString, k) let y = ChaPath(v.s).unquote() if y.isErr: raise newException(ValueError, y.error) diff --git a/src/config/toml.nim b/src/config/toml.nim index fc32f387..5f471b27 100644 --- a/src/config/toml.nim +++ b/src/config/toml.nim @@ -9,14 +9,14 @@ import types/opt import utils/twtstr type - ValueType* = enum - VALUE_STRING = "string" - VALUE_INTEGER = "integer" - VALUE_FLOAT = "float" - VALUE_BOOLEAN = "boolean" - VALUE_DATE_TIME = "datetime" - VALUE_TABLE = "table" - VALUE_ARRAY = "array" + TomlValueType* = enum + tvtString = "string" + tvtInteger = "integer" + tvtFloat = "float" + tvtBoolean = "boolean" + tvtDateTime = "datetime" + tvtTable = "table" + tvtArray = "array" TomlError = string @@ -34,20 +34,20 @@ type laxnames: bool TomlValue* = ref object - case vt*: ValueType - of VALUE_STRING: + case t*: TomlValueType + of tvtString: s*: string - of VALUE_INTEGER: + of tvtInteger: i*: int64 - of VALUE_FLOAT: + of tvtFloat: f*: float64 - of VALUE_BOOLEAN: + of tvtBoolean: b*: bool - of VALUE_TABLE: - t*: TomlTable - of VALUE_DATE_TIME: + of tvtTable: + tab*: TomlTable + of tvtDateTime: dt*: DateTime - of VALUE_ARRAY: + of tvtArray: a*: seq[TomlValue] ad*: bool @@ -88,25 +88,25 @@ func `$`(tab: TomlTable): string = result &= '\n' func `$`*(val: TomlValue): string = - case val.vt - of VALUE_STRING: + case val.t + of tvtString: result = "\"" for c in val.s: if c == '"': result &= '\\' result &= c result &= '"' - of VALUE_INTEGER: + of tvtInteger: result = $val.i - of VALUE_FLOAT: + of tvtFloat: result = $val.f - of VALUE_BOOLEAN: + of tvtBoolean: result = $val.b - of VALUE_TABLE: + of tvtTable: result = $val.t - of VALUE_DATE_TIME: + of tvtDateTime: result = $val.dt - of VALUE_ARRAY: + of tvtArray: #TODO if ad table array probably result = "[" for it in val.a: @@ -115,14 +115,14 @@ func `$`*(val: TomlValue): string = result &= ']' func `[]`*(val: TomlValue, key: string): TomlValue = - return val.t.map[key] + return val.tab.map[key] iterator pairs*(val: TomlValue): (string, TomlValue) {.inline.} = - for k, v in val.t.map: + for k, v in val.tab.map: yield (k, v) func contains*(val: TomlValue, key: string): bool = - return key in val.t.map + return key in val.tab.map const ValidBare = AsciiAlphaNumeric + {'-', '_'} @@ -253,17 +253,17 @@ proc flushLine(state: var TomlParser): Err[TomlError] = while i < keys.len - 1: if keys[i] in table.map: let node = table.map[keys[i]] - if node.vt == VALUE_TABLE: - table = node.t - elif node.vt == VALUE_ARRAY: + if node.t == tvtTable: + table = node.tab + elif node.t == tvtArray: assert state.tarray - table = node.a[^1].t + table = node.a[^1].tab else: let s = keys.join('.') return state.err("re-definition of node " & s) else: let node = TomlTable() - table.map[keys[i]] = TomlValue(vt: VALUE_TABLE, t: node) + table.map[keys[i]] = TomlValue(t: tvtTable, tab: node) table = node inc i if keys[i] in table.map: @@ -357,23 +357,23 @@ proc consumeNoState(state: var TomlParser): Result[bool, TomlError] = var node = state.root for i in 0 ..< table.key.high: if table.key[i] in node.map: - node = node.map[table.key[i]].t + node = node.map[table.key[i]].tab else: let t2 = TomlTable() - node.map[table.key[i]] = TomlValue(vt: VALUE_TABLE, t: t2) + node.map[table.key[i]] = TomlValue(t: tvtTable, tab: t2) node = t2 if table.key[^1] in node.map: var last = node.map[table.key[^1]] - if last.vt != VALUE_ARRAY: + if last.t != tvtArray: let key = table.key.join('.') return state.err("re-definition of node " & key & - " as table array (was " & $last.vt & ")") + " as table array (was " & $last.t & ")") last.ad = true - let val = TomlValue(vt: VALUE_TABLE, t: table) + let val = TomlValue(t: tvtTable, tab: table) last.a.add(val) else: - let val = TomlValue(vt: VALUE_TABLE, t: table) - let last = TomlValue(vt: VALUE_ARRAY, a: @[val]) + let val = TomlValue(t: tvtTable, tab: table) + let last = TomlValue(t: tvtArray, a: @[val]) node.map[table.key[^1]] = last state.currkey = table.key state.node = table @@ -441,27 +441,27 @@ proc consumeNumber(state: var TomlParser, c: char): TomlResult = let val = parseInt64(repr) if not val.isSome: return state.err("invalid integer") - return ok(TomlValue(vt: VALUE_INTEGER, i: val.get)) + return ok(TomlValue(t: tvtInteger, i: val.get)) of NUMBER_HEX: try: let val = parseHexInt(repr) - return ok(TomlValue(vt: VALUE_INTEGER, i: val)) + return ok(TomlValue(t: tvtInteger, i: val)) except ValueError: return state.err("invalid hexadecimal number") of NUMBER_OCT: try: let val = parseOctInt(repr) - return ok(TomlValue(vt: VALUE_INTEGER, i:val)) + return ok(TomlValue(t: tvtInteger, i: val)) except ValueError: return state.err("invalid octal number") of NUMBER_FLOAT: let val = parseFloat64(repr) - return ok(TomlValue(vt: VALUE_FLOAT, f: val)) + return ok(TomlValue(t: tvtFloat, f: val)) proc consumeValue(state: var TomlParser): TomlResult proc consumeArray(state: var TomlParser): TomlResult = - var res = TomlValue(vt: VALUE_ARRAY) + var res = TomlValue(t: tvtArray) var val: TomlValue while state.has(): let c = state.consume() @@ -485,7 +485,7 @@ proc consumeArray(state: var TomlParser): TomlResult = return err("unexpected end of file") proc consumeInlineTable(state: var TomlParser): TomlResult = - let res = TomlValue(vt: VALUE_TABLE, t: TomlTable()) + let res = TomlValue(t: tvtTable, tab: TomlTable()) var key: seq[string] var haskey: bool var val: TomlValue @@ -501,14 +501,14 @@ proc consumeInlineTable(state: var TomlParser): TomlResult = return state.err("missing key") if val == nil: return state.err("comma without element") - var table = res.t + var table = res.tab for i in 0 ..< key.high: let k = key[i] if k in table.map: return state.err("invalid re-definition of key " & k) else: let node = TomlTable() - table.map[k] = TomlValue(vt: VALUE_TABLE, t: node) + table.map[k] = TomlValue(t: tvtTable, tab: node) table = node let k = key[^1] if k in table.map: @@ -536,7 +536,7 @@ proc consumeValue(state: var TomlParser): TomlResult = case c of '"', '\'': let s = ?state.consumeString(c) - return ok(TomlValue(vt: VALUE_STRING, s: s)) + return ok(TomlValue(t: tvtString, s: s)) of ' ', '\t': discard of '\n': return state.err("newline without value") @@ -552,17 +552,17 @@ proc consumeValue(state: var TomlParser): TomlResult = elif c in ValidBare: let s = ?state.consumeBare(c) if s == "true": - return ok(TomlValue(vt: VALUE_BOOLEAN, b: true)) + return ok(TomlValue(t: tvtBoolean, b: true)) elif s == "false": - return ok(TomlValue(vt: VALUE_BOOLEAN, b: false)) + return ok(TomlValue(t: tvtBoolean, b: false)) elif state.laxnames: - return ok(TomlValue(vt: VALUE_STRING, s: s)) + return ok(TomlValue(t: tvtString, s: s)) else: return state.err("invalid token: " & s) else: return state.err("invalid character in value: " & c) if state.laxnames: - return ok(TomlValue(vt: VALUE_STRING, s: "")) + return ok(TomlValue(t: tvtString, s: "")) return state.err("unexpected end of file") proc parseToml*(inputStream: Stream, filename = "<input>", laxnames = false): @@ -591,4 +591,4 @@ proc parseToml*(inputStream: Stream, filename = "<input>", laxnames = false): else: return state.err("invalid character after value: " & c) ?state.flushLine() inputStream.close() - return ok(TomlValue(vt: VALUE_TABLE, t: state.root)) + return ok(TomlValue(t: tvtTable, tab: state.root)) |