Software /
code /
prosody
Comparison
util/serialization.lua @ 9569:0bc399953c27
util.serialization: Rename non-fatal fallback handler for clarity
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 27 Oct 2018 12:54:57 +0200 |
parent | 9568:69f589c888e7 |
child | 9570:ce403b6470f8 |
comparison
equal
deleted
inserted
replaced
9568:69f589c888e7 | 9569:0bc399953c27 |
---|---|
36 | 36 |
37 local function fatal_error(obj, why) | 37 local function fatal_error(obj, why) |
38 error("Can't serialize "..type(obj) .. (why and ": ".. why or "")); | 38 error("Can't serialize "..type(obj) .. (why and ": ".. why or "")); |
39 end | 39 end |
40 | 40 |
41 local function default_fallback(x, why) | 41 local function nonfatal_fallback(x, why) |
42 return s_format("nil --[[%s: %s]]", type(x), why or "fail"); | 42 return s_format("nil --[[%s: %s]]", type(x), why or "fail"); |
43 end | 43 end |
44 | 44 |
45 local string_escapes = { | 45 local string_escapes = { |
46 ['\a'] = [[\a]]; ['\b'] = [[\b]]; | 46 ['\a'] = [[\a]]; ['\b'] = [[\b]]; |
82 -- presets | 82 -- presets |
83 if opt.preset == "debug" then | 83 if opt.preset == "debug" then |
84 opt.preset = "oneline"; | 84 opt.preset = "oneline"; |
85 opt.freeze = true; | 85 opt.freeze = true; |
86 opt.fatal = false; | 86 opt.fatal = false; |
87 opt.fallback = default_fallback; | 87 opt.fallback = nonfatal_fallback; |
88 opt.unquoted = true; | 88 opt.unquoted = true; |
89 end | 89 end |
90 if opt.preset == "oneline" then | 90 if opt.preset == "oneline" then |
91 opt.indentwith = opt.indentwith or ""; | 91 opt.indentwith = opt.indentwith or ""; |
92 opt.itemstart = opt.itemstart or " "; | 92 opt.itemstart = opt.itemstart or " "; |
98 opt.itemlast = opt.itemlast or ""; | 98 opt.itemlast = opt.itemlast or ""; |
99 opt.equals = opt.equals or "="; | 99 opt.equals = opt.equals or "="; |
100 opt.unquoted = true; | 100 opt.unquoted = true; |
101 end | 101 end |
102 | 102 |
103 local fallback = opt.fallback or opt.fatal == false and default_fallback or fatal_error; | 103 local fallback = opt.fallback or opt.fatal == false and nonfatal_fallback or fatal_error; |
104 | 104 |
105 local function ser(v) | 105 local function ser(v) |
106 return (types[type(v)] or fallback)(v); | 106 return (types[type(v)] or fallback)(v); |
107 end | 107 end |
108 | 108 |