Skip to content

Commit

Permalink
Fix precision upgrade check for float + rational
Browse files Browse the repository at this point in the history
- Improve error handling of email requests
  • Loading branch information
benruijl committed Sep 28, 2024
1 parent f680528 commit 7da76d0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 91 deletions.
8 changes: 4 additions & 4 deletions src/domains/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,8 @@ impl Add<i64> 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;
Expand Down Expand Up @@ -752,8 +752,8 @@ impl Add<Rational> 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();
Expand Down
130 changes: 43 additions & 87 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, JsonValue> = 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<String, JsonValue>) -> Result<(), String> {
let mut stream = Self::connect()?;
let mut m: HashMap<String, JsonValue> = 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<String, JsonValue> = 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<String, JsonValue> = 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(
Expand All @@ -485,57 +479,19 @@ Error: {}",
company: &str,
super_license: &str,
) -> Result<(), String> {
let mut stream = Self::connect()?;
let mut m: HashMap<String, JsonValue> = 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<String, JsonValue> = 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)
}
}

0 comments on commit 7da76d0

Please sign in to comment.