Skip to content

Commit a498764

Browse files
committed
Fixed return types, and method of opening files
1 parent 5d29cf1 commit a498764

3 files changed

Lines changed: 21 additions & 40 deletions

File tree

src/lib.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,15 @@
1212
//! - Allows specifying a custom RDF namespace for generated predicates and objects.
1313
//! - Outputs the RDF data to a specified file or prints it to the console.
1414
15+
use clap::Error;
1516
use oxrdf::vocab::xsd;
1617
use oxrdf::{BlankNode, Graph, Literal, NamedNodeRef, TripleRef};
1718

1819
use serde_json::{Deserializer, Value};
1920
use std::collections::VecDeque;
20-
use std::fs::File;
21+
use std::fs::{File, OpenOptions};
2122
use std::io::{BufReader, Write};
2223

23-
pub enum GraphOrMessage {
24-
Graph(Graph),
25-
Message(String),
26-
}
27-
2824
/// Converts JSON data to RDF format.
2925
///
3026
/// This function reads JSON data from the specified file, processes it into RDF triples,
@@ -47,7 +43,7 @@ pub fn json_to_rdf(
4743
file_path: &String,
4844
namespace: &Option<String>,
4945
output_file: &Option<String>,
50-
) -> Result<GraphOrMessage, String> {
46+
) -> Result<Graph, Error> {
5147
let rdf_namespace: String = if namespace.is_some() {
5248
namespace.clone().unwrap()
5349
} else {
@@ -109,14 +105,16 @@ pub fn json_to_rdf(
109105
}
110106

111107
if let Some(output_path) = output_file {
112-
let mut file = File::create(output_path).expect("Error creating file");
113-
writeln!(file, "{}", graph).expect("Error writing to file");
114-
Ok(GraphOrMessage::Message(format!(
115-
"RDF created at: {}",
116-
output_path
117-
)))
108+
let mut file = OpenOptions::new()
109+
.create(true)
110+
.append(true)
111+
.open(output_path)
112+
.expect("Error opening file");
113+
114+
writeln!(file, "{}", graph).expect("Error writing json2rdf data to file");
115+
Ok(graph)
118116
} else {
119-
Ok(GraphOrMessage::Graph(graph))
117+
return Ok(graph);
120118
}
121119
}
122120

src/main.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
//! ```
2222
//! This will take `data.json`, apply the specified namespace, and save the RDF output in `output.nt`.
2323
use clap::{Parser, Subcommand};
24-
mod lib;
24+
use json2rdf::*;
2525

2626
/// Command-line interface for JSON2RDF Converter
2727
///
@@ -73,21 +73,10 @@ fn main() {
7373
namespace,
7474
json_files,
7575
output_file,
76-
}) => {
77-
match lib::json_to_rdf(json_files, namespace, output_file) {
78-
Ok(res) => match res {
79-
lib::GraphOrMessage::Graph(graph) => {
80-
// Handle the case where the function returns a Graph
81-
println!("Graph created with {} triples", graph.len());
82-
}
83-
lib::GraphOrMessage::Message(message) => {
84-
// Handle the case where the function returns a success message
85-
println!("{}", message);
86-
}
87-
},
88-
Err(e) => eprintln!("Error writing: {}", e),
89-
}
90-
}
76+
}) => match json_to_rdf(json_files, namespace, output_file) {
77+
Ok(_) => {}
78+
Err(e) => eprintln!("Error writing: {}", e),
79+
},
9180
None => {}
9281
}
9382
}

tests/integration_test.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,10 @@ fn test_graph_triple_count() {
55
let triple_count_string = json_to_rdf(&"tests/airplane.json".to_string(), &None, &None);
66

77
match triple_count_string {
8-
Ok(res) => match res {
9-
json2rdf::GraphOrMessage::Graph(graph) => {
10-
// Handle the case where the function returns a Graph
11-
println!("Graph created with {} triples", graph.len());
12-
assert_eq!(graph.len(), 23)
13-
}
14-
json2rdf::GraphOrMessage::Message(message) => {
15-
println!("{}", message);
16-
}
17-
},
8+
Ok(res) => {
9+
println!("Graph created with {} triples", res.len());
10+
assert_eq!(res.len(), 23)
11+
}
1812
Err(e) => eprintln!("Error writing: {}", e),
1913
}
2014
}

0 commit comments

Comments
 (0)