Skip to content

Commit

Permalink
feat: Add static dispatch for writer/reader (#18)
Browse files Browse the repository at this point in the history
Co-authored-by: cmdoret <[email protected]>
  • Loading branch information
gabyx and cmdoret authored Jun 25, 2024
1 parent 2ae88c9 commit ae2c9e7
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
32 changes: 23 additions & 9 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn BufRead> {
use io_enum::{BufRead, Read, Write};

#[derive(Read, BufRead)]
pub enum Reader {
Stdio(BufReader<io::Stdin>),
File(BufReader<File>),
}

#[derive(Write)]
pub enum Writer {
Stdio(BufWriter<io::Stdout>),
File(BufWriter<File>),
}

// 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<dyn Write> {
// 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())),
};
}

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ fn main() {
Subcommands::Pseudo(args) => {
info!(log, "Args: {:?}", args);
pseudonymize_graph(&log, &args.input, &args.config, &args.output, &args.index)

}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/pass_first.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
6 changes: 2 additions & 4 deletions src/pass_second.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -14,7 +12,7 @@ use crate::{
};

fn mask_triple(triple: &Triple) -> TripleMask {
return TripleMask::SUBJECT;
return TripleMask::SUBJECT
}

// mask and encode input triple
Expand Down

0 comments on commit ae2c9e7

Please sign in to comment.