Skip to content

Instantly share code, notes, and snippets.

@juliarose
Created March 28, 2022 19:42
Show Gist options
  • Save juliarose/9e6d580de85e782c48d6974411998018 to your computer and use it in GitHub Desktop.
Save juliarose/9e6d580de85e782c48d6974411998018 to your computer and use it in GitHub Desktop.
Rust macro generator for protobuf messages
const fs = require('fs');
const filepath = process.argv[2];
const filename = filepath.match(/([a-z_0-9]+)\.rs$/)[1];
const data = fs.readFileSync(filepath, 'utf8');
const structs = data
.split('\n')
.filter(line => /^pub struct /.test(line))
.map(line => line.replace(/(pub struct | \{)/g, ''));
const tab = ' ';
// console.log(`${filename}::{`);
// console.log(tab + structs.join(`,\n${tab}`));
// console.log('}');
structs.forEach(struct => console.log(`gc_msg!(${filename}::${struct});`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment