Skip to content

Commit

Permalink
Merge pull request #3 from jgarzik/hacking
Browse files Browse the repository at this point in the history
Hacking
  • Loading branch information
jgarzik committed Feb 13, 2024
2 parents 35a22bc + 300d434 commit d1a17e2
Showing 1 changed file with 83 additions and 69 deletions.
152 changes: 83 additions & 69 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ impl Pcc {
}
}

// Read a single LST record
fn read_lst_line(&mut self, line: &str) -> io::Result<()> {
let mut tags: Vec<&str> = line.split('\t').collect();
let ident = tags.remove(0);
println!("ID={}, {:?}", ident, tags);
Ok(())
}

// Read LST file into data dictionary
pub fn read_lst(&mut self, basedir: &str, lstpath: &str, lstopts: &str) -> io::Result<()> {
let mut fpath = String::new();
Expand Down Expand Up @@ -166,7 +174,80 @@ impl Pcc {
continue;
}

// TODO
self.read_lst_line(&line)?;
}

Ok(())
}

fn read_pcc_line(&mut self, basedir: &str, line: &str) -> io::Result<()> {
// split on ':'
let sor = line.split_once(':');
if sor.is_none() {
return Err(Error::new(ErrorKind::Other, "PCC invalid line:colon"));
}

let mut lhs;
let rhs;
(lhs, rhs) = sor.unwrap();
let _tag_negate;

if lhs.chars().next() == Some('!') {
lhs = &lhs[1..];
_tag_negate = true;
} else {
_tag_negate = false;
}

// is this tag in the known schema?
let tagtype_res = self.pcc_schema.get(lhs);
if tagtype_res.is_none() {
return Err(Error::new(
ErrorKind::Other,
format!("PCC invalid key {}", lhs),
));
}

let tagtype = tagtype_res.unwrap();
match tagtype {
// input included PCC file
TagType::ReadPcc => {
// relative path indicated by leading '@'
let (is_rel, fpath);
if rhs.chars().nth(0) == Some('@') {
is_rel = true;
fpath = &rhs[1..];
} else {
is_rel = false;
fpath = &rhs;
}

self.read(fpath, is_rel)?;
}

// read LST file
TagType::List => match rhs.split_once('|') {
None => self.read_lst(&basedir, rhs, String::from("").as_str())?,
Some((lstpath, lstopts)) => self.read_lst(&basedir, lstpath, lstopts)?,
},

// handle other data types
TagType::Bool | TagType::Date | TagType::Number | TagType::Text => {
// store in global data dictionary
let tag = self.dict.get_mut(lhs);
match tag {
// new key; store in hashmap
None => {
self.dict.insert(lhs.to_string(), rhs.to_string());
}

// existing key; append to string value
Some(val) => {
val.push_str("\n");
val.push_str(rhs);
}
}
}
}

Ok(())
Expand Down Expand Up @@ -202,74 +283,7 @@ impl Pcc {
continue;
}

// split on ':'
let sor = line.split_once(':');
if sor.is_none() {
return Err(Error::new(ErrorKind::Other, "PCC invalid line:colon"));
}

let mut lhs;
let rhs;
(lhs, rhs) = sor.unwrap();
let _tag_negate;

if lhs.chars().next() == Some('!') {
lhs = &lhs[1..];
_tag_negate = true;
} else {
_tag_negate = false;
}

// is this tag in the known schema?
let tagtype_res = self.pcc_schema.get(lhs);
if tagtype_res.is_none() {
return Err(Error::new(
ErrorKind::Other,
format!("PCC invalid key {}", lhs),
));
}

let tagtype = tagtype_res.unwrap();
match tagtype {
// input included PCC file
TagType::ReadPcc => {
// relative path indicated by leading '@'
let (is_rel, fpath);
if rhs.chars().nth(0) == Some('@') {
is_rel = true;
fpath = &rhs[1..];
} else {
is_rel = false;
fpath = &rhs;
}

self.read(fpath, is_rel)?;
}

// read LST file
TagType::List => match rhs.split_once('|') {
None => self.read_lst(&basedir, rhs, String::from("").as_str())?,
Some((lstpath, lstopts)) => self.read_lst(&basedir, lstpath, lstopts)?,
},

// handle other data types
TagType::Bool | TagType::Date | TagType::Number | TagType::Text => {
// store in global data dictionary
let tag = self.dict.get_mut(lhs);
match tag {
// new key; store in hashmap
None => {
self.dict.insert(lhs.to_string(), rhs.to_string());
}

// existing key; append to string value
Some(val) => {
val.push_str("\n");
val.push_str(rhs);
}
}
}
}
self.read_pcc_line(&basedir, &line)?;
}

Ok(())
Expand Down

0 comments on commit d1a17e2

Please sign in to comment.