Skip to content

Commit

Permalink
fixes, elevation params update
Browse files Browse the repository at this point in the history
  • Loading branch information
emanueldima committed Apr 25, 2024
1 parent e6c264b commit 5895b04
Show file tree
Hide file tree
Showing 14 changed files with 289 additions and 126 deletions.
15 changes: 13 additions & 2 deletions src/api/internal/elevation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use crate::{
// --@-> attr group --[]-> interp2_param1
// --[]-> ...

const STD_ITP_PARAM_WRITE_BACK_ON_DROP: &str = "w";

#[derive(Debug, Clone)]
struct Data {
origin: Xell,
Expand Down Expand Up @@ -257,6 +259,12 @@ impl GroupTrait for Group {
let (target, (constructor, params)) =
data.map.get_index_mut(i).ok_or_else(noerr)?;
if let CellKind::DetachedParam(b) = cell.kind {
if params.contains_key(&b.0.as_value()) {
return userres(format!(
"cannot add elevation param with label `{}` twice",
b.0
));
}
params.insert(b.0, b.1);
}
if let Some(index) = index {
Expand Down Expand Up @@ -345,11 +353,14 @@ impl Group {
}
GroupKind::Sub(i) => {
let (target, (constructor, params)) = data.map.get_index(i).ok_or_else(noerr)?;
// println!("construct elevation {} {:?}", target, params);
let cell = constructor(data.origin.clone(), target, params)?;
cell.set_self_as_domain_root();

if let Some(w) = params.get(&Value::Str("w")) {
if w.as_value() == Value::Bool(true) || w.as_value() == Value::None {
for (k, v) in params.iter() {
if matches!(k, OwnValue::Int(_))
&& v.as_value() == Value::Str(STD_ITP_PARAM_WRITE_BACK_ON_DROP)
{
cell.policy(WritePolicy::WriteBackOnDrop);
}
}
Expand Down
79 changes: 53 additions & 26 deletions src/api/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,35 @@ use indexmap::Equivalent;
pub const DISPLAY_VALUE_NONE: &str = "ø"; // ❍•⸰·
pub const DISPLAY_BYTES_VALUE_LEN: usize = 72;

#[derive(Copy, Clone)]
pub struct Int {
pub n: IntData,
pub sz: IntKind,
}
#[derive(Copy, Clone, Debug)]
pub enum IntData {
Signed(i64),
Unsigned(u64),
}
#[derive(Copy, Clone, Debug)]
pub enum Int {
I64(i64),
U64(u64),
I32(i32),
U32(u32),
pub enum IntKind {
I64,
U64,
I32,
U32,
}

impl Int {
pub fn as_i128(&self) -> i128 {
match self {
Int::I64(x) => *x as i128,
Int::U64(x) => *x as i128,
Int::I32(x) => *x as i128,
Int::U32(x) => *x as i128,
match self.n {
IntData::Signed(x) => x as i128,
IntData::Unsigned(x) => x as i128,
}
}
}

impl Hash for Int {
fn hash<H: Hasher>(&self, state: &mut H) {
core::mem::discriminant(self).hash(state);
self.as_i128().hash(state);
}
}
Expand All @@ -61,39 +68,59 @@ impl Ord for Int {

impl Default for Int {
fn default() -> Self {
Int::I64(0)
Int{sz: IntKind::I64, n: IntData::Signed(0)}
}
}

impl Display for Int {
fn fmt(&self, buf: &mut fmt::Formatter) -> fmt::Result {
match self {
Int::I32(x) => write!(buf, "{}", x),
Int::U32(x) => write!(buf, "{}", x),
Int::I64(x) => write!(buf, "{}", x),
Int::U64(x) => write!(buf, "{}", x),
Int { n: IntData::Signed(x), .. } => write!(buf, "{}", x),
Int { n: IntData::Unsigned(x), .. } => write!(buf, "{}", x),
}
}
}

impl fmt::Debug for Int {
fn fmt(&self, buf: &mut fmt::Formatter) -> fmt::Result {
write!(buf, "{}", self)?;
match self.sz {
IntKind::I32 => write!(buf, "i32"),
IntKind::U32 => write!(buf, "u32"),
IntKind::I64 => write!(buf, "i64"),
IntKind::U64 => write!(buf, "u64"),
}
}
}

impl From<i32> for Int {
fn from(x: i32) -> Self {
Int::I32(x)
Int{sz: IntKind::I32, n: IntData::Signed(x as i64)}
}
}
impl From<u32> for Int {
fn from(x: u32) -> Self {
Int::U32(x)
Int{sz: IntKind::U32, n: IntData::Unsigned(x as u64)}
}
}
impl From<i64> for Int {
fn from(x: i64) -> Self {
Int::I64(x)
Int{sz: IntKind::I64, n: IntData::Signed(x)}
}
}
impl From<u64> for Int {
fn from(x: u64) -> Self {
Int::U64(x)
Int{sz: IntKind::U64, n: IntData::Unsigned(x)}
}
}
impl From<isize> for Int {
fn from(x: isize) -> Self {
Int{sz: IntKind::I64, n: IntData::Signed(x as i64)}
}
}
impl From<usize> for Int {
fn from(x: usize) -> Self {
Int{sz: IntKind::U64, n: IntData::Unsigned(x as u64)}
}
}

Expand Down Expand Up @@ -375,32 +402,32 @@ impl From<Int> for Value<'_> {
}
impl From<i32> for Value<'_> {
fn from(x: i32) -> Self {
Value::Int(Int::I32(x))
Value::Int(Int::from(x))
}
}
impl From<u32> for Value<'_> {
fn from(x: u32) -> Self {
Value::Int(Int::U32(x))
Value::Int(Int::from(x))
}
}
impl From<i64> for Value<'_> {
fn from(x: i64) -> Self {
Value::Int(Int::I64(x))
Value::Int(Int::from(x))
}
}
impl From<u64> for Value<'_> {
fn from(x: u64) -> Self {
Value::Int(Int::U64(x))
Value::Int(Int::from(x))
}
}
impl From<isize> for Value<'_> {
fn from(x: isize) -> Self {
Value::Int(Int::I64(x as i64))
Value::Int(Int::from(x))
}
}
impl From<usize> for Value<'_> {
fn from(x: usize) -> Self {
Value::Int(Int::U64(x as u64))
Value::Int(Int::from(x))
}
}
impl From<StrFloat> for Value<'_> {
Expand Down
2 changes: 1 addition & 1 deletion src/interpretations/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl CellReaderTrait for CellReader {
if self.pos != 0 {
return fault("invalid attribute index");
}
Ok(Value::Int(Int::U64(md.filesize)))
Ok(Value::from(md.filesize))
}
}
}
Expand Down
21 changes: 11 additions & 10 deletions src/interpretations/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,17 @@ impl Cell {

let client = Client::builder().user_agent("hial").build()?;
let method = {
if params.contains_key(&Value::Str(HEAD_METHOD)) {
HEAD_METHOD
} else if let Some(method) = params.get(&Value::Str(METHOD_PARAM_NAME)) {
match method.as_cow_str().as_ref() {
HEAD_METHOD => HEAD_METHOD,
_ => GET_METHOD,
let mut m = GET_METHOD;
if let Some(method) = params.get(&Value::Str(METHOD_PARAM_NAME)) {
if method.as_cow_str().as_ref() == HEAD_METHOD {
m = HEAD_METHOD
}
} else {
GET_METHOD
}
} else if let Some(method) = params.get(&Value::from(0)) {
if method.as_value() == Value::Str(HEAD_METHOD) {
m = HEAD_METHOD
}
};
m
};
let request = if method == HEAD_METHOD {
client.head(url)
Expand Down Expand Up @@ -253,7 +254,7 @@ impl CellReaderTrait for CellReader {
(GroupKind::Root, 0) => Ok(Value::Bytes(&self.response.body)),
(GroupKind::Attr, 0) => nores(),
(GroupKind::Attr, 1) => nores(),
(GroupKind::Status, 0) => Ok(Value::Int(Int::I32(self.response.status as i32))),
(GroupKind::Status, 0) => Ok(Value::from(self.response.status as i32)),
(GroupKind::Status, 1) => Ok(Value::Str(&self.response.reason)),
(GroupKind::Headers, _) => {
let header_values = if let Some(hv) = self.response.headers.get_index(self.pos) {
Expand Down
8 changes: 4 additions & 4 deletions src/interpretations/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,10 @@ fn ownvalue_to_serde(v: OwnValue) -> SValue {
match v {
OwnValue::None => SValue::Null,
OwnValue::Bool(b) => SValue::Bool(b),
OwnValue::Int(Int::I32(i)) => SValue::Number(i.into()),
OwnValue::Int(Int::U32(i)) => SValue::Number(i.into()),
OwnValue::Int(Int::I64(i)) => SValue::Number(i.into()),
OwnValue::Int(Int::U64(i)) => SValue::Number(i.into()),
OwnValue::Int(Int{n,..}) => match n {
IntData::Signed(i) => SValue::Number(i.into()),
IntData::Unsigned(u) => SValue::Number(u.into()),
},
OwnValue::Float(StrFloat(f)) => {
SValue::Number(serde_json::Number::from_f64(f).unwrap_or(serde_json::Number::from(0)))
}
Expand Down
4 changes: 2 additions & 2 deletions src/interpretations/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ impl Cell {
let v = r.value()?;
let text = v.as_cow_str().to_string();

let Some(arg) = params.iter().next() else {
let Some(pattern) = params.get(&Value::from(0)) else {
return userres("regex requires a parameter");
};

let re = Regex::new(arg.0.as_cow_str().as_ref())
let re = Regex::new(pattern.as_cow_str().as_ref())
.map_err(|e| caused(HErrKind::User, "bad regex", e))?;

let mut data = Data { matches: vec![] };
Expand Down
Loading

0 comments on commit 5895b04

Please sign in to comment.