use std::io::{Read, Write}; use std::path::PathBuf; use std::{env, fs}; fn main() { let mut word_width = 0; let mut max_width_word = ""; let mut fh_chars = fs::File::open("./dict/chars.csv").unwrap(); let mut chars_buf = String::new(); fh_chars.read_to_string(&mut chars_buf).unwrap(); let mut fh_words = fs::File::open("./dict/words.csv").unwrap(); let mut words_buf = String::new(); fh_words.read_to_string(&mut words_buf).unwrap(); let mut output = "// Generated file // 单词最大长度 pub const WORD_LEN: usize = {{4}}; // 字表 pub const CHARS: &'static [&'static str] = &[ " .to_string(); let chars_list = chars_buf.split("\n"); for line in chars_list { let l = line.trim(); if l == "" { continue; } output.push_str(format!("\t\"{}\",\n", l).as_str()); } output.push_str( "]; // 单词表 pub const WORDS: &'static [&'static str] = &[ ", ); let words_list = words_buf.split("\n"); for line in words_list { let l = line.trim(); if l == "" { continue; } let segs: Vec<&str> = l.split(",").collect(); if segs.len() > 0 { let key = segs.get(0).unwrap(); let width = key.chars().count(); if width > word_width { word_width = width; max_width_word = *key } } output.push_str(format!("\t\"{}\",\n", l).as_str()); } println!("{}-{}", word_width, max_width_word); output.push_str( " ];", ); output = output.replace("{{4}}", format!("{}", word_width).as_str()); let mut outfile = fs::File::create("./src/vars.rs").unwrap(); outfile.write_all(output.as_bytes()).unwrap(); gen_header(); } fn gen_header() { let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); let package_name = env::var("CARGO_PKG_NAME").unwrap(); let output_file = PathBuf::from("./src") .join("pinyin.h") .display() .to_string(); let config = cbindgen::Config::from_file("cbindgen.toml").expect("Failed to load cbindgen.toml"); println!("{}", crate_dir); cbindgen::Builder::new() .with_crate(&crate_dir) .with_config(config) .generate() .expect("msg") .write_to_file(&output_file); }