Skip to content

Commit

Permalink
rename set_x functions with just x
Browse files Browse the repository at this point in the history
  • Loading branch information
emanueldima committed Mar 8, 2024
1 parent 9fd7038 commit e8745b1
Show file tree
Hide file tree
Showing 27 changed files with 112 additions and 118 deletions.
45 changes: 24 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ hial 'http://api.github.com^http^json/rate_limit_url^http^json/resources/core'
```bash
hial './myfile.json^json/question[count(/answer_entities/*)==0]'
# 🚧 wip: functions: sum, count, min, max
# 🚧 todo: functions: sum, count, min, max
``` -->

Print all services with inaccessible images in a Docker compose file:

```bash
# shell
hial './config.yaml^yaml/services/*[ /image^split[":"]/[0]^http[HEAD]@status/code>=400 ]'
# 🚧 wip: split interpretation (regex[( ([^:]*): )*]
# 🚧 wip: HEAD param for http
# 🚧 todo: split interpretation (regex[( ([^:]*): )*]
# 🚧 todo: HEAD param for http
```

```rust
Expand All @@ -54,38 +54,38 @@ Print the structure of a rust file (struct, enum, type, functions) as a tree:

```bash
hial './src/tests/rust.rs^rust/**[#type^split["_"]/[-1]=="item"]/*[name|parameters|return_type]'
# 🚧 wip: search results as tree
# 🚧 todo: search results as tree
```

##### 2. Modify data selected as above.

Change the default mysql port systemwide:
```bash
# shell
hial '/etc/mysql/my.cnf^fs[rw]^ini/mysqld/port = 3307'
# 🚧 wip: rw parameter for fs interpretation
hial '/etc/mysql/my.cnf^fs[w]^ini/mysqld/port = 3307'
# 🚧 todo: rw parameter for fs interpretation
```

```bash
// rust
Cell::from("/etc/mysql/my.cnf")
.to("^fs[rw]^ini/mysqld/port")
.to("^fs[w]^ini/mysqld/port")
.write()
.set_value(3307)?;
.value(3307)?;
```

Change the user's docker configuration:
```bash
# shell
hial '~/.docker/config.json^json/auths/docker.io/username = "newuser"'
# 🚧 wip: support ~
# 🚧 todo: support ~
```
```rust
// rust
Cell::from("~/.docker/config.json")
.to("^fs[rw]^json/auths/docker.io/username")
.to("^fs[w]^json/auths/docker.io/username")
.write()
.set_value("newuser")?;
.value("newuser")?;
```

##### 3. Copy pieces of data from one place to another.
Expand All @@ -94,19 +94,20 @@ Copy a string from some json object entry which is embedded in a zip file, into

```bash
# shell
hial 'copy( ./assets.zip^zip/data.json^json/meshes/sphere ./src/assets/sphere.rs^rust/**[:let_declaration][/pattern=sphere]/value )'
# 🚧 wip: support copy
# 🚧 wip: support zip
# 🚧 wip: support :type filter
# 🚧 wip: /**[filter] should match leaves only
hial 'copy ./assets.zip^zip/data.json^json/meshes/sphere ./src/assets/sphere.rs^rust/**[#type=="let_declaration"][/pattern=sphere]/value'
# 🚧 todo: support copy
# 🚧 todo: support zip
# 🚧 todo: /**[filter] should match leaves only
```

Split a markdown file into sections and put each in a separate file:

```bash
# shell
`hial 'copy ./book.md^md/*[:heading1][as x] ./{label(x)}.md'
# 🚧 wip: support markdown
`hial 'copy ./book.md^md/*[#type=="heading1"][as x] ./{label(x)}.md'
# 🚧 todo: support copy
# 🚧 todo: support markdown
# 🚧 todo: support interpolation in destination
```

##### 4. Transform data from one format or shape into another.
Expand All @@ -116,7 +117,8 @@ Transform a json file into an xml file with the same format and vice versa:
```bash
hial 'copy file.json^json^tree^xml ./file.xml'
hial 'copy file.xml^xml^tree^json ./file.json'
# 🚧 wip: support tree implementation and conversion
# 🚧 todo: support copy
# 🚧 todo: support tree implementation and conversion
```

##### 5. Structured diffs
Expand All @@ -125,7 +127,8 @@ Compare two files in different formats and print the resulting diff tree:

```bash
hial 'diff ./file.json^json^tree ./file.xml^xml^tree'
# 🚧 wip: support diff
# 🚧 todo: support diff
# 🚧 todo: support tree implementation and conversion
```

## Installation and usage
Expand Down Expand Up @@ -154,7 +157,7 @@ Cell: value = String("{\"a\":1}"),
3. a json object which is represented by a tree of cells, the root cell being the json object `{}` with a sub cell with label `a` and value `1`:
```json
```yaml
Cell:
type: "object",
sub:
Expand Down
8 changes: 3 additions & 5 deletions doc/issues.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
# List of Todos and other Issues

- replace set_value(x) with value(x)
- support type selector: `hial './src/tests/rust.rs^rust/*[:function_item]'`
- add split(":") and regex interpretations
- /*[type().ends_with()] ?? -> or not, just replace it with #type^split("_")/[-1]=="xx"
- /*[name|parameters|return_type] ??
- ^fs[rw] ??
- should blobs/bytes be part of value? they are only useful by reinterpretation
- ^fs[w] ??
- set value on the command line: '/username = "newuser"'
- https://raw.githubusercontent.com/rust-lang/rust/master/src/tools/rustfmt/src/lib.rs^http^rust does not work
- support zip, markdown
- support 'copy source destination'
- support ^json^tree^xml
- support diff ./file.json^json^tree ./file.xml^xml^tree
- '**[filter]' must be work as '**/*[filter]' (filter to be applied only on leaves)
- support type selector: `hial './src/tests/rust.rs^rust/*[:function_item]'`
- support rust/ts write: `hial './src/tests/rust.rs^rust/*[:function_item].label = "modified_fn_name"'`
- new structure: /api, /api/impl, /interpretations/api, /interpretations/*, /search
- add http interpretation params: method=HEAD, accept=""
- functions
- should blobs/bytes be part of value? they are only useful by reinterpretation

- release first minimal version:
- interpretations: path+fs, json+yaml+toml+xml, rust+js, url?+http
Expand Down
6 changes: 3 additions & 3 deletions src/base/elevation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ fn elevation_map(interpretation: &str) -> Res<Arc<IndexMap<&'static str, Elevate
nores()
}

pub(crate) fn top_interpretation(cell: &Cell) -> Option<&str> {
if cell.interpretation() == "fs" && cell.read().ty().ok()? == "fs" {
pub(crate) fn auto_interpretation(cell: &Cell) -> Option<&str> {
if cell.interpretation() == "fs" && cell.read().ty().ok()? == "file" {
if let Ok(reader) = cell.read().err() {
if let Ok(Value::Str(name)) = reader.label() {
if name.ends_with(".c") {
Expand Down Expand Up @@ -158,7 +158,7 @@ impl ElevationGroup {
pub fn get(&self, key: Value) -> Res<Cell> {
let old_interp = self.0.interpretation();
let interp = match key {
Value::None => guard_some!(top_interpretation(&self.0), {
Value::None => guard_some!(auto_interpretation(&self.0), {
return nores();
}),
Value::Str(k) => k,
Expand Down
2 changes: 1 addition & 1 deletion src/base/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl CellReaderTrait for HErr {
}

impl CellWriterTrait for HErr {
fn set_value(&mut self, value: OwnValue) -> Res<()> {
fn value(&mut self, value: OwnValue) -> Res<()> {
Err(self.clone())
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/base/extra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,14 @@ impl CellReader {
}

impl CellWriterTrait for CellWriter {
fn set_label(&mut self, value: OwnValue) -> Res<()> {
fn label(&mut self, value: OwnValue) -> Res<()> {
self.domain.dirty.set(true);
dispatch_dyn_cell_writer!(&mut self.dyn_cell_writer, |x| { x.set_label(value) })
dispatch_dyn_cell_writer!(&mut self.dyn_cell_writer, |x| { x.label(value) })
}

fn set_value(&mut self, ov: OwnValue) -> Res<()> {
fn value(&mut self, ov: OwnValue) -> Res<()> {
self.domain.dirty.set(true);
dispatch_dyn_cell_writer!(&mut self.dyn_cell_writer, |x| { x.set_value(ov) })
dispatch_dyn_cell_writer!(&mut self.dyn_cell_writer, |x| { x.value(ov) })
}
}
impl CellWriter {
Expand Down Expand Up @@ -372,11 +372,11 @@ impl Cell {
}
}

pub fn top_interpretation(&self) -> Option<&str> {
pub fn auto_interpretation(&self) -> Option<&str> {
if let DynCell::Error(_) = self.dyn_cell {
return None;
}
elevation::top_interpretation(self)
elevation::auto_interpretation(self)
}

pub fn elevate(&self) -> Group {
Expand Down Expand Up @@ -666,7 +666,7 @@ impl Cell {

fn save_from_to(dyn_cell: &DynCell, target: &Cell) -> Res<()> {
let serial = dispatch_dyn_cell!(dyn_cell, |x| { x.read()?.serial()? });
target.write().set_value(OwnValue::String(serial))
target.write().value(OwnValue::String(serial))
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/base/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ impl CellReaderTrait for FieldReader {
}

impl CellWriterTrait for FieldWriter {
fn set_value(&mut self, value: OwnValue) -> Res<()> {
fn value(&mut self, value: OwnValue) -> Res<()> {
match self.ty {
FieldType::Value => self.writer.set_value(value),
FieldType::Label => self.writer.set_label(value),
FieldType::Value => self.writer.value(value),
FieldType::Label => self.writer.label(value),
FieldType::Type => userres("cannot change cell type"),
FieldType::Index => self.writer.set_index(value),
FieldType::Serial => self.writer.set_serial(value),
Expand Down
4 changes: 2 additions & 2 deletions src/base/intra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ pub trait CellWriterTrait: Debug {
todo!() // TODO: remove this default implementation
}

fn set_label(&mut self, value: OwnValue) -> Res<()> {
fn label(&mut self, value: OwnValue) -> Res<()> {
todo!() // TODO: remove this default implementation
}

fn set_value(&mut self, value: OwnValue) -> Res<()>;
fn value(&mut self, value: OwnValue) -> Res<()>;

fn set_serial(&mut self, value: OwnValue) -> Res<()> {
todo!() // TODO: remove this default implementation
Expand Down
35 changes: 27 additions & 8 deletions src/base/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use core::{
use std::borrow::{Borrow, Cow};

pub const DISPLAY_VALUE_NONE: &str = "ø"; // ❍•⸰·
pub const DISPLAY_BYTES_VALUE_LEN: usize = 72;

#[derive(Copy, Clone, Debug)]
pub enum Int {
Expand Down Expand Up @@ -174,7 +175,11 @@ impl<'a> Display for Value<'a> {
Value::Float(x) => write!(buf, "{}", x),
Value::Str(x) => write!(buf, "{}", x),
// Value::OsStr(x) => write!(buf, "{}", x.to_string_lossy()),
Value::Bytes(x) => write!(buf, "{}", String::from_utf8_lossy(x)),
Value::Bytes(x) => {
write!(buf, "⟨")?;
write_bytes(buf, x)?;
write!(buf, "⟩")
}
}
}
}
Expand All @@ -189,18 +194,32 @@ impl<'a> fmt::Debug for Value<'a> {
Value::Str(x) => write!(buf, "Value::Str({:?})", x),
// Value::OsStr(x) => write!(buf, "{}", x.to_string_lossy()),
Value::Bytes(x) => {
let sb = String::from_utf8_lossy(x);
let s = sb.as_ref();
if s.len() > 120 {
write!(buf, "Value::Bytes(len {}, \"{}\"...)", s.len(), &s[..120])
} else {
write!(buf, "Value::Bytes(len {}, \"{}\")", s.len(), s)
}
write!(buf, "Value::Bytes(len {}, \"", x.len())?;
write_bytes(buf, x)?;
write!(buf, "\")")
}
}
}
}

fn write_bytes(buf: &mut fmt::Formatter, x: &[u8]) -> fmt::Result {
let sb = String::from_utf8_lossy(x);
let s = sb.as_ref();
let not_ascii = |c| !(' '..='~').contains(&c);
if s.contains(not_ascii) {
let s = s.replace(not_ascii, ".");
if s.len() > DISPLAY_BYTES_VALUE_LEN {
write!(buf, "{}", &s[..DISPLAY_BYTES_VALUE_LEN])
} else {
write!(buf, "{}", s)
}
} else if s.len() > DISPLAY_BYTES_VALUE_LEN {
write!(buf, "{}", &s[..DISPLAY_BYTES_VALUE_LEN])
} else {
write!(buf, "{}", s)
}
}

impl<T> PartialEq<T> for Value<'_>
where
T: Borrow<str>,
Expand Down
11 changes: 6 additions & 5 deletions src/interpretations/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub(crate) struct CellReader {
files: ReadRc<FileList>,
ty: GroupType,
pos: u32,
cached_value: OnceCell<String>,
cached_value: OnceCell<Box<[u8]>>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -129,12 +129,13 @@ impl CellReaderTrait for CellReader {
let content = std::fs::read(&fe.path).map_err(|e| {
caused(HErrKind::IO, format!("cannot read file: {:?}", fe.path), e)
})?;
let content = String::from_utf8_lossy(&content);
// let content = String::from_utf8_lossy(&content);
self.cached_value
.set(content.into_owned())
.set(content.into_boxed_slice())
.map_err(|_| faulterr("cannot set cached value, it is already set"))?;
}
Ok(Value::Str(self.cached_value.get().unwrap()))
// Ok(Value::Str(self.cached_value.get().unwrap()))
Ok(Value::Bytes(self.cached_value.get().unwrap()))
}
GroupType::FileAttributes(fpos) => {
if self.pos != 0 {
Expand Down Expand Up @@ -171,7 +172,7 @@ impl CellReader {
}

impl CellWriterTrait for CellWriter {
fn set_value(&mut self, value: OwnValue) -> Res<()> {
fn value(&mut self, value: OwnValue) -> Res<()> {
let string_value = value.to_string();
let fe = self.fileentry()?;
let md = fe.metadata.as_ref().map_err(|e| e.clone())?;
Expand Down
2 changes: 1 addition & 1 deletion src/interpretations/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl CellReaderTrait for CellReader {
}

impl CellWriterTrait for CellWriter {
fn set_value(&mut self, value: OwnValue) -> Res<()> {
fn value(&mut self, value: OwnValue) -> Res<()> {
fault("set_value not yet implemented for http")
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/interpretations/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl CellReaderTrait for CellReader {
}

impl CellWriterTrait for CellWriter {
fn set_label(&mut self, label: OwnValue) -> Res<()> {
fn label(&mut self, label: OwnValue) -> Res<()> {
match self.nodes {
WriteNodeGroup::Array(_) => {
return userres("cannot set label on array object");
Expand All @@ -300,7 +300,7 @@ impl CellWriterTrait for CellWriter {
};
Ok(())
}
fn set_value(&mut self, value: OwnValue) -> Res<()> {
fn value(&mut self, value: OwnValue) -> Res<()> {
match self.nodes {
WriteNodeGroup::Array(ref mut a) => {
a[self.pos] = Node::Scalar(ownvalue_to_serde(value));
Expand Down
2 changes: 1 addition & 1 deletion src/interpretations/ownvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl CellReaderTrait for CellReader {
}

impl CellWriterTrait for CellWriter {
fn set_value(&mut self, value: OwnValue) -> Res<()> {
fn value(&mut self, value: OwnValue) -> Res<()> {
*self.0 = value;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/interpretations/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl CellReader {
}

impl CellWriterTrait for CellWriter {
fn set_value(&mut self, value: OwnValue) -> Res<()> {
fn value(&mut self, value: OwnValue) -> Res<()> {
match value {
OwnValue::String(s) => {
*(self.0) = PathBuf::from(s);
Expand Down
Loading

0 comments on commit e8745b1

Please sign in to comment.