Skip to content

Commit

Permalink
Use clap to provide file input
Browse files Browse the repository at this point in the history
This commit also changes the reading of the lines to be unbuffered for reduced memory consumption, and adds an a author field to the Cargo manifest
  • Loading branch information
Erin van der Veen committed Jan 9, 2024
1 parent 620e345 commit c0c797b
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 15 deletions.
173 changes: 173 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
name = "genealogos"
version = "0.1.0"
edition = "2021"
authors = ["Tweag I/O"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.4.14", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde-cyclonedx = "0.8"
serde_json = "1.0"
uuid = { version = "1.6", features = ["v4"] }
33 changes: 19 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
mod model;
mod nixtract;

use std::fs::File;
use std::io::{self, BufRead};
use std::path::PathBuf;

use clap::Parser;
use serde_cyclonedx::cyclonedx::v_1_5 as cyclonedx;

use crate::model::Model;
use crate::nixtract::Nixtract;

mod model;
mod nixtract;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
file: PathBuf,
}

fn main() -> Result<(), io::Error> {
let mut input_reader = std::io::stdin().lock();
let args = Args::parse();

let file = File::open(args.file)?;

let mut entries = vec![];

loop {
let mut buffer = String::new();
match input_reader.read_line(&mut buffer) {
Ok(0) => break,
Ok(_n) => {
let entry: nixtract::NixtractEntry = serde_json::from_str(&buffer.trim()).unwrap();
entries.push(entry);
}
Err(_) => todo!(),
}
for line in io::BufReader::new(file).lines().flatten() {
let entry: nixtract::NixtractEntry = serde_json::from_str(&line.trim()).unwrap();
entries.push(entry);
}
let nixtract: Nixtract = Nixtract { entries };

Expand Down

0 comments on commit c0c797b

Please sign in to comment.