Skip to content

Instantly share code, notes, and snippets.

@jess-sol
jess-sol / counter_nonce.rs
Created April 10, 2024 17:36
Example of a Ring counting NonceSequence. Counter uses 11 bytes so the last byte can be used to identify the last chunk (STREAM construct) - https://github.com/C2SP/C2SP/blob/main/age.md#payload
use std::mem::size_of;
use ring::aead::{self, Nonce, NonceSequence};
/// The u128 is treated as an 11 byte counter, and will panic on overflow
const COUNTER_BYTES: usize = 11;
const COUNTER_MAX_SIZE: u128 = 1 << (8 * COUNTER_BYTES);
pub(crate) struct CounterNonceSequence(u128);
impl NonceSequence for CounterNonceSequence {
@jess-sol
jess-sol / main.rs
Created September 17, 2023 01:03
Contextual parsing of bare strings in Chumsky
use std::collections::BTreeMap;
use chumsky::{error::Error, prelude::*};
fn main() {
assert_eq!(parser().parse("hello!").into_result(), Ok(Value::String("hello!".to_string())));
assert_eq!(
parser().parse("{a: b, c: {d: e}}").into_result(),
Ok(Value::Map(BTreeMap::from([
(Value::String("a".to_string()), Value::String("b".to_string())),
@jess-sol
jess-sol / Cargo.toml
Created September 6, 2023 21:31
Chumsky example parser for nested maps similar to yaml
[package]
name = "rust_example"
version = "0.1.0"
edition = "2021"
[dependencies]
chumsky = { version = "1.0.0-alpha.4", git = "https://github.com/jess-sol/chumsky.git", branch = "topic/map_with_ctx", features = [
"serde",
] }
@jess-sol
jess-sol / digit-permutations.rs
Created January 14, 2022 16:40
Use Johnson-Trotter algorithm to compute all permutations of the digits of a number in Rust
#[derive(Clone, Debug)]
enum Direction { Left, Right }
#[derive(Clone, Debug)]
struct DirectionalDigit(Direction, usize);
impl std::fmt::Display for DirectionalDigit {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self.0 {
Direction::Left => write!(f, "<{}", self.1),
Direction::Right => write!(f, "{}>", self.1),
@jess-sol
jess-sol / minikube-dnsmasq
Created June 23, 2021 14:31
Configures dnsmasq to refer to DNS server running in minikube when ingress-dns addon is enabled for a profile
#!/usr/bin/env bash
printf '' | sudo tee /etc/dnsmasq.d/minikube.conf
while IFS= read -r profile; do
cat <<EOF | sudo tee -a /etc/dnsmasq.d/minikube.conf
server=/$profile.invalid/$(minikube ip --profile "$profile")
address=/$profile.invalid/$(minikube ip --profile "$profile")
EOF
done < <(minikube profile list -o json | jq -r '.valid[] | select(.Config.Addons."ingress-dns") | .Name')
#!/usr/bin/env bash
ACCESS_TOKEN=$(< token)
function ddns {
local record="domain.tld"
local domain="exiting.a.record"
ip="$(curl -s http://checkip.amazonaws.com/)"
record_id="$(get_id_for_name "$record" "$domain")"
import pyspark
from pyspark import SparkConf, SparkContext
from pyspark.sql import SparkSession, Window, Row
from datetime import datetime
import pyspark.sql.types as T
BUCKETS = 5
sc = pyspark.SparkContext('local[*]')
spark = SparkSession(sc)
@jess-sol
jess-sol / install-pyenv-centos-7
Created August 21, 2019 16:17
Install pyenv on CentOS 7 for a specific user with dependencies to manage installed versions of Python
sudo yum install \
git gcc zlib-devel bzip2-devel readline-devel \
sqlite-devel openssl-devel libffi-devel
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
cat <<'EOF' >> ~/.bash_profile
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
@jess-sol
jess-sol / vagrant-arch-linux-setup.md
Last active August 12, 2019 21:29
Set up Vagrant on Arch Linux
sudo pacman -S \
  qemu libvirt virt-manager \
  ebtables nfs-utils dnsmasq vagrant

vagrant plugin install vagrant-libvirt vagrant-mutate
vagrant plugin install vagrant-hostmanager vagrant-cachier # Optional

sudo usermod -a -G libvirt `whoami`
sudo systemctl restart libvirtd # Make sure ebtables installation takes effect
@jess-sol
jess-sol / rust-subprocess.rb
Created July 1, 2016 15:51
Communicate with Subprocess using Rust. Uses subprocess_communicate and encoding crates. Demonstrates encoding/decoding UTF8 strings.
extern crate subprocess_communicate;
extern crate encoding;
use std::path::Path;
use std::process::{Command, Stdio, Child};
use self::subprocess_communicate::subprocess_communicate;
use self::encoding::all::UTF_8;
use self::encoding::{Encoding, EncoderTrap, DecoderTrap};