Croissant Runtime
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
crsn/crsn/src/utils/mod.rs

65 lines
1.7 KiB

use std::fmt::Display;
use std::str::FromStr;
pub use option_ext::UncheckedOptionExt;
use sexp::{Atom, Sexp};
mod option_ext;
/// Convert a value to Sexp
#[allow(non_snake_case)]
pub fn A(s: impl Display) -> Sexp {
let s = s.to_string();
let x: Result<u64, _> = FromStr::from_str(&s);
if let Ok(x) = x {
return Sexp::Atom(Atom::U(x), Default::default());
}
let x: Result<i64, _> = FromStr::from_str(&s);
if let Ok(x) = x {
return Sexp::Atom(Atom::I(x), Default::default());
}
let x: Result<f64, _> = FromStr::from_str(&s);
if let Ok(x) = x {
return Sexp::Atom(Atom::F(x), Default::default());
}
if s.contains(|c: char| " \t\"\\\n\t\r".contains(c)) {
Sexp::Atom(Atom::QS(s), Default::default())
} else {
Sexp::Atom(Atom::S(s), Default::default())
}
}
/// Convert a value to Sexp, with a bit mask
#[allow(non_snake_case)]
pub fn AM(s: impl Display, offs : u32) -> Sexp {
if offs == 0 {
return A(s);
}
let s = s.to_string();
let x: Result<u64, _> = FromStr::from_str(&s);
if let Ok(x) = x {
return Sexp::Atom(Atom::S(format!("{}:{}", x, offs)), Default::default());
}
let x: Result<i64, _> = FromStr::from_str(&s);
if let Ok(x) = x {
return Sexp::Atom(Atom::S(format!("{}:{}", x, offs)), Default::default());
}
let x: Result<f64, _> = FromStr::from_str(&s);
if let Ok(x) = x {
return Sexp::Atom(Atom::S(format!("{}:{}", x, offs)), Default::default());
}
if s.contains(|c: char| " \t\"\\\n\t\r".contains(c)) {
panic!("Quoted string with a bit mask!");
} else {
Sexp::Atom(Atom::S(format!("{}:{}", s, offs)), Default::default())
}
}