comments: var block + interface-less error

This commit is contained in:
Clyde Bazile
2023-04-24 10:51:43 -04:00
committed by Clyde Bazile
parent 8568b1b20d
commit 2372f55064
4 changed files with 16 additions and 13 deletions

View File

@@ -4,22 +4,25 @@ import (
"errors" "errors"
) )
type Error interface { var (
Error() string ErrUnimplemented = NewError("not implemented")
} ErrBusy = NewError("device or resource busy")
ErrNoDevice = NewError("no such device")
var ErrUnimplemented Error = errors.New("not implemented") )
var ErrBusy Error = errors.New("device or resource busy")
var ErrNoDevice Error = errors.New("no such device")
type errorString struct { type errorString struct {
s string s string
} }
func NewError(text string) Error { func NewError(text string) error {
return &errorString{text} return &errorString{text}
} }
func IsError(err error) bool {
var target *errorString
return errors.As(err, &target)
}
func (e *errorString) Error() string { func (e *errorString) Error() string {
return e.s return e.s
} }

View File

@@ -353,7 +353,7 @@ func (c *camera) Properties() []prop.Media {
return properties return properties
} }
func (c *camera) IsAvailable() (bool, availability.Error) { func (c *camera) IsAvailable() (bool, error) {
var err error var err error
// close the opened file descriptor as quickly as possible and in all cases, including panics // close the opened file descriptor as quickly as possible and in all cases, including panics

View File

@@ -48,10 +48,10 @@ type Driver interface {
} }
type AvailabilityAdapter interface { type AvailabilityAdapter interface {
IsAvailable() (bool, availability.Error) IsAvailable() (bool, error)
} }
func IsAvailable(d Driver) (bool, availability.Error) { func IsAvailable(d Driver) (bool, error) {
if aa, ok := d.(AvailabilityAdapter); ok { if aa, ok := d.(AvailabilityAdapter); ok {
return aa.IsAvailable() return aa.IsAvailable()
} }

View File

@@ -56,7 +56,7 @@ type adapterWrapper struct {
id string id string
info Info info Info
state State state State
isAvailable func() (bool, availability.Error) isAvailable func() (bool, error)
} }
func (w *adapterWrapper) ID() string { func (w *adapterWrapper) ID() string {
@@ -113,7 +113,7 @@ func (w *adapterWrapper) AudioRecord(p prop.Media) (r audio.Reader, err error) {
return return
} }
func (w *adapterWrapper) IsAvailable() (bool, availability.Error) { func (w *adapterWrapper) IsAvailable() (bool, error) {
if w.isAvailable == nil { if w.isAvailable == nil {
return false, availability.ErrUnimplemented return false, availability.ErrUnimplemented
} }