Skip to content

Commit

Permalink
use lazylock, cleanup build.rs, bump to min rust 1.80
Browse files Browse the repository at this point in the history
  • Loading branch information
tedil committed Jan 7, 2025
1 parent 8c7d0ed commit 75d9d06
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
25 changes: 23 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down Expand Up @@ -95,7 +96,10 @@ fn generate_codon_2bit_to_aa1_chrmt_vertebrate(f: &mut BufWriter<File>) -> Resul
}

fn generate_aa1_to_aa3_str_lookup_function(f: &mut BufWriter<File>) -> 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)?;
Expand All @@ -106,8 +110,25 @@ fn generate_aa1_to_aa3_str_lookup_function(f: &mut BufWriter<File>) -> Result<()
Ok(())
}

fn generate_aa1_to_aa3_str_lookup_table(f: &mut BufWriter<File>) -> 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<File>) -> Result<()> {
writeln!(f, "fn _aa3_to_aa1(aa3: &[u8]) -> Option<u8> {{")?;
writeln!(f, "const fn _aa3_to_aa1(aa3: &[u8]) -> Option<u8> {{")?;
writeln!(f, " match aa3 {{")?;
for (aa3, aa1) in AA3_TO_AA1_VEC {
writeln!(f, " b\"{}\" => Some(b'{}'),", aa3, aa1)?;
Expand Down
2 changes: 1 addition & 1 deletion src/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub fn aa1_to_aa3(seq: &str) -> Result<String, Error> {
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);
Expand Down

0 comments on commit 75d9d06

Please sign in to comment.