precis_tools/generators/
unicode_version.rs1use crate::error::Error;
2use crate::generators::CodeGen;
3use lazy_static::lazy_static;
4use regex::Regex;
5use std::fs::File;
6use std::io::Write;
7
8pub struct UnicodeVersionGen {
11 version: String,
12}
13
14impl UnicodeVersionGen {
15 pub fn new(version: &str) -> Self {
17 Self {
18 version: String::from(version),
19 }
20 }
21}
22
23fn get_version(version: &str) -> Result<(u64, u64, u64), Error> {
24 lazy_static! {
25 static ref VERSION_RX: Regex = Regex::new(r"([0-9]+).([0-9]+).([0-9]+)").unwrap();
26 }
27
28 let caps = match VERSION_RX.captures(version) {
29 Some(c) => c,
30 None => return err!("Failed to find version in '{}'", version),
31 };
32
33 let capture_to_num = |n| {
34 caps.get(n)
35 .unwrap()
36 .as_str()
37 .parse::<u64>()
38 .map_err(|_e| Error {
39 mesg: format!("Failed to parse version from '{:?}'", version),
40 line: Some(0),
41 path: None,
42 })
43 };
44 let major = capture_to_num(1)?;
45 let minor = capture_to_num(2)?;
46 let patch = capture_to_num(3)?;
47
48 Ok((major, minor, patch))
49}
50
51impl CodeGen for UnicodeVersionGen {
52 fn generate_code(&mut self, file: &mut File) -> Result<(), Error> {
53 let (major, minor, patch) = get_version(&self.version)?;
54 writeln!(
55 file,
56 "/// The [Unicode version](http://www.unicode.org/versions) of data"
57 )?;
58 writeln!(
59 file,
60 "pub const UNICODE_VERSION: (u8, u8, u8) = ({}, {}, {});",
61 major, minor, patch
62 )?;
63 Ok(writeln!(file)?)
64 }
65}