diff --git a/Cargo.toml b/Cargo.toml index 49e2c07..0e22e24 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ description = "Port of biocommons/hgvs to Rust" license = "Apache-2.0" repository = "https://github.com/varfish-org/hgvs-rs" readme = "README.md" -rust-version = "1.77.0" +rust-version = "1.80.0" [lib] name = "hgvs" diff --git a/build.rs b/build.rs index 14789f0..2be8998 100644 --- a/build.rs +++ b/build.rs @@ -16,6 +16,7 @@ fn main() -> Result<()> { generate_codon_2bit_to_aa1_chrmt_vertebrate(&mut f)?; generate_aa1_to_aa3_str_lookup_function(&mut f)?; + generate_aa1_to_aa3_str_lookup_table(&mut f)?; generate_aa3_to_aa1_lookup_function(&mut f)?; f.flush()?; @@ -95,7 +96,10 @@ fn generate_codon_2bit_to_aa1_chrmt_vertebrate(f: &mut BufWriter) -> Resul } fn generate_aa1_to_aa3_str_lookup_function(f: &mut BufWriter) -> Result<()> { - writeln!(f, "fn _aa1_to_aa3_str(aa1: u8) -> Option<&'static str> {{")?; + writeln!( + f, + "const fn _aa1_to_aa3_str(aa1: u8) -> Option<&'static str> {{" + )?; writeln!(f, " match aa1 {{")?; for (aa3, aa1) in AA3_TO_AA1_VEC { writeln!(f, " b'{}' => Some(\"{}\"),", aa1, aa3)?; @@ -106,8 +110,25 @@ fn generate_aa1_to_aa3_str_lookup_function(f: &mut BufWriter) -> Result<() Ok(()) } +fn generate_aa1_to_aa3_str_lookup_table(f: &mut BufWriter) -> Result<()> { + let mut result = [""; 256]; + for (aa3, aa1) in AA3_TO_AA1_VEC { + result[aa1.as_bytes()[0] as usize] = aa3; + } + write!(f, "const AA1_TO_AA3_STR: [Option<&str>; 256] = [")?; + for v in result { + if v.is_empty() { + write!(f, "None, ")?; + } else { + write!(f, r##"Some("{}"), "##, v)?; + } + } + writeln!(f, "];")?; + Ok(()) +} + fn generate_aa3_to_aa1_lookup_function(f: &mut BufWriter) -> Result<()> { - writeln!(f, "fn _aa3_to_aa1(aa3: &[u8]) -> Option {{")?; + writeln!(f, "const fn _aa3_to_aa1(aa3: &[u8]) -> Option {{")?; writeln!(f, " match aa3 {{")?; for (aa3, aa1) in AA3_TO_AA1_VEC { writeln!(f, " b\"{}\" => Some(b'{}'),", aa3, aa1)?; diff --git a/src/sequences.rs b/src/sequences.rs index a44fd83..238ce2e 100644 --- a/src/sequences.rs +++ b/src/sequences.rs @@ -167,7 +167,7 @@ pub fn aa1_to_aa3(seq: &str) -> Result { let mut result = String::with_capacity(seq.len() * 3); for (i, aa1) in seq.as_bytes().iter().enumerate() { - let aa3 = _aa1_to_aa3_str(*aa1).ok_or_else(|| { + let aa3 = AA1_TO_AA3_STR[*aa1 as usize].ok_or_else(|| { Error::InvalidOneLetterAminoAcid(format!("{:?}", aa1), format!("{}", i + 1)) })?; result.push_str(aa3);