From 4970a59bfa06def2b6a42baf86cb8dfa57606b0a Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 20 Nov 2024 21:20:18 +0900 Subject: [PATCH] save --- .../gno.land/p/demo/pausable/pausable.gno | 3 ++ examples/gno.land/p/demo/users/users.gno | 6 --- examples/gno.land/r/gnoland/users/errors.gno | 6 +++ examples/gno.land/r/gnoland/users/render.gno | 1 + examples/gno.land/r/gnoland/users/users.gno | 42 +++++++++++++++++++ 5 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 examples/gno.land/r/gnoland/users/errors.gno create mode 100644 examples/gno.land/r/gnoland/users/render.gno create mode 100644 examples/gno.land/r/gnoland/users/users.gno diff --git a/examples/gno.land/p/demo/pausable/pausable.gno b/examples/gno.land/p/demo/pausable/pausable.gno index e9cce63c1e3..5c2aa69d863 100644 --- a/examples/gno.land/p/demo/pausable/pausable.gno +++ b/examples/gno.land/p/demo/pausable/pausable.gno @@ -1,6 +1,7 @@ package pausable import ( + "errors" "std" "gno.land/p/demo/ownable" @@ -11,6 +12,8 @@ type Pausable struct { paused bool } +var ErrPaused = errors.New("realm is currently paused") + // New returns a new Pausable struct with non-paused state as default func New() *Pausable { return &Pausable{ diff --git a/examples/gno.land/p/demo/users/users.gno b/examples/gno.land/p/demo/users/users.gno index 29c46712448..8c09b0854fa 100644 --- a/examples/gno.land/p/demo/users/users.gno +++ b/examples/gno.land/p/demo/users/users.gno @@ -16,8 +16,6 @@ type User struct { Profile string Number int Block int - Invites int - Inviter std.Address } func (u *User) Render() string { @@ -34,10 +32,6 @@ func (u *User) Render() string { out += ufmt.Sprintf("- ID #%d\n\n", u.Number) out += ufmt.Sprintf("- Address: %s\n\n", u.Address.String()) out += ufmt.Sprintf("- Registered at block #%d\n\n", u.Block) - out += ufmt.Sprintf("- Leftover invites: %d\n\n", u.Invites) - if u.Inviter != "" { - out += ufmt.Sprintf("- Inviter: %s\n\n", u.Inviter.String()) - } return out } diff --git a/examples/gno.land/r/gnoland/users/errors.gno b/examples/gno.land/r/gnoland/users/errors.gno new file mode 100644 index 00000000000..08f8bd095a5 --- /dev/null +++ b/examples/gno.land/r/gnoland/users/errors.gno @@ -0,0 +1,6 @@ +package users + +import "errors" + +var ErrInvalidUsername = errors.New("users: invalid username") +var ErrAlreadyExists = errors.New("users: username already exists") diff --git a/examples/gno.land/r/gnoland/users/render.gno b/examples/gno.land/r/gnoland/users/render.gno new file mode 100644 index 00000000000..82abcb9fccb --- /dev/null +++ b/examples/gno.land/r/gnoland/users/render.gno @@ -0,0 +1 @@ +package users diff --git a/examples/gno.land/r/gnoland/users/users.gno b/examples/gno.land/r/gnoland/users/users.gno new file mode 100644 index 00000000000..c84ed97da6a --- /dev/null +++ b/examples/gno.land/r/gnoland/users/users.gno @@ -0,0 +1,42 @@ +package users + +import ( + "github.com/gnolang/gno/examples/gno.land/p/demo/avl" + "github.com/gnolang/gno/examples/gno.land/p/demo/ownable" + p "github.com/gnolang/gno/examples/gno.land/p/demo/pausable" + "github.com/gnolang/gno/examples/gno.land/p/demo/seqid" + "regexp" +) + +const reValidUsername = "^[a-zA-Z]{3}[a-zA-Z0-9]*[0-9]{3}$\n" + +var ( + store avl.Tree // + idCounter seqid.ID + admin = ownable.NewWithAddress("") // @moul /// switch to dao managed down the line + pausable = p.NewFromOwnable(admin) // + reUsername = regexp.MustCompile(reValidUsername) +) + +func init() { + // PreRegister() +} + +func Register(username, bio string) error { + if pausable.IsPaused() { + return p.ErrPaused + } + + if store.Has(username) { + return ErrAlreadyExists + } + + matched, _ := regexp.MatchString(reValidUsername, username) + if !matched { + return ErrInvalidUsername + } + + store.Set(username) + + return nil +}