Skip to content

Instantly share code, notes, and snippets.

View J-Cake's full-sized avatar
🥚
Egg

Jacob Schneider J-Cake

🥚
Egg
View GitHub Profile
@J-Cake
J-Cake / README.md
Last active March 26, 2024 14:10
Rust Error macro

JCake's Error Macro

I like the ? syntax a lot because it is very readable and delegates errors nicely but it often becomes unwieldly because ? only coerces between like-typed error types. If you define a type which contains each error type and the necessary impls for automatic type coercion, your life becomes much easier. This macro does that. You define a list of all possible error types, and this macro spits out a system which you need only import.

Here a code example:

pub mod error;
@J-Cake
J-Cake / display.rs
Created December 29, 2022 02:47
Simple Display Example
use std::fs::{File, OpenOptions};
use std::io;
use std::os::fd::{AsRawFd, FromRawFd, RawFd};
use syscall::PAGE_SIZE;
fn main() {
let file = syscall::open(std::env::args().skip(1).next().unwrap(), syscall::O_CLOEXEC | syscall::O_NONBLOCK | syscall::O_RDWR)
.map(|socket| {
unsafe { File::from_raw_fd(socket as RawFd) }
}).unwrap();
@J-Cake
J-Cake / README.md
Last active July 15, 2023 01:10
Render text into raqote using `rusttype`, because raqote text rendering is ... lacking

Rendering text

Easy peasy....

mod text;

let mut ctx = raqote::DrawTarget::new(720, 480);
let buffer = text::text(&text::TextProps { text: format!("Hello World"), size: 14 });
<!DOCTYPE html>
<html>
<head>
<style>
pre, pre code {
font-family: sans-serif;
}
</style>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>
@J-Cake
J-Cake / DBTable.ts
Last active January 7, 2022 04:23
Very simple table system which can use files on disk
import fs from 'fs';
import cp from 'child_process';
import readline from 'readline';
export enum Status {
Success = 0,
Unauthorised,
Nonexistent,
Invalid
@J-Cake
J-Cake / argparser.ts
Last active December 1, 2021 12:30
A simple but capable arg parser for CLI interfaces.
type Option<Parser extends (arg: string) => any> =
({ long?: string, short: string } | { long: string, short?: string })
& { format?: Parser, default?: ReturnType<Parser>, description?: string };
export type Parameters<Names extends { [name: string]: any }> = { [name in keyof Names]: Option<Names[name]> };
export type Options<Main, Names extends { [name: string]: any }, Config extends Parameters<Names>> = { default: Main }
& { [Parameter in keyof Config]: Config[Parameter] extends Option<(arg: string) => infer Type> ? Type : boolean };
export default function parse<Main, Names extends { [name: string]: any }>(parameters: Parameters<Names>, main?: (arg: string) => Main): (args: string[]) => Options<Main, Names, typeof parameters> {
@J-Cake
J-Cake / promisify.ts
Last active October 16, 2021 08:12
the `promisify` utility function converts a callback-style function to a promise-style one. It does this elegantly. Below is a generic TypeScript implementation
export function toPromise<ReturnType extends any[], ArgTypes extends any[]>(callbackFn: (...args: [...ArgTypes, (err: any, ...data: ReturnType) => void]) => void): (...args: ArgTypes) => Promise<ReturnType> {
return function (...args: ArgTypes): Promise<ReturnType> {
return new Promise(function (resolve) {
callbackFn(...args, (err: any, ...data: ReturnType) => resolve(data));
});
};
}
import sys
import getopt
opts, args = getopt.getopt(sys.argv[1:], '', ['mask=', 'ip='])
mask_input = list(filter(lambda x: '--mask' in x, opts))
ip_address_input = list(filter(lambda x: '--ip' in x, opts))
if len(mask_input) > 0:
maskNum = max(min(int(mask_input[-1][1]), 32), 0)
@J-Cake
J-Cake / index.html
Created February 20, 2020 01:31
Show how to reference css files
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<link rel="stylesheet" href="/style.css"/>
<style>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>
:root {
--background: black;