package devices

import (
	"errors"

	"golang.org/x/net/context"
)

const (
	storerKey = "code.secondbit.org/ducky/devices.hg#Storer"
)

var (
	// ErrNoStorerSet is returned when the Context has no Storer set in it.
	ErrNoStorerSet = errors.New("storerKey not set in Context")
	// ErrStorerKeyNotStorer is returned when there's a value in the Context for storerKey, but it's not a Storer.
	ErrStorerKeyNotStorer = errors.New("the value for storerKey does not fulfill the Storer interface")
)

func getStorer(c context.Context) (Storer, error) {
	val := c.Value(storerKey)
	if val == nil {
		return nil, ErrNoStorerSet
	}
	storer, ok := val.(Storer)
	if !ok {
		return nil, ErrStorerKeyNotStorer
	}
	return storer, nil
}
