Skip to content

Instantly share code, notes, and snippets.

@mitchchn
Created May 7, 2019 22:46
Show Gist options
  • Save mitchchn/28d9a6682093706cf42ef0c65d856f0a to your computer and use it in GitHub Desktop.
Save mitchchn/28d9a6682093706cf42ef0c65d856f0a to your computer and use it in GitHub Desktop.
OPID parsing methods in Rust
impl Form {
/// Converts legacy `__form__0` to `0`.
pub fn get_id(&self) -> Result<i32, std::num::ParseIntError> {
parse_form_opid(self.opid.as_str())
}
}
impl Field {
/// Taking the old school `"__0"` and turning it into `0`
pub fn get_id(&self) -> Result<i32, std::num::ParseIntError> {
self.opid.trim_start_matches("__").parse()
}
pub fn get_form_id(&self) -> Option<i32> {
if let Some(form_id) = &self.form_opid {
parse_form_opid(form_id.as_str()).ok()
} else {
None
}
}
}
fn parse_form_opid(opid: &str) -> Result<i32, std::num::ParseIntError> {
opid.trim_start_matches("__form__").parse()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment