Skip to content

Instantly share code, notes, and snippets.

View jmsdnns's full-sized avatar

Jms Dnns jmsdnns

View GitHub Profile
@jmsdnns
jmsdnns / lexing_parsing.rs
Created November 14, 2024 05:45
A simple example of an EBNF parser written in Rust, without support from external crates.
// <expression ::= <term> { ("+" | "-") <term> }
// <term> ::= <factor> { ("*" | "/") <factor> }
// <factor> ::= <number> | "(" <expression> ")"
// <number> ::= <digit> { <digit> }
// <digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
use std::str::FromStr;
#[derive(Debug, PartialEq, Clone)]
pub enum Token {
@jmsdnns
jmsdnns / read_chunks.rs
Created October 9, 2024 22:28
Reading a file into chunks using Tokio's File tools
// TOTAL CHUNKS: 5
// - chunk: 5242880
// - chunk: 5242880
// - chunk: 5242880
// - chunk: 5242880
// - chunk: 1974756
use tokio::{fs::File, io, io::AsyncReadExt, io::AsyncSeekExt};
pub async fn read_chunk(file_path: &str, start: u64, end: u64) -> io::Result<Vec<u8>> {
@jmsdnns
jmsdnns / paths_w_inquire.rs
Created October 6, 2024 18:21
Creating a file path verifying type with Rust's inquire crate
use inquire::{validator::Validation, CustomType};
use std::path::{self, Path};
fn full_path(p: String) -> String {
path::absolute(p.as_str())
.unwrap()
.canonicalize()
.unwrap()
.to_str()
.unwrap()
@jmsdnns
jmsdnns / midwest.rs
Last active November 18, 2024 01:53
Trying to help my friends in the midwest feel comfortable with Rust
use std::fs::File;
enum ComeWith<T, E> {
YouBetcha(T),
Ope(E),
}
impl<T, E> From<std::result::Result<T, E>> for ComeWith<T, E> {
fn from(r: std::result::Result<T, E>) -> Self {
match r {
@jmsdnns
jmsdnns / isitfluent.py
Created September 17, 2024 06:13
Fluent interfaces technically return self or this from each chained function. Pandas returns new instances of the same type. Technically, that isn't fluent. I suspect it is meant to be indistinguishable for optimization reasons.
#!/usr/bin/env python
import pandas as pd
def getDataframe(url_table, ind):
df = pd.read_html(url_table)[ind]
return df
@jmsdnns
jmsdnns / rust_parse.log
Last active September 3, 2024 21:43
Pretty printed the way Rust's parse_macro_input! parses a simple struct. Handy to know the full thing for pattern matching.
// rust has pretty print support for printing structures. this is how rust's proc_macros
// parse the following struct:
//
// pub struct Book {
// id: u64,
// title: String,
// pages: u64,
// author: String,
// }
//
@jmsdnns
jmsdnns / simple_orm.rs
Last active August 31, 2024 06:21
Trying out Rust's proc macros by building a very basic ORM
use proc_macro::TokenStream;
use quote::quote;
use syn::{
parse_macro_input, Data::Struct, DataStruct, DeriveInput, Field, Fields::Named, FieldsNamed,
Path, Type, TypePath,
};
#[derive(Debug)]
struct DBModel {
name: String,
@jmsdnns
jmsdnns / vms.justfile
Created August 30, 2024 17:16
A simple config for managing groups of VMs using lima and just
default:
just -l
lsvm:
limactl ls
mkvm id:
limactl create \
--name="{{id}}" \
--vm-type=vz \
@jmsdnns
jmsdnns / async_password_hashing.rs
Last active September 15, 2024 09:12
An async password hasher that puts hashing in a background thread where it can safely block the cpu
// Output:
// HASH: "$argon2id$v=19$m=19456,t=2,p=1$vecn7S39OC1B7Ei0ZBfl1A$q94uWmUFrtuUhERwWCLkCGSO+CRZwGEWUTmJSO8wr0c"
// ACCESS VERIFIED
use anyhow::{Context, Result};
use argon2::{password_hash::SaltString, Argon2, PasswordHash};
async fn hash_password(password: String) -> Result<String> {
tokio::task::spawn_blocking(move || -> Result<String> {
let salt = SaltString::generate(rand::thread_rng());
@jmsdnns
jmsdnns / compose.go
Created August 29, 2024 12:24
basic example of how Go uses composition instead of inheritence
package main
import "fmt"
type Polygon struct {
Sides int
}
func (p *Polygon) NSides() int {
return p.Sides