error.ha (1327B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use fmt; 5 use strings; 6 7 // All possible errors returned from [[date]]. 8 export type error = !(insufficient | invalid | zfunresolved | parsefail); 9 10 // Converts an [[error]] into a human-friendly string. The result may be 11 // statically allocated. 12 export fn strerror(err: error) const str = { 13 match (err) { 14 case let lack: insufficient => 15 static let buf: [92]u8 = [0...]; 16 return strings::rtrim(fmt::bsprint(buf, 17 "Insufficient date information, could not calculate:", 18 if (lack & insufficient::LOCALITY: u8 == 0) "" else 19 "locality", 20 if (lack & insufficient::DAYDATE: u8 == 0) "" else 21 "daydate", 22 if (lack & insufficient::DAYTIME: u8 == 0) "" else 23 "time-of-day", 24 if (lack & insufficient::ZOFF: u8 == 0) "" else 25 "zone-offset", 26 )); 27 case invalid => 28 return "Invalid date information"; 29 case let lap: zfunresolved => 30 if (lap) { 31 return "Failed to resolve zone-offset in a timezone transition overlap"; 32 } else { 33 return "Failed to resolve zone-offset in a timezone transition gap"; 34 }; 35 case let pf: parsefail => 36 const (bi, rn) = pf; 37 def FMTMSG = "Date parsing failure for layout rune '{}' at byteindex {}"; 38 static let buf: [len(FMTMSG) + 3]u8 = [0...]; 39 return fmt::bsprintf(buf, FMTMSG, rn, bi); 40 }; 41 };