From 7da76d0cbe1cbb716aaf163bee3b4d43f129c345 Mon Sep 17 00:00:00 2001 From: Ben Ruijl Date: Sat, 28 Sep 2024 11:12:13 +0200 Subject: [PATCH] Fix precision upgrade check for float + rational - Improve error handling of email requests --- src/domains/float.rs | 8 +-- src/lib.rs | 130 ++++++++++++++----------------------------- 2 files changed, 47 insertions(+), 91 deletions(-) diff --git a/src/domains/float.rs b/src/domains/float.rs index a79e8c58..997f66bb 100644 --- a/src/domains/float.rs +++ b/src/domains/float.rs @@ -640,8 +640,8 @@ impl Add for Float { let e2 = rhs.unsigned_abs().ilog2() + 1; let old_prec = self.prec(); - if e1 < 0 || e1.unsigned_abs() < e2 { - self.set_prec(self.prec() + e2.checked_add_signed(-e1).unwrap() + 1); + if e1.unsigned_abs() <= e2 { + self.set_prec(old_prec + (e2 as i32 - e1) as u32 + 1); } let mut r = self.0 + rhs; @@ -752,8 +752,8 @@ impl Add for Float { let old_prec = self.prec(); - if e1 < 0 || e1 < e2 { - self.set_prec(self.prec() + (e2 - e1) as u32 + 1); + if e1 <= e2 { + self.set_prec(old_prec + (e2 - e1) as u32 + 1); } let np = self.prec(); diff --git a/src/lib.rs b/src/lib.rs index 5a545ac1..6e62af7d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -415,68 +415,62 @@ Error: {}", env!("SYMBOLICA_VERSION") } - /// Request a key for **non-professional** use for the user `name`, that will be sent to the e-mail address - /// `email`. - pub fn request_hobbyist_license(name: &str, email: &str) -> Result<(), String> { - if let Ok(mut stream) = Self::connect() { - let mut m: HashMap = HashMap::default(); - m.insert("name".to_owned(), name.to_owned().into()); - m.insert("email".to_owned(), email.to_owned().into()); - m.insert("type".to_owned(), "hobbyist".to_owned().into()); - let mut v = JsonValue::from(m).stringify().unwrap(); - v.push('\n'); - - stream.write_all(v.as_bytes()).unwrap(); - - let mut buf = Vec::new(); - stream.read_to_end(&mut buf).unwrap(); - let read_str = std::str::from_utf8(&buf).unwrap(); - - if read_str == "{\"status\":\"email sent\"}\n" { - Ok(()) - } else if read_str.is_empty() { - Err("Empty response".to_owned()) - } else { - let message: JsonValue = read_str[..read_str.len() - 1].parse().unwrap(); - let message_parsed: &HashMap<_, _> = message.get().unwrap(); - let status: &String = message_parsed.get("status").unwrap().get().unwrap(); - Err(status.clone()) - } - } else { - Err("Could not connect to the license server".to_owned()) - } - } - - /// Request a key for a trial license for the user `name` working at `company`, that will be sent to the e-mail address - /// `email`. - pub fn request_trial_license(name: &str, email: &str, company: &str) -> Result<(), String> { + fn request_license_email(data: HashMap) -> Result<(), String> { let mut stream = Self::connect()?; - let mut m: HashMap = HashMap::default(); - m.insert("name".to_owned(), name.to_owned().into()); - m.insert("email".to_owned(), email.to_owned().into()); - m.insert("company".to_owned(), company.to_owned().into()); - m.insert("type".to_owned(), "trial".to_owned().into()); - let mut v = JsonValue::from(m).stringify().unwrap(); + let mut v = JsonValue::from(data).stringify().unwrap(); v.push('\n'); - stream.write_all(v.as_bytes()).unwrap(); + stream + .write_all(v.as_bytes()) + .map_err(|e| format!("{}\nError: {}", NETWORK_ERROR, e))?; let mut buf = Vec::new(); - stream.read_to_end(&mut buf).unwrap(); - let read_str = std::str::from_utf8(&buf).unwrap(); + stream + .read_to_end(&mut buf) + .map_err(|e| format!("{}\nError: {}", NETWORK_ERROR, e))?; + let read_str = std::str::from_utf8(&buf).map_err(|_| "Bad server response".to_string())?; if read_str == "{\"status\":\"email sent\"}\n" { Ok(()) } else if read_str.is_empty() { Err("Empty response".to_owned()) } else { - let message: JsonValue = read_str[..read_str.len() - 1].parse().unwrap(); - let message_parsed: &HashMap<_, _> = message.get().unwrap(); - let status: &String = message_parsed.get("status").unwrap().get().unwrap(); + let message: JsonValue = read_str[..read_str.len() - 1] + .parse() + .map_err(|_| "Bad server response".to_string())?; + let message_parsed: &HashMap<_, _> = message + .get() + .ok_or_else(|| "Bad server response".to_string())?; + let status: &String = message_parsed + .get("status") + .unwrap() + .get() + .ok_or_else(|| "Bad server response".to_string())?; Err(status.clone()) } } + /// Request a key for **non-professional** use for the user `name`, that will be sent to the e-mail address + /// `email`. + pub fn request_hobbyist_license(name: &str, email: &str) -> Result<(), String> { + let mut m: HashMap = HashMap::default(); + m.insert("name".to_owned(), name.to_owned().into()); + m.insert("email".to_owned(), email.to_owned().into()); + m.insert("type".to_owned(), "hobbyist".to_owned().into()); + Self::request_license_email(m) + } + + /// Request a key for a trial license for the user `name` working at `company`, that will be sent to the e-mail address + /// `email`. + pub fn request_trial_license(name: &str, email: &str, company: &str) -> Result<(), String> { + let mut m: HashMap = HashMap::default(); + m.insert("name".to_owned(), name.to_owned().into()); + m.insert("email".to_owned(), email.to_owned().into()); + m.insert("company".to_owned(), company.to_owned().into()); + m.insert("type".to_owned(), "trial".to_owned().into()); + Self::request_license_email(m) + } + /// Request a sublicense key for the user `name` working at `company` that has the site-wide license `super_license`. /// The key will be sent to the e-mail address `email`. pub fn request_sublicense( @@ -485,57 +479,19 @@ Error: {}", company: &str, super_license: &str, ) -> Result<(), String> { - let mut stream = Self::connect()?; let mut m: HashMap = HashMap::default(); m.insert("name".to_owned(), name.to_owned().into()); m.insert("email".to_owned(), email.to_owned().into()); m.insert("company".to_owned(), company.to_owned().into()); m.insert("type".to_owned(), "sublicense".to_owned().into()); m.insert("super_license".to_owned(), super_license.to_owned().into()); - let mut v = JsonValue::from(m).stringify().unwrap(); - v.push('\n'); - - stream.write_all(v.as_bytes()).unwrap(); - - let mut buf = Vec::new(); - stream.read_to_end(&mut buf).unwrap(); - let read_str = std::str::from_utf8(&buf).unwrap(); - - if read_str == "{\"status\":\"email sent\"}\n" { - Ok(()) - } else if read_str.is_empty() { - Err("Empty response".to_owned()) - } else { - let message: JsonValue = read_str[..read_str.len() - 1].parse().unwrap(); - let message_parsed: &HashMap<_, _> = message.get().unwrap(); - let status: &String = message_parsed.get("status").unwrap().get().unwrap(); - Err(status.clone()) - } + Self::request_license_email(m) } /// Get the license key for the account registered with the provided email address. pub fn get_license_key(email: &str) -> Result<(), String> { - let mut stream = Self::connect()?; let mut m: HashMap = HashMap::default(); m.insert("email".to_owned(), email.to_owned().into()); - let mut v = JsonValue::from(m).stringify().unwrap(); - v.push('\n'); - - stream.write_all(v.as_bytes()).unwrap(); - - let mut buf = Vec::new(); - stream.read_to_end(&mut buf).unwrap(); - let read_str = std::str::from_utf8(&buf).unwrap(); - - if read_str == "{\"status\":\"email sent\"}\n" { - Ok(()) - } else if read_str.is_empty() { - Err("Empty response".to_owned()) - } else { - let message: JsonValue = read_str[..read_str.len() - 1].parse().unwrap(); - let message_parsed: &HashMap<_, _> = message.get().unwrap(); - let status: &String = message_parsed.get("status").unwrap().get().unwrap(); - Err(status.clone()) - } + Self::request_license_email(m) } }