#!/usr/bin/env ruby require 'stringio' target = File.join(Dir.pwd, "build.rs") File.unlink(target) if File.exist?(target) domains = `curl https://data.iana.org/TLD/tlds-alpha-by-domain.txt | sed '1d'` .split("\n").map(&:strip).sort StringIO.open do |s| s.puts "// Auto generated #{Time.now}" s.puts <<-DOC extern crate phf_codegen; use std::env; use std::fs::File; use std::io::{BufWriter, Write}; use std::path::Path; fn main() { let path = Path::new(&env::var("OUT_DIR").unwrap()).join("codegen.rs"); let mut file = BufWriter::new(File::create(&path).unwrap()); writeln!( &mut file, "/// Top level domain static map, list is obtained from iana.org." ) .unwrap(); writeln!( &mut file, "pub static TLD: phf::Set<&'static str> = {};", phf_codegen::Set::new() DOC domains.each do |domain| s.puts %[ .entry("#{domain.downcase}")] end s.puts %[ .build() ).unwrap(); ] s.puts '}' IO.write(target, s.string) end puts `rustfmt #{target}`