snark_tool/service/io/
reader_json.rs

1use crate::graph::graph::{Graph, GraphConstructor};
2use crate::procedure::basic_procedures::write::GraphWithProperties;
3use crate::procedure::procedure::GraphProperties;
4use crate::service::io::error::ReadError;
5use crate::service::io::reader::GraphFileReader;
6use crate::service::io::reader_g6::G6Reader;
7use crate::service::io::reader_s6::S6Reader;
8use std::fs::File;
9use std::io::Read;
10use std::{fs, marker, result};
11
12type Result<T> = result::Result<T, ReadError>;
13
14pub struct JsonReader<'a, G> {
15    file: &'a fs::File,
16    parsed: bool,
17    position: usize,
18    graphs: Vec<GraphWithProperties>,
19    _ph: marker::PhantomData<G>,
20}
21
22impl<'a, G> GraphFileReader<'a, G> for JsonReader<'a, G>
23where
24    G: Graph + GraphConstructor,
25{
26    fn new(file: &'a File) -> Self {
27        JsonReader {
28            file,
29            parsed: false,
30            position: 0,
31            graphs: vec![],
32            _ph: marker::PhantomData,
33        }
34    }
35
36    fn next(&mut self) -> Option<Result<G>> {
37        unimplemented!();
38
39        // if !self.parsed {
40        //     self.parse_file();
41        // }
42        // if self.position < self.graphs.len() {
43        //     self.position += 1;
44        //     // read graph by format at position
45        //     // return graph
46        // }
47        // None
48    }
49}
50
51impl<'a, G: Graph + GraphConstructor> JsonReader<'a, G> {
52    fn parse_file(&mut self) -> Result<()> {
53        let file_string = &mut "".to_string();
54        self.file.read_to_string(file_string)?;
55        let graphs_res = serde_json::from_str(file_string);
56        self.graphs = graphs_res.unwrap();
57        self.parsed = true;
58        Ok(())
59    }
60
61    pub fn next_with_properties(&mut self) -> Option<Result<(G, GraphProperties)>> {
62        if !self.parsed {
63            let parse_result = self.parse_file();
64            if parse_result.is_err() {
65                return Some(Err(parse_result.err().unwrap()));
66            }
67        }
68        if self.position < self.graphs.len() {
69            let graph_with_properties = &self.graphs[self.position];
70            self.position += 1;
71            let graph = Self::read_graph(
72                &graph_with_properties.graph,
73                &graph_with_properties.graph_format,
74            );
75            if graph.is_err() {
76                return Some(Err(graph.err().unwrap()));
77            }
78            let result = (graph.unwrap(), graph_with_properties.properties.clone());
79            return Some(Ok(result));
80        }
81        None
82    }
83
84    fn read_graph(graph: &String, graph_format: &String) -> Result<G> {
85        match graph_format.as_str() {
86            "g6" => {
87                return G6Reader::read_graph(graph);
88            }
89            "s6" => {
90                return S6Reader::read_graph(graph);
91            }
92            _ => {
93                return Err(ReadError {
94                    message: format!(
95                        "unknown graph format to read from json object: {}",
96                        graph_format
97                    ),
98                });
99            }
100        }
101    }
102}