Skip to content

Commit

Permalink
fix fs trying to write back to its origin
Browse files Browse the repository at this point in the history
  • Loading branch information
emanueldima committed Mar 21, 2024
1 parent 1762beb commit fb85583
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 15 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,24 @@ Change the default mysql port systemwide:
```bash
# shell
hial '/etc/mysql/my.cnf^fs[w]^ini/mysqld/port = 3307'
# 🚧 todo: assign operator
```

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

Change the user's docker configuration:
```bash
# shell
hial '~/.docker/config.json^json/auths/docker.io/username = "newuser"'
# 🚧 todo: assign operator
hial '~/.docker/config.json^fs[w]^json/auths/docker.io/username = "newuser"'
# 🚧 todo: create new object entities on write
```
```rust
// rust
Cell::from("~/.docker/config.json")
.to("^fs[w]^json/auths/docker.io/username")
Cell::new("~/.docker/config.json^fs[w]^json/auths/docker.io/username")
.write()
.value("newuser")?;
```
Expand Down Expand Up @@ -116,6 +113,7 @@ Split a markdown file into sections and put each in a separate file:

##### 4. Transform data from one format or shape into another.


Transform a json file into an xml file with the same format and vice versa:

```bash
Expand Down
1 change: 1 addition & 0 deletions doc/issues.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# List of Todos and other Issues

- stop fs from writing back to the path cell
- add split(":") interpretation, read-write
- '**[filter]' must work as '**/*[filter]' (filter to be applied only on leaves)
- support rust/ts write: `hial './src/tests/rust.rs^rust/*[:function_item].label = "modified_fn_name"'`
Expand Down
4 changes: 3 additions & 1 deletion src/api/interpretation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ pub trait CellReaderTrait: Debug {
fn value(&self) -> Res<Value>;

/// provide a serialization of the data from the cell and its descendants
/// to be used for saving the data to a data store
/// to be used for saving the data to a data store.
/// If this returns HErrKind::None, then no data needs to be saved
/// (i.e. the data is already fully persisted).
fn serial(&self) -> Res<String>;
}

Expand Down
13 changes: 12 additions & 1 deletion src/api/xell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,18 @@ impl Xell {
}

fn save_from_to(dyn_cell: &DynCell, target: &Xell) -> Res<()> {
let serial = dispatch_dyn_cell!(dyn_cell, |x| { x.read()?.serial()? });
let serial = dispatch_dyn_cell!(dyn_cell, |x| {
match x.read()?.serial() {
Ok(serial) => serial,
Err(e) => {
if e.kind == HErrKind::None {
return Ok(()); // no serial, nothing to save
} else {
return Err(e);
}
}
}
});
target.write().value(OwnValue::String(serial))
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/prog/parse_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn statement_path(input: &str) -> NomRes<&str, Statement> {
fn statement_assignment(input: &str) -> NomRes<&str, Statement> {
context(
"assignment",
tuple((path_with_starter, space0, tag("="), space0, value_uint)),
tuple((path_with_starter, space0, tag("="), space0, rvalue)),
)(input)
.map(|(next_input, res)| (next_input, Statement::Assignment(res.0 .0, res.0 .1, res.4)))
}
2 changes: 1 addition & 1 deletion src/tests/data/write3.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
B
A
26 changes: 22 additions & 4 deletions src/tests/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,30 @@ fn fs_path() -> Res<()> {
fn fs_write_beyond_fs() -> Res<()> {
// test that the fs drop will not try to write back to the path cell itself
let ov = Xell::from("./src/tests/data/write3.txt").policy(WritePolicy::WriteBackOnDrop);

// A is the initial value
assert_eq!(
ov.be("path").be("fs").read().value()?,
Value::Bytes("A".as_bytes())
);

// write B and drop, should write automatically
{
ov.be("path").be("fs").write().value("A")?;
ov.be("path").be("fs").write().value("B")?;
// fs should try to write back now
}
// TODO: text failure: stop fs from writing back to the path cell
// assert_eq!(ov.be("path").be("fs").read().value()?, "Hi there");
assert_eq!(
ov.be("path").be("fs").read().value()?,
Value::Bytes("B".as_bytes())
);

// write A again to go back to the initial state
{
ov.be("path").be("fs").write().value("A")?;
}
assert_eq!(
ov.be("path").be("fs").read().value()?,
Value::Bytes("A".as_bytes())
);

Ok(())
}

0 comments on commit fb85583

Please sign in to comment.