Move converted code to its own submodule

This commit is contained in:
Alex Page 2023-06-22 01:49:34 -04:00
parent 6d27106e35
commit 3c55eaadbc
2 changed files with 45 additions and 45 deletions

1321
src/confid/black_box.rs Normal file

File diff suppressed because it is too large Load diff

43
src/confid/mod.rs Normal file
View file

@ -0,0 +1,43 @@
use std::ffi::{CStr, CString};
use thiserror::Error;
mod black_box;
#[derive(Error, Debug)]
pub enum ConfirmationIdError {
#[error("Installation ID is too short.")]
TooShort,
#[error("Installation ID is too long.")]
TooLarge,
#[error("Invalid character in installation ID.")]
InvalidCharacter,
#[error("Installation ID checksum failed. Please check that it is typed correctly.")]
InvalidCheckDigit,
#[error("Unknown installation ID version.")]
UnknownVersion,
#[error("Unable to generate valid confirmation ID.")]
Unlucky,
}
pub fn generate(installation_id: &str) -> Result<String, ConfirmationIdError> {
let inst_id = CString::new(installation_id).unwrap();
let conf_id = [0u8; 49];
let result = unsafe { black_box::Generate(inst_id.as_ptr(), conf_id.as_ptr() as *mut i8) };
match result {
0 => {}
1 => return Err(ConfirmationIdError::TooShort),
2 => return Err(ConfirmationIdError::TooLarge),
3 => return Err(ConfirmationIdError::InvalidCharacter),
4 => return Err(ConfirmationIdError::InvalidCheckDigit),
5 => return Err(ConfirmationIdError::UnknownVersion),
6 => return Err(ConfirmationIdError::Unlucky),
_ => panic!("Unknown error code: {}", result),
}
unsafe {
Ok(CStr::from_ptr(conf_id.as_ptr() as *const i8)
.to_str()
.unwrap()
.to_string())
}
}