Skip to content

Commit

Permalink
search improvements, replace CellTrait::get with get_all
Browse files Browse the repository at this point in the history
  • Loading branch information
emanueldima committed Mar 1, 2024
1 parent 3d1ccf4 commit e45216d
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 166 deletions.
1 change: 1 addition & 0 deletions issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- explicit and implicit write support (policy, include readonly)
- fix tests, todo!() and TODO: in code

- change search: multiple path indices for one cell
- support type selector: `hial './src/tests/rust.rs^rust/:function_item'`
- support rust/ts write: `hial './src/tests/rust.rs^rust/:function_item[-1]#label = "modified_fn_name"'`
- set value on the command line
Expand Down
4 changes: 0 additions & 4 deletions src/base/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,6 @@ impl GroupTrait for HErr {
Err(self.clone())
}

fn get(&self, label: Value<'_>) -> Res<Self::Cell> {
Err(self.clone())
}

fn get_all(&self, label: Value<'_>) -> Res<Self::CellIterator> {
Err(self.clone())
}
Expand Down
90 changes: 57 additions & 33 deletions src/base/extra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,22 +234,6 @@ impl From<HErr> for Cell {
}
}

impl fmt::Debug for Cell {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let orig = format!("{:?}", self.domain.origin).replace('\n', "\n\t");
let root = format!("{:?}", self.domain.dyn_root).replace('\n', "\n\t");
write!(
f,
"Cell{{\n\tdyn_cell={:?}, \n\tdomain={{ write_policy={:?}, is_dirty={}, root={}, origin={} }}",
self.dyn_cell,
self.domain.write_policy.get(),
self.domain.dirty.get(),
root,
orig,
)
}
}

impl CellReaderTrait for CellReader {
fn ty(&self) -> Res<&str> {
dispatch_dyn_cell_reader!(&self.0, |x| { x.ty() })
Expand Down Expand Up @@ -649,18 +633,34 @@ impl Cell {
}
}

impl fmt::Debug for Cell {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let orig = format!("{:?}", self.domain.origin).replace('\n', "\n\t");
let root = format!("{:?}", self.domain.dyn_root).replace('\n', "\n\t");
write!(
f,
"Cell{{\n\tdyn_cell={:?}, \n\tdomain={{ write_policy={:?}, is_dirty={}, root={}, origin={} }}",
self.dyn_cell,
self.domain.write_policy.get(),
self.domain.dirty.get(),
root,
orig,
)
}
}

impl fmt::Debug for Domain {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Domain{{ dyn_root={:?}, origin={:?} write_policy={:?}, is_dirty={} }}",
"Domain{{ write_policy={:?}, is_dirty={} root={:?}, origin={:?} }}",
self.write_policy.get(),
self.dirty.get(),
self.dyn_root.get(),
self.origin
.as_ref()
.map(|c| c.path().unwrap_or("<error>".into()))
.unwrap_or_default(),
self.write_policy.get(),
self.dirty.get()
)
}
}
Expand Down Expand Up @@ -780,8 +780,12 @@ impl Group {
GroupKind::Dyn { dyn_group, domain } => {
dispatch_dyn_group!(dyn_group, |x| {
Cell {
dyn_cell: match x.get(key) {
Ok(c) => DynCell::from(c),
dyn_cell: match x.get_all(key) {
Ok(mut iter) => match iter.next() {
Some(Ok(cell)) => DynCell::from(cell),
Some(Err(err)) => DynCell::from(err),
None => DynCell::from(noerr()),
},
Err(e) => DynCell::from(e),
},
domain: Rc::clone(domain),
Expand Down Expand Up @@ -831,8 +835,18 @@ impl Group {
},
}
}
GroupKind::Elevation(elevation_group) => CellIterator {
cell_iterator: CellIteratorKind::Elevation(elevation_group.get(key)),
GroupKind::Elevation(elevation_group) => match elevation_group.get(key) {
Ok(c) => {
if let Err(ref old_cell) = c.domain.dyn_root.set(c.dyn_cell.clone()) {
warning!("❗️cannot overwrite domain dyn_root: {:?}", old_cell);
}
CellIterator {
cell_iterator: CellIteratorKind::Elevation(Ok(c)),
}
}
Err(e) => CellIterator {
cell_iterator: CellIteratorKind::Elevation(Err(e)),
},
},
}
}
Expand Down Expand Up @@ -888,16 +902,26 @@ impl Iterator for CellIterator {
})
}
CellIteratorKind::Elevation(cell_res) => match cell_res {
Ok(cell) => Some(cell.clone()),
Err(err) => Some(Cell {
dyn_cell: DynCell::from(err.clone()),
domain: Rc::new(Domain {
write_policy: cell::Cell::new(WritePolicy::ReadOnly),
origin: None,
dyn_root: OnceCell::new(),
dirty: cell::Cell::new(false),
}),
}),
Ok(cell) => {
let cell = cell.clone();
*cell_res = nores();
Some(cell)
}
Err(err) => {
if err.kind == HErrKind::None {
None
} else {
Some(Cell {
dyn_cell: DynCell::from(err.clone()),
domain: Rc::new(Domain {
write_policy: cell::Cell::new(WritePolicy::ReadOnly),
origin: None,
dyn_root: OnceCell::new(),
dirty: cell::Cell::new(false),
}),
})
}
}
},
}
}
Expand Down
21 changes: 9 additions & 12 deletions src/base/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,20 @@ impl GroupTrait for FieldGroup {
})
}

fn get(&self, label: Value) -> Res<Self::Cell> {
if let Value::Str(l) = label {
return match l {
fn get_all(&self, label: Value) -> Res<Self::CellIterator> {
let cell = if let Value::Str(l) = label {
match l {
"value" => self.at(FieldType::Value as usize),
"label" => self.at(FieldType::Label as usize),
"type" => self.at(FieldType::Type as usize),
"index" => self.at(FieldType::Index as usize),
"serial" => self.at(FieldType::Serial as usize),
_ => nores(),
};
}
nores()
}

fn get_all(&self, label: Value) -> Res<Self::CellIterator> {
let cell = self.get(label)?;
Ok(std::iter::once(Ok(cell)))
_ => return nores(),
}
} else {
return nores();
};
Ok(std::iter::once(cell))
}
}

Expand Down
5 changes: 0 additions & 5 deletions src/base/intra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ pub trait GroupTrait: Clone + Debug {
self.len().map_or(false, |l| l == 0)
}
fn at(&self, index: usize) -> Res<Self::Cell>;
fn get(&self, label: Value<'_>) -> Res<Self::Cell>;
fn get_all(&self, label: Value<'_>) -> Res<Self::CellIterator>;

fn add(&mut self) -> Res<()> {
Expand Down Expand Up @@ -100,10 +99,6 @@ impl<C: CellTrait> GroupTrait for VoidGroup<C> {
nores()
}

fn get(&self, key: Value) -> Res<C> {
nores()
}

fn get_all(&self, label: Value) -> Res<Self::CellIterator> {
nores()
}
Expand Down
12 changes: 4 additions & 8 deletions src/interpretations/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,8 @@ impl GroupTrait for Group {
}
}

fn get(&self, key: Value) -> Res<Cell> {
match self.ty {
fn get_all(&self, key: Value) -> Res<Self::CellIterator> {
let cell = match self.ty {
GroupType::Folder => {
let files = self
.files
Expand All @@ -498,12 +498,8 @@ impl GroupTrait for Group {
nores()
}
}
}
}

fn get_all(&self, key: Value) -> Res<Self::CellIterator> {
let cell = self.get(key)?;
Ok(std::iter::once(Ok(cell)))
};
Ok(std::iter::once(cell))
}
}

Expand Down
10 changes: 3 additions & 7 deletions src/interpretations/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ impl GroupTrait for Group {
}
}

fn get(&self, key: Value) -> Res<Cell> {
match (self.kind, key) {
fn get_all(&self, key: Value) -> Res<Self::CellIterator> {
let cell = match (self.kind, key) {
(GroupKind::Attr, sel) if sel == "status" => Ok(Cell {
group: self.clone(),
pos: 0,
Expand Down Expand Up @@ -336,11 +336,7 @@ impl GroupTrait for Group {
}
}
_ => nores(),
}
}

fn get_all(&self, key: Value) -> Res<Self::CellIterator> {
let cell = self.get(key);
};
Ok(std::iter::once(cell))
}
}
Expand Down
12 changes: 4 additions & 8 deletions src/interpretations/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ impl GroupTrait for Group {
}
}

fn get(&self, key: Value) -> Res<Cell> {
match &self.nodes {
fn get_all(&self, key: Value) -> Res<Self::CellIterator> {
let cell = match &self.nodes {
NodeGroup::Array(a) => nores(),
NodeGroup::Object(o) => match key {
Value::Str(k) => match o
Expand All @@ -418,12 +418,8 @@ impl GroupTrait for Group {
},
_ => nores(),
},
}
}

fn get_all(&self, key: Value) -> Res<Self::CellIterator> {
let cell = self.get(key)?;
Ok(std::iter::once(Ok(cell)))
};
Ok(std::iter::once(cell))
}
}

Expand Down
38 changes: 17 additions & 21 deletions src/interpretations/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,25 +282,6 @@ impl GroupTrait for Group {
}
}

fn get(&self, key: Value) -> Res<Cell> {
match &self.nodes {
NodeGroup::Array(a) => nores(),
NodeGroup::Table(t) => match key {
Value::Str(k) => {
let t = t.read().ok_or_else(|| lockerr("cannot read group"))?;
match t.get_index_of(k) {
Some(pos) => Ok(Cell {
group: self.clone(),
pos,
}),
_ => nores(),
}
}
_ => nores(),
},
}
}

fn len(&self) -> Res<usize> {
Ok(match &self.nodes {
NodeGroup::Array(a) => a.read().ok_or_else(|| lockerr("cannot read group"))?.len(),
Expand All @@ -324,8 +305,23 @@ impl GroupTrait for Group {
}

fn get_all(&self, key: Value) -> Res<Self::CellIterator> {
let cell = self.get(key)?;
Ok(std::iter::once(Ok(cell)))
let cell = match &self.nodes {
NodeGroup::Array(a) => nores(),
NodeGroup::Table(t) => match key {
Value::Str(k) => {
let t = t.read().ok_or_else(|| lockerr("cannot read group"))?;
match t.get_index_of(k) {
Some(pos) => Ok(Cell {
group: self.clone(),
pos,
}),
_ => nores(),
}
}
_ => nores(),
},
};
Ok(std::iter::once(cell))
}
}

Expand Down
12 changes: 4 additions & 8 deletions src/interpretations/treesitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,28 +297,24 @@ impl GroupTrait for Cell {
})
}

fn get(&self, key: Value) -> Res<Cell> {
fn get_all(&self, key: Value) -> Res<Self::CellIterator> {
let mut cursor = self.cursor.clone();
if !cursor.goto_first_child() {
return nores();
}
for i in 0..self.cursor.node().child_count() {
if key == cursor.field_name().unwrap_or_default() {
return Ok(Cell {
let cell = Cell {
domain: self.domain.clone(),
cursor,
position: i,
});
};
return Ok(std::iter::once(Ok(cell)));
}
if !cursor.goto_next_sibling() {
return nores();
}
}
nores()
}

fn get_all(&self, key: Value) -> Res<Self::CellIterator> {
let cell = self.get(key);
Ok(std::iter::once(cell))
}
}
18 changes: 1 addition & 17 deletions src/interpretations/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,7 @@ impl Iterator for CellIterator {
let cell = this.group.at(this.next_pos)?;
this.next_pos += 1;
let reader = cell.read()?;
let k = reader.label()?;
if this.key.as_value() == k {
if Some(this.key.as_value()) == reader.label().ok() {
return Ok(cell);
}
}
Expand Down Expand Up @@ -639,21 +638,6 @@ impl GroupTrait for Group {
})
}

fn get(&self, key: Value) -> Res<Cell> {
for i in 0..self.len()? {
if let Ok(cell) = self.at(i) {
if let Ok(reader) = cell.read() {
if let Ok(k) = reader.label() {
if key == k {
return Ok(cell);
}
}
}
}
}
nores()
}

fn get_all(&self, key: Value) -> Res<CellIterator> {
Ok(CellIterator {
group: self.clone(),
Expand Down
Loading

0 comments on commit e45216d

Please sign in to comment.