File: errors.ha

package info (click to toggle)
hare 0.25.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,948 kB
  • sloc: asm: 1,264; makefile: 123; sh: 114; lisp: 101
file content (68 lines) | stat: -rw-r--r-- 1,570 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use errors;
use io;

// Encountered invalid DER encoded data.
export type invalid = !void;

// Unexpected data format.
export type badformat = !void;

// Premature EOF.
export type truncated = !void;

// Data does not fit into the encoder buffer.
export type overflow = !void;

type asn1error = !(invalid | badformat | overflow | truncated);

// Any error within the asn1 module.
export type error = !(...io::error | ...asn1error);


// Converts an [[error]] into a user-friendly string.
export fn strerror(e: error) str = {
	match (e) {
	case invalid =>
		return "Encountered invalid DER encoded data";
	case badformat =>
		return "Unexpected data format";
	case truncated =>
		return "Premature EOF";
	case overflow =>
		return "Data does not fit into the encoder buffer";
	case let e: io::error =>
		return io::strerror(e);
	};
};

fn wrap_err(e: error) io::error = {
	match (e) {
	case let e: io::error =>
		return e;
	case let e: asn1error =>
		static assert(size(asn1error) <= size(errors::opaque_data));
		let w = errors::opaque_ { strerror = &wrap_strerror, ... };
		let ptr = &w.data: *error;
		*ptr = e;
		return w;
	};
};

fn wrap_strerror(err: *errors::opaque_data) const str = {
	let e = err: *error;
	return strerror(*e);
};

// Unwraps an [[io::error]] returned by ASN.1 readers as an [[error]].
export fn unwrap_err(e: io::error) error = {
	match (e) {
	case let e: errors::opaque_ =>
		let ptr = &e.data: *error;
		return *ptr;
	case let e: io::error =>
		return e;
	};
};