Last active
April 26, 2021 19:52
-
-
Save L0ry-git/888dcfe3e8e43ac5b21ecfcaa4566429 to your computer and use it in GitHub Desktop.
Joke tool that allows you to write and compile Rust code as folder names. The syntax for every folder name is <line number> <code> (for example "11 let a = 1;"). See lines 46-55 for the other syntax rules. Crate name is supposed to be "folderustc". Usage: folderustc <source folder> <rust compiler args>.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::{ | |
fs::{self, DirEntry, File}, | |
io::{prelude::*, Error, Result}, | |
process::Command | |
}; | |
//Default temp file name | |
const FILE_NAME: &'static str = "source.rs"; | |
fn main() -> Result<()> { | |
//Gets args and skips the first one (executable name) | |
let mut args = std::env::args().skip(1); | |
//Gets the source folder from remaining args | |
let code_folder = args | |
.next() | |
.expect("Please specify a source directory!"); | |
//Reads the source code | |
let source_code = fs::read_dir(&code_folder) | |
.expect("Invalid source directory!") | |
.fold(String::new(), process_folder_name); | |
//Temp file path | |
let temp_path: String = String::from(FILE_NAME); | |
//Creates the file and write the source code | |
let mut file = File::create(&temp_path).unwrap(); | |
file.write_all(source_code.as_bytes())?; | |
//Compiles | |
Command::new("rustc") | |
.arg(FILE_NAME) | |
.args(args) | |
.output() | |
.expect("Compilation failed!"); | |
//Removes temp file and returns | |
fs::remove_file(&temp_path)?; | |
Ok(()) | |
} | |
//Gets called for each subfolder corresponding to a line | |
fn process_folder_name(data: String, current: std::result::Result<DirEntry, Error>) -> String { | |
let fname = current.unwrap().file_name(); | |
let raw = fname.to_str().expect("IO Error!"); | |
//some replaces are applied here because folder names do not support some characters | |
data + "\n" + &raw[(raw.find(" ").unwrap_or(0) + 1)..] | |
.replace("''", "\"") //use '' as double quotes | |
.replace("@", "*") //use @ as raw pointer thing | |
.replace("§", "?") //use § as question mark | |
.replace("°-", ">") //use °- as greater than | |
.replace("-°", "<") //use -° as less than | |
.replace("-°°", "\\") //use -°° as left bar | |
.replace("°°-", "/") //use °°- as right bar | |
.replace("-°-", "|") //use -°- as middle bar | |
.replace("^^", ":") //use ^^ as colon | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
uwu this is so cursed