Skip to content

Instantly share code, notes, and snippets.

View OliverBrotchie's full-sized avatar
🪴

Oliver Brotchie OliverBrotchie

🪴
View GitHub Profile
@OliverBrotchie
OliverBrotchie / result.ts
Created August 31, 2022 17:04
New implementation of Optionals
/* eslint-disable no-prototype-builtins */
/**
* A Rust-like Result class.
*
* Note: Please use either Ok or Err to construct Results.
*
* @example
* ```
* function divide(left: number, right: number): Result<number, Error> {
@OliverBrotchie
OliverBrotchie / cargo.toml
Last active August 12, 2022 21:52
Super simple chat server.
[package]
name = "chat-server"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tokio = { version = "1.20.1", features = ["full"] }
@OliverBrotchie
OliverBrotchie / env.ts
Created July 18, 2022 12:24
Enviroment Config
/**
* @file Environment variables & configuration utilities.
*/
import { EnvError } from "../errors/envError";
import dotenv from "dotenv";
dotenv.config();
export enum Env {
/**
* Similar to a standard Error, but with a custom message and status code field.
* @param status The status code of the error.
* @param message The message of the error.
*/
export class HttpError extends Error {
name = "HttpError";
status: number;
constructor(status: number, message: string) {
@OliverBrotchie
OliverBrotchie / example.ts
Created July 18, 2022 12:18
A L/R Result class
function divide(left: number, right: number): Result<number, Error> {
if (right === 0) return Err("Divided by zero");
return Ok(left / right);
}
class DivisionError extends Error {
constructor(message: string) {
super(message);
}
@OliverBrotchie
OliverBrotchie / handler.rs
Last active October 12, 2021 20:16
Async connection handler
use actix_web::http::header::HeaderMap;
use chrono::{DateTime, Utc};
use std::collections::HashMap;
use std::sync::Mutex;
#[derive(Debug, Clone)]
pub struct Fingerprint {
properties: Vec<(String, String)>,
fonts: Vec<String>,
headers: HeaderMap,
@OliverBrotchie
OliverBrotchie / OptionOneMatch.ts
Last active September 27, 2021 12:14
Match example
const None = Symbol("None");
const DefaultErr = Symbol("Err");
type Option<T> = ((s: (val: T) => T) => T) | typeof None;
type Some<T> = (val: T) => Option<T>;
const Some: <T>(val: T) => Option<T> = (val) => (s) => s(val);
type Result<T, E> = (o: (val: T) => E, s: (val: symbol) => E) => E;
type Err<T> = T;
const Err: <A, B>(val: symbol) => Result<A, B> = (val: symbol) => (_, g) =>
@OliverBrotchie
OliverBrotchie / OptionOne.ts
Created September 25, 2021 23:12
Types for Optionals
const None = Symbol("None");
const DefaultErr = Symbol("Err");
type Option<T> = ((s: (val: T) => T) => T) | typeof None;
type Some<T> = (val: T) => Option<T>;
const Some: <T>(val: T) => Option<T> = (val) => (s) => s(val);
type Result<T, E> = (o: (val: T) => E, s: (val: symbol) => E) => E;
type Err<T> = T;
const Err: <A, B>(val: symbol) => Result<A, B> = (val: symbol) => (_, g) =>
@OliverBrotchie
OliverBrotchie / autocompile.sh
Created June 3, 2021 04:06
Autocompile and display a Groff document
/bin/sh
# Automatically compiles and views a pdf from Groff. [Requires Zathura, entr]
if [ $# -eq 0 ] || [ "$1" == "-h" ] ; then
echo "Usage: \$0 \$1"
echo " \$0 The name of the ms document you wish to autocompile."
echo " \$1 The name of the output."
exit
fi
@OliverBrotchie
OliverBrotchie / Chromium_Bug.sass
Last active April 24, 2021 15:06
@media (height: x) does not work on android.
body
&::after
content: 'Error, this should not display!'
@for $i from 0px through 2161px
@media (height: $i)
&::after
content: 'Height: #{$i}'