Skip to content

Instantly share code, notes, and snippets.

View DrPlantabyte's full-sized avatar

Chris Hall DrPlantabyte

  • Sydney, Australia
View GitHub Profile
use std::sync::{Arc, Mutex};
use tokio;
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Starting...");
println!("===== Direct Execution ===");
println!("Got {} from Task 1", task1());
println!("Got {} from Task 2", task2());
println!("Got {} from Task 3", task3());
@DrPlantabyte
DrPlantabyte / better_errors.rs
Created July 3, 2023 02:32
HOW TO IMPLEMENT JAVA-STYLE EXCEPTIONS IN IDIOMATIC RUST
/*
The contents of this file are Public Domain. You may use as you wish.
HOW TO IMPLEMENT JAVA-STYLE EXCEPTIONS IN IDIOMATIC RUST
(aka "How to have errors that don't suck")
Here we define a macro that creates a struct implementing the Error trait and automatically captures
and displays the backtrace unless running in release mode (and even then, you can specify
`RUST_BACKTRACE=1` to get the backtraces in release)
@DrPlantabyte
DrPlantabyte / build.rs
Last active October 15, 2022 21:16
build.rs for automatic version increment on build
use std::error::Error;
// NOTE: requires `regex = "1.6"` under [build-dependencies] in Cargo.toml
fn main() {
println!("Running build.rs...");
match run() {
Ok(_) => println!("...Success!"),
Err(e) => {
println!("...Failure!");
eprintln!("{:?}", e);
@DrPlantabyte
DrPlantabyte / jpms-new-project.py
Last active December 14, 2021 02:41
Python script to create a JPMS project without fancy build tools
#!/usr/bin/python3
import os, sys
from os import path
def main():
print("\tCreating new Java JPMS module project...")
project_name = input("Project name: ").strip()
if path.exists(project_name):
print("Abort: '%s' already exists. ")
exit(1)
@DrPlantabyte
DrPlantabyte / FXMLControllerCreator.java
Last active March 21, 2018 02:41
Generate a JavaFX controller class from a given FXML file.
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.swing.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
@DrPlantabyte
DrPlantabyte / netbeans-project-gradle.build
Created July 20, 2017 11:23
A simple gradle build file that you can drop into your typical Netbeans java project to build the jar using gradle from the command-line.
apply plugin: 'java'
libsDirName = '../dist'
docsDirName = '../dist'
sourceSets {
main {
java {
srcDirs = ['src']
}
}
}
@DrPlantabyte
DrPlantabyte / CCH-java-app_build.gradle
Last active July 20, 2017 11:54
This is my default build.gradle file, which has a createProject task that makes all of the folders so you don't have to guess the convention and even intializes it with a blank JavaFX app
// buildscript blocks MUST go before plugins
buildscript {
dependencies {
classpath group: 'de.dynamicfiles.projects.gradle.plugins', name: 'javafx-gradle-plugin', version: '8.8.2'
}
repositories {
mavenLocal()
mavenCentral()
}
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
@DrPlantabyte
DrPlantabyte / TreasureChests.java
Created April 18, 2016 00:03
Example of the Steam Advantage mod generating loot tables that will be loaded by the Additional Loot Tables to add new mod items to dungeon treasure chests
package cyano.steamadvantage.init;
import cyano.steamadvantage.SteamAdvantage;
import net.minecraftforge.fml.common.FMLLog;
import org.apache.logging.log4j.Level;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
@DrPlantabyte
DrPlantabyte / NativePythonTest.java
Last active February 18, 2016 19:29
Run scientific python scripts in java app via process spawning
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;