Skip to content

Commit

Permalink
add more write scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
emanueldima committed Feb 8, 2024
1 parent f831616 commit 92f0fae
Show file tree
Hide file tree
Showing 16 changed files with 291 additions and 220 deletions.
2 changes: 1 addition & 1 deletion issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

- fix double kleene error (see test)
- support type selector: `hial './src/tests/rust.rs^rust/:function_item'`
- support assignment (write): `hial './src/tests/rust.rs^rust/:function_item[-1]#label = "modified_fn_name"'`
- support ts write: `hial './src/tests/rust.rs^rust/:function_item[-1]#label = "modified_fn_name"'`
- set value on the command line
- later: separate api module, used by ffi and dependent crates

Expand Down
20 changes: 10 additions & 10 deletions src/base/extra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ impl CellReaderTrait for CellReader {
}
}
impl CellReader {
pub fn as_file_path(&self) -> Res<&std::path::Path> {
if let DynCellReader::File(ref file_cell) = self.0 {
return file_cell.as_file_path();
}
if let DynCellReader::Path(ref path_cell) = self.0 {
return path_cell.as_file_path();
}
userres("this interpretation has no file path")
}

pub fn err(self) -> Res<CellReader> {
if let DynCellReader::Error(error) = self.0 {
return Err(error);
Expand Down Expand Up @@ -590,16 +600,6 @@ impl Cell {
s
}

pub fn as_file_path(&self) -> Res<&std::path::Path> {
if let DynCell::File(ref file_cell) = self.dyn_cell {
return file_cell.as_path();
}
if let DynCell::Path(ref path_cell) = self.dyn_cell {
return path_cell.as_path();
}
nores().with_path_res(self.path())
}

pub fn save(&self, target: Cell) -> Res<()> {
if let DynCell::Error(err) = &self.dyn_cell {
return Err(err.clone());
Expand Down
8 changes: 7 additions & 1 deletion src/base/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ impl CellReaderTrait for FieldReader {

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

pub trait CellWriterTrait: Debug {
fn set_value(&mut self, value: OwnValue) -> Res<()>;
fn set_index(&mut self, value: OwnValue) -> Res<()> {
todo!() // TODO: remove this default implementation
}

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

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

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

fn delete(&mut self) -> Res<()> {
todo!() // TODO: remove this default implementation
}
Expand All @@ -60,7 +68,7 @@ pub trait GroupTrait: Clone + Debug {
// fn get_all<'s, 'a, S: Into<Selector<'a>>>(&'s self, label: S) -> Res<Self::SelectIterator>;

fn add(&mut self) -> Res<()> {
todo!() // remove this default implementation
todo!() // TODO: remove this default implementation
}
}

Expand Down
25 changes: 10 additions & 15 deletions src/interpretations/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ impl CellReaderTrait for CellReader {
}
}

impl CellReader {
pub(crate) fn as_file_path(&self) -> Res<&Path> {
let fileentry =
guard_ok!(&self.group.files[self.pos as usize], err => {return Err(err.clone())});
Ok(fileentry.path.as_path())
}
}

impl CellWriterTrait for CellWriter {
fn set_value(&mut self, value: OwnValue) -> Res<()> {
let string_value = value.to_string();
Expand Down Expand Up @@ -184,19 +192,12 @@ impl CellWriterTrait for CellWriter {
}
}
}

fn set_label(&mut self, value: OwnValue) -> Res<()> {
todo!() // add implementation
}

fn delete(&mut self) -> Res<()> {
todo!() // add implementation
}
}

impl Cell {
pub(crate) fn from_cell(cell: XCell, _: &str) -> Res<XCell> {
let path = cell.as_file_path()?;
let r = cell.read();
let path = r.as_file_path()?;
let file_cell = Cell {
group: Group {
files: Rc::new(vec![read_file(path)]),
Expand All @@ -218,12 +219,6 @@ impl Cell {
};
Ok(new_cell(DynCell::from(file_cell), None))
}

pub(crate) fn as_path(&self) -> Res<&Path> {
let fileentry =
guard_ok!(&self.group.files[self.pos as usize], err => {return Err(err.clone())});
Ok(fileentry.path.as_path())
}
}

impl CellTrait for Cell {
Expand Down
2 changes: 1 addition & 1 deletion src/interpretations/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl CellReaderTrait for CellReader {

impl CellWriterTrait for CellWriter {
fn set_value(&mut self, value: OwnValue) -> Res<()> {
todo!() // remove this default implementation
fault("set_value not yet implemented for http")
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/interpretations/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ impl Cell {
(serde_json::from_str(s.as_ref())?, indent)
}
"fs" => {
let path = cell.as_file_path()?;
let r = cell.read();
let path = r.as_file_path()?;
let indent = detect_file_indentation(path);
(
serde_json::from_reader(
Expand Down
57 changes: 39 additions & 18 deletions src/interpretations/path.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use std::{
cell::OnceCell,
path::{Path, PathBuf},
rc::Rc,
};

use linkme::distributed_slice;

use crate::base::{Cell as XCell, *};
use crate::{
base::{Cell as XCell, *},
utils::ownrc::{OwnRc, ReadRc, WriteRc},
};

#[distributed_slice(ELEVATION_CONSTRUCTORS)]
static VALUE_TO_PATH: ElevationConstructor = ElevationConstructor {
Expand All @@ -15,16 +18,13 @@ static VALUE_TO_PATH: ElevationConstructor = ElevationConstructor {
};

#[derive(Clone, Debug)]
pub(crate) struct Data(PathBuf, String);

#[derive(Clone, Debug)]
pub(crate) struct Cell(Rc<Data>);
pub(crate) struct Cell(OwnRc<PathBuf>);

#[derive(Debug)]
pub(crate) struct CellReader(Rc<Data>);
pub(crate) struct CellReader(ReadRc<PathBuf>, OnceCell<String>);

#[derive(Debug)]
pub(crate) struct CellWriter(Rc<Data>);
pub(crate) struct CellWriter(WriteRc<PathBuf>);

impl Cell {
pub(crate) fn from_cell(cell: XCell, _: &str) -> Res<XCell> {
Expand All @@ -37,7 +37,8 @@ impl Cell {
Self::make_cell(PathBuf::from(value), value.to_owned(), Some(cell))
}
"fs" => {
let path = cell.as_file_path()?;
let r = cell.read();
let path = r.as_file_path()?;
Self::make_cell(
path.to_owned(),
path.to_string_lossy().into_owned(),
Expand All @@ -49,18 +50,17 @@ impl Cell {
}

fn make_cell(path: PathBuf, string: String, origin: Option<XCell>) -> Res<XCell> {
let path_cell = Cell(Rc::new(Data(path, string)));
let path_cell = Cell(OwnRc::new(path));
Ok(new_cell(DynCell::from(path_cell), origin))
}

pub(crate) fn as_path(&self) -> Res<&Path> {
Ok(self.0 .0.as_path())
}
}

impl CellReaderTrait for CellReader {
fn value(&self) -> Res<Value> {
Ok(Value::Str(&self.0 .1))
let s = self
.1
.get_or_init(|| self.0.as_os_str().to_string_lossy().to_string());
Ok(Value::Str(s))
}

fn label(&self) -> Res<Value> {
Expand All @@ -76,9 +76,21 @@ impl CellReaderTrait for CellReader {
}
}

impl CellReader {
pub(crate) fn as_file_path(&self) -> Res<&Path> {
Ok(self.0.as_path())
}
}

impl CellWriterTrait for CellWriter {
fn set_value(&mut self, value: OwnValue) -> Res<()> {
todo!() // remove this default implementation
match value {
OwnValue::String(s) => {
*(self.0) = PathBuf::from(s);
Ok(())
}
_ => userres(format!("cannot set fs path to {:?}", value)),
}
}
}

Expand All @@ -96,11 +108,20 @@ impl CellTrait for Cell {
}

fn read(&self) -> Res<Self::CellReader> {
Ok(CellReader(self.0.clone()))
Ok(CellReader(
self.0
.read()
.ok_or_else(|| lockerr("cannot lock path for reading"))?,
OnceCell::new(),
))
}

fn write(&self) -> Res<Self::CellWriter> {
Ok(CellWriter(self.0.clone()))
Ok(CellWriter(
self.0
.write()
.ok_or_else(|| lockerr("cannot lock path for writing"))?,
))
}

fn head(&self) -> Res<(Self, Relation)> {
Expand Down
3 changes: 2 additions & 1 deletion src/interpretations/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ impl Cell {
Self::make_cell(value, Some(cell))
}
"fs" => {
let path = cell.as_file_path()?;
let r = cell.read();
let path = r.as_file_path()?;
Self::make_cell(
&std::fs::read_to_string(path)
.map_err(|e| caused(HErrKind::IO, "cannot read file", e))?,
Expand Down
5 changes: 3 additions & 2 deletions src/interpretations/treesitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ impl Cell {
Self::make_cell(source, lang.to_owned(), Some(cell))
}
"fs" => {
let path = cell.as_file_path()?;
let r = cell.read();
let path = r.as_file_path()?;
let source = std::fs::read_to_string(path)
.map_err(|e| caused(HErrKind::IO, "cannot read file", e))?;
Self::make_cell(source, lang.to_owned(), Some(cell))
Expand Down Expand Up @@ -190,7 +191,7 @@ impl CellWriterTrait for Cell {
fn set_value(&mut self, value: OwnValue) -> Res<()> {
// TODO: not clear how to edit the tree because afterwards
// some cursors/cells will be invalid
todo!()
todo!() // TODO: implement this somehow
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/interpretations/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,21 @@ impl CellReaderTrait for CellReader {
}
}

impl CellReader {
pub(crate) fn as_url(&self) -> Res<Url> {
Ok(self.0.url.clone())
}
}

impl CellWriterTrait for CellWriter {
fn set_value(&mut self, value: OwnValue) -> Res<()> {
todo!()
match value {
OwnValue::String(s) => {
let url = Url::parse(s.as_str())?;
Ok(())
}
_ => userres(format!("cannot set url from non-string value {:?}", value)),
}
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/interpretations/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ impl Cell {
pub(crate) fn from_cell(cell: XCell, _: &str) -> Res<XCell> {
match cell.interpretation() {
"fs" => {
let path = cell.as_file_path()?;
let r = cell.read();
let path = r.as_file_path()?;
let file = File::open(path).map_err(|e| {
caused(
HErrKind::InvalidFormat,
Expand Down Expand Up @@ -413,14 +414,14 @@ impl CellReaderTrait for CellReader {
Writer::new_with_indent(Cursor::new(Vec::new()), first as u8, indent.len())
};
serialize_node(&mut writer, &nodes[*pos])?;
let ser = String::from_utf8(writer.into_inner().into_inner()).map_err(|e| {
let ser = writer.into_inner().into_inner();
String::from_utf8(ser).map_err(|e| {
caused(
HErrKind::InvalidFormat,
"invalid utf8 in xml serialization",
e,
)
});
ser
})
}
CellReader::Attr { nodes, pos, .. } => match &nodes[*pos] {
Attribute::Attribute(k, v) => Ok(format!("{}=\"{}\"", k, v)),
Expand Down
3 changes: 2 additions & 1 deletion src/interpretations/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ impl Cell {
}
"fs" => {
let mut source = String::new();
let path = cell.as_file_path()?;
let r = cell.read();
let path = r.as_file_path()?;
File::open(path)
.map_err(|e| {
caused(
Expand Down
Loading

0 comments on commit 92f0fae

Please sign in to comment.