Skip to content

Instantly share code, notes, and snippets.

View jmsdnns's full-sized avatar

Jms Dnns jmsdnns

View GitHub Profile
@jmsdnns
jmsdnns / lib.rs
Created April 24, 2025 18:12
Using Rust library to parse yaml from Ocaml
use serde::{Deserialize, Serialize};
use serde_yaml::Value;
use std::ffi::{CStr, CString};
use std::os::raw::c_char;
#[repr(C)]
pub struct KeyValue {
pub key: *const c_char,
pub value: *const c_char,
}
@jmsdnns
jmsdnns / README.md
Created April 23, 2025 18:37
Using Rust library in Ocaml

Example: Calling Rust libs from Ocaml

Create rust project and an ocaml directory.

cargo new rahst --lib
mkdir camls

Then replace everything in rahst/src/lib.rs with the following:

@jmsdnns
jmsdnns / beez_plan_parser_2.rs
Last active March 31, 2025 15:47
I used the nom crate in the previous gist. This uses winnow instead. People don't use nom anymore, apparently. lol
use std::fs;
use winnow::Result;
use winnow::prelude::*;
use winnow::{
ascii::{line_ending, space0, space1},
combinator::{opt, repeat, terminated},
token::take_while,
};
#[derive(Debug, PartialEq)]
@jmsdnns
jmsdnns / beez_plan_parser.rs
Created March 30, 2025 15:12
my first try at building a parser for execution plans in killabeez
use nom::{
IResult, Parser,
branch::alt,
bytes::complete::{tag, take_until},
character::complete::{char, multispace0, space0},
multi::many0,
sequence::delimited,
};
pub mod errors;
@jmsdnns
jmsdnns / tf_file_type.ml
Last active March 7, 2025 14:04
pattern matching on terraform json variations
(* val type_ : Yojson.Safe.t -> [ `Plan | `State | `Unknown ] *)
let type_ json =
let module Plan = struct
type t = {
terraform_version : string;
planned_values : Yojson.Safe.t;
}
[@@deriving of_yojson { strict = false }]
end in
@jmsdnns
jmsdnns / create_sg.rs
Created March 1, 2025 19:48
Two ways of saying the same thing: rust & hcl
pub async fn create_security_group(client: &Client, ac: &AppConfig) -> Result<String, Error> {
let vpc_id = ac.vpc_id.as_ref().unwrap();
let tag_specifications = create_tag_spec(ac, ResourceType::SecurityGroup);
let ssh_cidr_block = ac.ssh_cidr_block.as_ref().unwrap();
println!("[create_security_group] vpc_id {:?}", vpc_id);
println!("[create_security_group] tags {:?}", tag_specifications);
println!("[create_security_group] ssh cidr {:?}", ssh_cidr_block);
let response = client
.create_security_group()
@jmsdnns
jmsdnns / jmap.ml
Created February 3, 2025 00:14
A port scanner in Ocaml. Rudimentary form done to learn how Ocaml does networking.
module Scanner = struct
(* attempt to open a port and return an option *)
let open_port host port timeout = fun () ->
let open Lwt.Infix in
let sockaddr = Unix.ADDR_INET (Unix.inet_addr_of_string host, port) in
let socket = Lwt_unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
Lwt.catch
(* Port is open *)
(fun () ->
@jmsdnns
jmsdnns / jsonlite.ml
Created January 26, 2025 07:15
Querying a sqlite DB from Ocaml. JSON is stored in a column and selects are done on JSON values.
module UserModel = struct
module DB = Sqlite3
module JSON = Yojson.Basic
let create_table db =
let create_sql =
"CREATE TABLE IF NOT EXISTS users (" ^
"id INTEGER PRIMARY KEY, " ^
"name TEXT, " ^
"age INTEGER, " ^
@jmsdnns
jmsdnns / downloader.ml
Last active January 17, 2025 20:24
My first Ocaml program. It downloads two files at the same time using LWT.
(*
compile with:
`ocamlfind ocamlopt -thread -o downloader -linkpkg -package lwt,cohttp-lwt-unix downloader.ml`
*)
module Downloader = struct
(* download a single file *)
let dl_file url filename =
let open Lwt.Infix in
@jmsdnns
jmsdnns / main.go
Last active November 28, 2024 01:45
Simple demonstration of streaming chunks to a bubbletea TUI
package main
import (
"fmt"
"io"
"log"
"os"
"time"
"github.com/charmbracelet/bubbles/viewport"