implemented event and req
This commit is contained in:
52
pkg/encoders/reason/reason.go
Normal file
52
pkg/encoders/reason/reason.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package reason
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"lol.mleku.dev/log"
|
||||
)
|
||||
|
||||
// R is the machine-readable prefix before the colon in an OK or CLOSED envelope message.
|
||||
// Below are the most common kinds that are mentioned in NIP-01.
|
||||
type R []byte
|
||||
|
||||
var (
|
||||
AuthRequired = R("auth-required")
|
||||
PoW = R("pow")
|
||||
Duplicate = R("duplicate")
|
||||
Blocked = R("blocked")
|
||||
RateLimited = R("rate-limited")
|
||||
Invalid = R("invalid")
|
||||
Error = R("error")
|
||||
Unsupported = R("unsupported")
|
||||
Restricted = R("restricted")
|
||||
)
|
||||
|
||||
// S returns the R as a string
|
||||
func (r R) S() string { return string(r) }
|
||||
|
||||
// B returns the R as a byte slice.
|
||||
func (r R) B() []byte { return r }
|
||||
|
||||
// IsPrefix returns whether a text contains the same R prefix.
|
||||
func (r R) IsPrefix(reason []byte) bool {
|
||||
return bytes.HasPrefix(
|
||||
reason, r.B(),
|
||||
)
|
||||
}
|
||||
|
||||
// F allows creation of a full R text with a printf style format.
|
||||
func (r R) F(format string, params ...any) (o []byte) {
|
||||
log.D.F(format, params...)
|
||||
return Msg(r, format, params...)
|
||||
}
|
||||
|
||||
// Msg constructs a properly formatted message with a machine-readable prefix
|
||||
// for OK and CLOSED envelopes.
|
||||
func Msg(prefix R, format string, params ...any) (o []byte) {
|
||||
if len(prefix) < 1 {
|
||||
prefix = Error
|
||||
}
|
||||
return []byte(fmt.Sprintf(prefix.S()+": "+format, params...))
|
||||
}
|
||||
Reference in New Issue
Block a user