2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2019-06-23 05:21:30 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-06-22 21:20:28 +03:00
|
|
|
module builtin
|
2020-12-19 01:27:35 +03:00
|
|
|
|
2021-03-12 21:05:05 +03:00
|
|
|
// IError holds information about an error instance
|
|
|
|
pub interface IError {
|
2021-03-14 13:17:31 +03:00
|
|
|
msg string
|
2021-03-12 21:05:05 +03:00
|
|
|
code int
|
2020-05-31 13:57:26 +03:00
|
|
|
}
|
2019-12-19 23:52:45 +03:00
|
|
|
|
2021-03-12 21:05:05 +03:00
|
|
|
// Error is the default implementation of IError, that is returned by e.g. `error()`
|
|
|
|
pub struct Error {
|
|
|
|
pub:
|
|
|
|
msg string
|
|
|
|
code int
|
2020-05-31 13:57:26 +03:00
|
|
|
}
|
|
|
|
|
2021-03-20 02:55:16 +03:00
|
|
|
pub fn (err IError) str() string {
|
|
|
|
return match err {
|
|
|
|
None__ { 'none' }
|
|
|
|
Error { err.msg }
|
|
|
|
else { '$err.type_name(): $err.msg' }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 20:13:50 +03:00
|
|
|
const none__ = IError(&None__{})
|
|
|
|
|
|
|
|
struct None__ {
|
|
|
|
msg string
|
|
|
|
code int
|
2021-03-12 21:05:05 +03:00
|
|
|
}
|
|
|
|
|
2021-03-14 13:17:31 +03:00
|
|
|
fn (_ None__) str() string {
|
|
|
|
return 'none'
|
|
|
|
}
|
2021-03-13 20:13:50 +03:00
|
|
|
|
2021-05-18 20:02:56 +03:00
|
|
|
[if trace_error]
|
|
|
|
fn trace_error(x string) {
|
|
|
|
eprintln('> ${@FN} | $x')
|
|
|
|
}
|
|
|
|
|
2021-03-14 03:54:46 +03:00
|
|
|
// error returns a default error instance containing the error given in `message`.
|
|
|
|
// Example: `if ouch { return error('an error occurred') }`
|
|
|
|
[inline]
|
|
|
|
pub fn error(message string) IError {
|
2021-05-18 20:02:56 +03:00
|
|
|
trace_error(message)
|
2021-03-14 03:54:46 +03:00
|
|
|
return &Error{
|
|
|
|
msg: message
|
|
|
|
}
|
|
|
|
}
|
2021-03-12 21:05:05 +03:00
|
|
|
|
2021-03-14 03:54:46 +03:00
|
|
|
// error_with_code returns a default error instance containing the given `message` and error `code`.
|
|
|
|
// `if ouch { return error_with_code('an error occurred', 1) }`
|
|
|
|
[inline]
|
|
|
|
pub fn error_with_code(message string, code int) IError {
|
2021-05-18 20:02:56 +03:00
|
|
|
trace_error('$message | code: $code')
|
2021-03-14 13:17:31 +03:00
|
|
|
return &Error{
|
2021-03-14 03:54:46 +03:00
|
|
|
msg: message
|
|
|
|
code: code
|
|
|
|
}
|
|
|
|
}
|
2021-02-22 19:44:15 +03:00
|
|
|
|
2021-03-14 03:54:46 +03:00
|
|
|
// Option is the base of V's internal optional return system.
|
|
|
|
struct Option {
|
2021-02-22 19:44:15 +03:00
|
|
|
state byte
|
2021-03-14 03:54:46 +03:00
|
|
|
err IError = none__
|
2021-02-22 19:44:15 +03:00
|
|
|
// Data is trailing after err
|
|
|
|
// and is not included in here but in the
|
2021-03-14 03:54:46 +03:00
|
|
|
// derived Option_xxx types
|
2021-02-22 19:44:15 +03:00
|
|
|
}
|
|
|
|
|
2021-03-14 03:54:46 +03:00
|
|
|
fn opt_ok(data voidptr, mut option Option, size int) {
|
2021-02-28 22:24:29 +03:00
|
|
|
unsafe {
|
2021-03-14 03:54:46 +03:00
|
|
|
*option = Option{}
|
2021-02-28 22:24:29 +03:00
|
|
|
// use err to get the end of OptionBase and then memcpy into it
|
2021-04-05 21:21:46 +03:00
|
|
|
C.memcpy(&byte(&option.err) + sizeof(IError), data, size)
|
2021-02-22 19:44:15 +03:00
|
|
|
}
|
|
|
|
}
|
2021-03-17 13:31:36 +03:00
|
|
|
|
|
|
|
[unsafe]
|
|
|
|
pub fn (e &Error) free() {
|
|
|
|
unsafe { e.msg.free() }
|
|
|
|
}
|
|
|
|
|
|
|
|
[unsafe]
|
|
|
|
pub fn (n &None__) free() {
|
|
|
|
unsafe { n.msg.free() }
|
|
|
|
}
|
|
|
|
|
|
|
|
[typedef]
|
|
|
|
struct C.IError {
|
|
|
|
_object voidptr
|
|
|
|
}
|
|
|
|
|
|
|
|
[unsafe]
|
|
|
|
pub fn (ie &IError) free() {
|
|
|
|
unsafe {
|
|
|
|
ie.msg.free()
|
|
|
|
cie := &C.IError(ie)
|
2021-03-20 16:16:36 +03:00
|
|
|
free(cie._object)
|
2021-03-17 13:31:36 +03:00
|
|
|
}
|
|
|
|
}
|