Wyn Standard Library

The Wyn standard library provides 120+ functions across 16 modules for common programming tasks.

Core Modules

io - Input/Output Operations

File operations, reading, writing, and stream handling.

Functions: read_file, write_file, append_file, file_exists, create_dir, list_dir, delete_file, copy_file, move_file, file_size, file_info, stdin_read, stdout_write, stderr_write, flush, temp_file

import io

fn main() {
    // Write to file
    io.write_file("hello.txt", "Hello, World!")
    
    // Read from file
    match io.read_file("hello.txt") {
        ok(content) => print("File content: " + content)
        err(msg) => print("Error: " + msg)
    }
    
    // Check if file exists
    if io.file_exists("hello.txt") {
        print("File exists!")
    }
}

net - Network Programming

TCP/UDP sockets, HTTP client/server functionality.

Functions: server_create, server_accept, server_read_request, server_send_response, server_send_json, client_connect, client_send, client_recv, parse_url, parse_path, parse_headers, encode_url, decode_url, resolve_host, get_local_ip, close, set_timeout, bind, listen, accept, send, recv, connect

import net

fn main() {
    // Simple HTTP server
    const server = net.server_create(8080)
    print("Server running on :8080")
    
    while true {
        const client = net.server_accept(server)
        const request = net.server_read_request(client)
        net.server_send_response(client, 200, "Hello from Wyn!")
        net.close(client)
    }
}

json - JSON Processing

Parse and serialize JSON data with type safety.

Functions: parse, stringify, object, array, get, put, as_str, as_int, as_float, as_bool, as_array, as_object, to_string

import json

fn main() {
    // Parse JSON
    const data = json.parse("{\"name\": \"Alice\", \"age\": 30}")
    match data {
        ok(obj) => {
            const name = obj.get("name").as_str()
            const age = obj.get("age").as_int()
            print("Name: " + name + ", Age: " + age.to_str())
        }
        err(msg) => print("Parse error: " + msg)
    }
    
    // Create JSON
    const user = json.object()
        .put("name", "Bob")
        .put("age", 25)
        .to_string()
    print("JSON: " + user)
}

time - Date and Time

Time operations, formatting, and duration handling.

Functions: now, since, sleep, sleep_ms, format, parse, add_duration, sub_duration, year, month, day, hour, minute, second, weekday, unix_timestamp, from_unix

import time

fn main() {
    const start = time.now()
    
    // Sleep for 1 second
    time.sleep(1)
    
    const elapsed = time.since(start)
    print("Elapsed: " + elapsed.to_str() + "ms")
    
    // Format current time
    const formatted = time.format(time.now(), "%Y-%m-%d %H:%M:%S")
    print("Current time: " + formatted)
}

math - Mathematical Functions

Mathematical operations, constants, and utilities.

Functions: abs, min, max, pow, sqrt, sin, cos, tan, asin, acos, atan, atan2, log, log10, exp, ceil, floor, round, pi, e, random, random_range

import math

fn main() {
    print("Pi: " + math.pi().to_str())
    print("Square root of 16: " + math.sqrt(16.0).to_str())
    print("2^8 = " + math.pow(2.0, 8.0).to_str())
    
    const random_num = math.random_range(1, 100)
    print("Random number: " + random_num.to_str())
}

Data Structures

collections - Data Structures

HashMap, Set, and other collection types.

Functions: hashmap_new, hashmap_put, hashmap_get, hashmap_remove, hashmap_contains, hashmap_keys, hashmap_values, hashmap_size, set_new, set_add, set_remove, set_contains, set_union, set_intersection

import collections

fn main() {
    // HashMap example
    const map = collections.hashmap_new()
    collections.hashmap_put(map, "name", "Alice")
    collections.hashmap_put(map, "age", "30")
    
    const name = collections.hashmap_get(map, "name")
    print("Name: " + name)
    
    // Set example
    const set = collections.set_new()
    collections.set_add(set, "apple")
    collections.set_add(set, "banana")
    
    if collections.set_contains(set, "apple") {
        print("Set contains apple")
    }
}

array - Array Operations

Array manipulation and functional programming utilities.

Functions: push, pop, len, get, set, slice, concat, reverse, sort, filter, map, reduce, find, contains, index_of

fn main() {
    let mut numbers = [1, 2, 3, 4, 5]
    
    // Add element
    numbers.push(6)
    
    // Filter even numbers
    const evens = numbers.filter(|n| n % 2 == 0)
    
    // Map to strings
    const strings = evens.map(|n| n.to_str())
    
    print("Even numbers: " + strings.join(", "))
}

Utilities

string - String Operations

String manipulation and processing functions.

Functions: len, upper, lower, trim, split, join, replace, contains, starts_with, ends_with, substring, index_of, last_index_of, repeat, reverse, is_empty

fn main() {
    const text = "  Hello, World!  "
    
    print("Length: " + text.len().to_str())
    print("Trimmed: '" + text.trim() + "'")
    print("Uppercase: " + text.upper())
    
    const words = text.trim().split(", ")
    print("Words: " + words.join(" | "))
}

regex - Regular Expressions

Pattern matching and text processing.

Functions: compile, match, find, find_all, replace, replace_all, split, is_match

import regex

fn main() {
    const pattern = regex.compile(r"\d+")
    const text = "I have 42 apples and 13 oranges"
    
    const numbers = regex.find_all(pattern, text)
    for num in numbers {
        print("Found number: " + num)
    }
}

crypto - Cryptography

Hashing, encryption, and security functions.

Functions: sha256, md5, hmac, random_bytes, base64_encode, base64_decode, hash_password, verify_password

import crypto

fn main() {
    const data = "Hello, World!"
    const hash = crypto.sha256(data)
    print("SHA256: " + hash)
    
    const encoded = crypto.base64_encode(data)
    print("Base64: " + encoded)
}

System Integration

os - Operating System

Environment variables, process management, and system information.

Functions: getenv, setenv, args, exit, exec, spawn_process, wait, kill, getcwd, chdir, hostname, username, platform, arch, pid, ppid

import os

fn main() {
    print("Platform: " + os.platform())
    print("Architecture: " + os.arch())
    print("Current directory: " + os.getcwd())
    
    const args = os.args()
    print("Command line arguments: " + args.len().to_str())
    
    const home = os.getenv("HOME")
    print("Home directory: " + home)
}

log - Logging

Structured logging with different levels.

Functions: debug, info, warn, error, fatal, set_level, set_output, with_fields, new_logger

import log

fn main() {
    log.info("Application started")
    log.debug("Debug information")
    log.warn("This is a warning")
    log.error("An error occurred")
}

Advanced Modules

http - HTTP Client

HTTP client for making web requests.

Functions: get, post, put, delete, request, set_header, set_timeout, follow_redirects, parse_response

import http

fn main() {
    match http.get("https://api.github.com/users/octocat") {
        ok(response) => {
            print("Status: " + response.status.to_str())
            print("Body: " + response.body)
        }
        err(msg) => print("HTTP error: " + msg)
    }
}

encoding - Data Encoding

Various encoding and decoding utilities.

Functions: base64_encode, base64_decode, url_encode, url_decode, hex_encode, hex_decode, utf8_encode, utf8_decode

compress - Compression

Data compression and decompression.

Functions: gzip_compress, gzip_decompress, deflate, inflate, zip_create, zip_extract, tar_create

For complete documentation and more examples, visit the GitHub repository.