Skip to content

Commit

Permalink
Merge pull request #22 from THCLab/feat/deref
Browse files Browse the repository at this point in the history
Support dereference of refn
  • Loading branch information
mitfik authored Dec 5, 2023
2 parents e37d6a4 + bad8363 commit 47278ce
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
17 changes: 15 additions & 2 deletions oca-file/src/ocafile/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod error;
mod instructions;

use std::collections::HashMap;

use convert_case::{Case, Casing};
use self::instructions::{add::AddInstruction, from::FromInstruction, remove::RemoveInstruction};
use crate::ocafile::error::Error;
Expand Down Expand Up @@ -130,7 +132,7 @@ pub fn parse_from_string(unparsed_file: String) -> Result<OCAAst, ParseError> {
Ok(oca_ast)
}

pub fn generate_from_ast(ast: &OCAAst) -> String {
pub fn generate_from_ast(ast: &OCAAst, references: Option<HashMap<String, String>>) -> String {
let mut ocafile = String::new();

ast.commands.iter().for_each(|command| {
Expand All @@ -150,7 +152,18 @@ pub fn generate_from_ast(ast: &OCAAst) -> String {
if let ast::NestedValue::Reference(value) = value {
match value {
ast::RefValue::Name(refn) => {
line.push_str(format!("{}=refn:{} ", key, refn).as_str());
match references {
Some(ref references) => {
if let Some(refs) = references.get(refn) {
line.push_str(format!("{}=refs:{} ", key, refs).as_str());
} else {
panic!("Reference not found: {}", refn)
}
},
None => {
line.push_str(format!("{}={} ", key, refn).as_str());
}
}
}
ast::RefValue::Said(refs) => {
line.push_str(format!("{}=refs:{} ", key, refs).as_str());
Expand Down
20 changes: 16 additions & 4 deletions oca/src/facade/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use oca_bundle::state::oca::{
capture_base::CaptureBase, DynOverlay, OCABundle,
};

#[cfg(feature = "local-references")]
use std::collections::HashMap;
use std::rc::Rc;
use serde::Serialize;
Expand Down Expand Up @@ -316,13 +315,26 @@ impl Facade {
Ok(history)
}

pub fn get_oca_bundle_ocafile(&self, said: String) -> Result<String, Vec<String>> {
pub fn get_oca_bundle_ocafile(&self, said: String, dereference: bool) -> Result<String, Vec<String>> {
let oca_bundle_steps = self.get_oca_bundle_steps(said)?;
let mut oca_ast = OCAAst::new();
for step in oca_bundle_steps {
oca_ast.commands.push(step.command);
};

let mut refs: Option<HashMap<String, String>> = None;
if dereference {
let mut local_refs = HashMap::new();
#[cfg(feature = "local-references")]
self.db.get_all(Namespace::OCAReferences).unwrap()
.iter()
.for_each(|(k, v)| {
local_refs.insert(k.clone(), String::from_utf8(v.to_vec()).unwrap());
});
refs = Some(local_refs);
}
Ok(oca_file::ocafile::generate_from_ast(&oca_ast))

Ok(oca_file::ocafile::generate_from_ast(&oca_ast, refs))
}

pub fn get_oca_bundle_ast(&self, said: String) -> Result<OCAAst, Vec<String>> {
Expand All @@ -336,7 +348,7 @@ impl Facade {

pub fn parse_oca_bundle_to_ocafile(&self, bundle: &OCABundle) -> Result<String, Vec<String>> {
let oca_ast = bundle.to_ast();
Ok(oca_file::ocafile::generate_from_ast(&oca_ast))
Ok(oca_file::ocafile::generate_from_ast(&oca_ast, None))
}
}

Expand Down

0 comments on commit 47278ce

Please sign in to comment.