MCPcopy Create free account
hub / github.com/dev-formata-io/stof / parse_data

Function parse_data

src/parser/data.rs:23–70  ·  view source on GitHub ↗

Parse binary data into the parse context.

(input: &'a str, context: &mut ParseContext)

Source from the content-addressed store, hash-verified

21
22/// Parse binary data into the parse context.
23pub 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}

Callers 1

document_statementFunction · 0.85

Calls 9

whitespaceFunction · 0.85
defaultFunction · 0.85
data_existsMethod · 0.80
insert_dataMethod · 0.80
joinMethod · 0.80
node_pathMethod · 0.80
parseMethod · 0.45
self_ptrMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected