Skip to content

Instantly share code, notes, and snippets.

View matthewjberger's full-sized avatar

Matthew J. Berger matthewjberger

View GitHub Profile
fd dirname | fzf | cd
@matthewjberger
matthewjberger / main.rs
Last active June 27, 2023 06:11
custom hashmap serialization in rust
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::collections::HashMap;
#[derive(Debug, PartialEq, Serialize, Deserialize)]
enum UnitEnum {
Variant1,
Variant2,
Variant3,
}
theme = "gruvbox_dark_hard"
[editor]
line-number = "relative"
true-color = true
cursorline = true
gutters = ["diagnostics", "spacer", "line-numbers", "spacer", "diff"]
[keys.normal."space"]
s = ":write-all"
@matthewjberger
matthewjberger / Cargo.toml
Created May 26, 2023 01:14
Put a pixel on the screen with Rust!
[package]
name = "pixel"
version = "0.1.0"
edition = "2021"
[dependencies]
sdl2 = { version = "0.34.5", features = ["bundled"] }
//! Utility for comparing and identifying differences between two structures using serde serialization.
//!
//! The `Diff` struct provides a utility for comparing and identifying differences between two structures in Rust using serde serialization.
//! It allows you to compare two instances of a structure and obtain a list of paths to the differing fields or elements.
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
struct Diff;
#[derive(Debug, PartialEq, Eq)]
enum MyEnum {
Variant1(i32),
Variant2(String),
}
fn main() {
let a = MyEnum::Variant1(5);
let b = MyEnum::Variant1(5);
assert_eq!(a.eq(&b), true);
@matthewjberger
matthewjberger / main.rs
Last active May 17, 2023 15:24
Watch a file with debounced file events using notify and tokio in Rust
// Cargo.toml
//
// notify = "5.1.0"
// notify-debouncer-mini = "0.2.1"
// tokio = { version = "1.28.1", features = ["full"] }
use notify::RecursiveMode;
use notify_debouncer_mini::{new_debouncer, DebouncedEventKind};
use std::{error::Error, path::Path, time::Duration};
use tokio::time::sleep;
@matthewjberger
matthewjberger / main.rs
Created May 17, 2023 15:04
Watch a file with notify and tokio in rust
// Cargo.toml
// notify = "5.1.0"
// tokio = { version = "1.28.1", features = ["full"] }
use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher};
use std::{error::Error, time::Duration};
use tokio::{
sync::mpsc::{self, UnboundedReceiver},
time::sleep,
};
@matthewjberger
matthewjberger / check_features.md
Last active May 16, 2023 20:13
An awk command for getting the features section of a rust project's Cargo.toml

Check if a rust app has the "derive" feature listed in its Cargo.toml

if awk '/\[features\]/{p=1; next} /^\[/{p=0} p && /^derive =/{found=1; exit} END{exit !found}' Cargo.toml; then
  # The crate has the feature we searched for
fi
  • /\[features\]/{p=1; next}: When the line contains [features], set the variable p to 1 to start processing the subsequent lines. Then, skip to the next line.
  • /^\[/{p=0}: When the line starts with [, set the variable p to 0 to stop processing the lines.
@matthewjberger
matthewjberger / main.rs
Created May 15, 2023 06:47
Display JSON with syntax highlighting using crossterm in Rust
// Cargo.toml
//
// crossterm = "0.26.1"
// serde_json = "1.0.70"
use crossterm::{
style::{Color, Print, ResetColor, SetForegroundColor},
terminal::{disable_raw_mode, enable_raw_mode},
ExecutableCommand, Result,
};