Parse binary data into the parse context.
(input: &'a str, context: &mut ParseContext)
| 21 | |
| 22 | /// Parse binary data into the parse context. |
| 23 | pub fn parse_data<'a>(input: &'a str, context: &mut ParseContext) -> IResult<&'a str, (), StofParseError> { |
| 24 | let (input, _) = whitespace(input)?; |
| 25 | |
| 26 | // Optionally start the data declaration with "data" |
| 27 | let (input, version) = opt(preceded(tag("data"), opt(preceded(char('@'), take_until(" "))))).parse(input)?; |
| 28 | let (input, _) = space0(input)?; |
| 29 | |
| 30 | // Get version of the data being parsed as a string |
| 31 | if let Some(version) = version { |
| 32 | if let Some(_version) = version { |
| 33 | // Use this version in the future if/when binary data format changes |
| 34 | //println!("DATA VERSION: {version}"); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Parse the binary data like a normal blob |
| 39 | let (input, bytes) = delimited( |
| 40 | char('|'), |
| 41 | terminated(separated_list1(char(','), blob_number), whitespace), |
| 42 | alt(( |
| 43 | preceded(char(','), preceded(whitespace, char('|'))), |
| 44 | char('|') |
| 45 | )) |
| 46 | ).parse(input)?; |
| 47 | |
| 48 | // Optionally end the binary data declaration with a semicolon or a comma |
| 49 | let (input, _) = opt(preceded(space0, alt((char(';'), char(','))))).parse(input)?; |
| 50 | |
| 51 | let self_ptr = context.self_ptr(); |
| 52 | if let Ok(mut data) = bincode::deserialize::<Data>(&bytes) { |
| 53 | // avoid colliding with existing data |
| 54 | if data.id.data_exists(&context.graph) { |
| 55 | data.id = SId::default(); |
| 56 | } |
| 57 | |
| 58 | // remove existing nodes (inserting a new data) |
| 59 | data.nodes.clear(); |
| 60 | |
| 61 | if let Some(_dref) = context.graph.insert_data(&self_ptr, data) { |
| 62 | return Ok((input, ())); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Do not throw an error here - failing to load data should be allowed for various reasons... |
| 67 | let path = self_ptr.node_path(&context.graph, true).unwrap().join("."); |
| 68 | println!("{} {} {}", "failed to deserialize stof data @".dimmed(), path.purple(), "missing data".red()); |
| 69 | return Ok((input, ())); |
| 70 | } |
no test coverage detected