aboutsummaryrefslogtreecommitdiffstats
path: root/gleam/gossip/src/validation.gleam
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2026-03-01 19:28:38 -0500
committerelioat <elioat@tilde.institute>2026-03-01 19:28:38 -0500
commit561101ed7b02ec0a3b102d0d96b8e902bebd82da (patch)
tree4172e17899851b9e7c8fbd2619020534614089d2 /gleam/gossip/src/validation.gleam
parent29d00607b632f3b48e7422f3fd2067c98eab5d5b (diff)
downloadtour-master.tar.gz
Diffstat (limited to 'gleam/gossip/src/validation.gleam')
-rw-r--r--gleam/gossip/src/validation.gleam95
1 files changed, 95 insertions, 0 deletions
diff --git a/gleam/gossip/src/validation.gleam b/gleam/gossip/src/validation.gleam
new file mode 100644
index 0000000..361f1d8
--- /dev/null
+++ b/gleam/gossip/src/validation.gleam
@@ -0,0 +1,95 @@
+import gleam/list
+import gleam/string
+
+/// Validate a peer name.
+///
+/// Requirements:
+/// - Non-empty after trimming whitespace
+/// - At least 1 character, at most 32 characters
+/// - Must be a valid identifier (alphanumeric + underscore)
+pub fn validate_peer_name(name: String) -> Result(String, String) {
+ let trimmed = string.trim(name)
+
+ case string.length(trimmed) {
+ 0 -> Error("Peer name cannot be empty")
+ len if len > 32 -> Error("Peer name too long (max 32 characters)")
+ _ -> {
+ case is_valid_identifier(trimmed) {
+ True -> Ok(trimmed)
+ False ->
+ Error("Peer name must contain only letters, numbers, and underscores")
+ }
+ }
+ }
+}
+
+/// Validate a remote peer address.
+///
+/// Accepts:
+/// - Peer name: "alice" (alphanumeric)
+/// - Node address: "alice@localhost" or "alice@192.168.1.1"
+pub fn validate_remote_peer(remote: String) -> Result(Nil, String) {
+ let trimmed = string.trim(remote)
+
+ case string.length(trimmed) {
+ 0 -> Error("Remote peer address cannot be empty")
+ len if len > 256 -> Error("Remote peer address too long")
+ _ -> {
+ case string.contains(trimmed, "@") {
+ True -> {
+ let parts = string.split(trimmed, "@")
+ case list.length(parts) {
+ 2 -> {
+ let peer_part = case parts {
+ [p, _] -> p
+ _ -> ""
+ }
+ case is_valid_identifier(peer_part) {
+ True -> Ok(Nil)
+ False -> Error("Invalid peer name in address: " <> peer_part)
+ }
+ }
+ _ -> Error("Invalid node address format (expected: peer@hostname)")
+ }
+ }
+ False -> {
+ case is_valid_identifier(trimmed) {
+ True -> Ok(Nil)
+ False ->
+ Error("Invalid peer name (must be alphanumeric + underscore)")
+ }
+ }
+ }
+ }
+ }
+}
+
+/// Check if a string is a valid identifier.
+/// Valid: alphanumeric (a-z, A-Z, 0-9) and underscore.
+/// Note: empty string returns True (vacuous truth from list.all).
+/// Callers that care about empty should check length separately.
+pub fn is_valid_identifier(s: String) -> Bool {
+ let chars = string.split(s, "")
+ list.all(chars, fn(c) {
+ let is_alpha =
+ string.contains(
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
+ c,
+ )
+ let is_digit = string.contains("0123456789", c)
+ let is_underscore = c == "_"
+ is_alpha || is_digit || is_underscore
+ })
+}
+
+/// Generate a unique registry ID from a peer name.
+///
+/// Uses erlang:phash2 for collision-resistant hashing.
+/// Deterministic: same name always produces the same ID.
+pub fn hash_peer_name(name: String) -> Int {
+ // phash2 returns 0..range-1; use 2^30 for full positive-int range
+ erlang_phash2(name, 1_073_741_824)
+}
+
+@external(erlang, "erlang", "phash2")
+fn erlang_phash2(term: String, range: Int) -> Int