From ae2c9e79be558009b081cbbb823ae65e9b41ac96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20N=C3=BCtzi?= Date: Tue, 25 Jun 2024 22:10:00 +0200 Subject: [PATCH] feat: Add static dispatch for writer/reader (#18) Co-authored-by: cmdoret --- Cargo.lock | 21 +++++++++++++++++++++ Cargo.toml | 1 + src/io.rs | 32 +++++++++++++++++++++++--------- src/main.rs | 1 + src/pass_first.rs | 8 ++++++-- src/pass_second.rs | 6 ++---- 6 files changed, 54 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f41ebe2..1c01368 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -133,6 +133,17 @@ dependencies = [ "powerfmt", ] +[[package]] +name = "derive_utils" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61bb5a1014ce6dfc2a378578509abe775a5aa06bff584a547555d9efdb81b926" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "dirs-next" version = "2.0.0" @@ -215,6 +226,15 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "io-enum" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53b53d712d99a73eec59ee5e4fe6057f8052142d38eeafbbffcb06b36d738a6e" +dependencies = [ + "derive_utils", +] + [[package]] name = "is-terminal" version = "0.4.12" @@ -335,6 +355,7 @@ version = "0.1.0" dependencies = [ "bitflags", "clap", + "io-enum", "rio_api", "rio_turtle", "serde", diff --git a/Cargo.toml b/Cargo.toml index 6eda731..33b985f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,5 +17,6 @@ clap = { version = "4.5.7", features = ["derive"] } rio_turtle = "0.8.4" rio_api = "0.8.4" bitflags = "2.5.0" +io-enum = "1.1.3" serde_yml = "0.0.10" tempfile = "3.10.1" diff --git a/src/io.rs b/src/io.rs index 941100f..3983da1 100644 --- a/src/io.rs +++ b/src/io.rs @@ -4,23 +4,37 @@ use serde_yml; use std::{ boxed::Box, fs::File, - io::{stdin, stdout, BufRead, BufReader, BufWriter, Write}, + io::{self, stdin, stdout, BufRead, BufReader, BufWriter, Write}, path::Path, }; -/// Get a reader based on input path, either from stdin or a file. -pub fn get_reader(path: &Path) -> Box { +use io_enum::{BufRead, Read, Write}; + +#[derive(Read, BufRead)] +pub enum Reader { + Stdio(BufReader), + File(BufReader), +} + +#[derive(Write)] +pub enum Writer { + Stdio(BufWriter), + File(BufWriter), +} + +// Get a reader based on input path, either from stdin or a file. +pub fn get_reader(path: &Path) -> Reader { return match path.to_str().unwrap() { - "-" => Box::new(BufReader::new(stdin())), - _ => Box::new(BufReader::new(File::open(&path).unwrap())), + "-" => Reader::Stdio(BufReader::new(stdin())), + path => Reader::File(BufReader::new(File::open(path).unwrap())), }; } -/// Get a writer based on input path, either to stdout or a file. -pub fn get_writer(path: &Path) -> Box { +// Get a writer based on input path, either to stdout or a file. +pub fn get_writer(path: &Path) -> Writer { return match path.to_str().unwrap() { - "-" => Box::new(BufWriter::new(stdout())), - path => Box::new(BufWriter::new(File::create(path).unwrap())), + "-" => Writer::Stdio(BufWriter::new(stdout())), + path => Writer::File(BufWriter::new(File::create(path).unwrap())), }; } diff --git a/src/main.rs b/src/main.rs index a24b009..023439e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -86,6 +86,7 @@ fn main() { Subcommands::Pseudo(args) => { info!(log, "Args: {:?}", args); pseudonymize_graph(&log, &args.input, &args.config, &args.output, &args.index) + } } } diff --git a/src/pass_first.rs b/src/pass_first.rs index c71e1b1..fb378e2 100644 --- a/src/pass_first.rs +++ b/src/pass_first.rs @@ -1,6 +1,10 @@ -use rio_api::{model::Triple, parser::TriplesParser}; +use std::path::Path; +use std::io::{BufRead, BufReader, stdin, Write}; +use rio_api::{ + parser::TriplesParser, + model::Triple, +}; use rio_turtle::TurtleError; -use std::{io::Write, path::Path}; use crate::io; diff --git a/src/pass_second.rs b/src/pass_second.rs index 54afdbd..6d366c2 100644 --- a/src/pass_second.rs +++ b/src/pass_second.rs @@ -1,9 +1,7 @@ use rio_api::{model::Triple, parser::TriplesParser}; use rio_turtle::TurtleError; use std::{ - collections::HashMap, - io::{BufRead, Write}, - path::Path, + collections::HashMap, io::{BufRead, Write}, path::Path }; use crate::{ @@ -14,7 +12,7 @@ use crate::{ }; fn mask_triple(triple: &Triple) -> TripleMask { - return TripleMask::SUBJECT; + return TripleMask::SUBJECT } // mask and encode input triple