Skip to content

Commit 24d6f57

Browse files
committed
work on merge
1 parent 6149ac9 commit 24d6f57

File tree

6 files changed

+44
-11
lines changed

6 files changed

+44
-11
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ serde_json = "1.0.62"
2929
chrono = "0.4.19"
3030
quantiles = "0.7.1"
3131
structopt = "0.3.21"
32+
glob = "0.3.0"

src/bin/analyzer/main.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,23 @@
1010
)]
1111

1212
use anyhow::{Context, Result};
13-
use codeprints_analyzer::count_commits;
1413
use codeprints_analyzer::Parser;
14+
use codeprints_analyzer::Timeline;
15+
use codeprints_analyzer::{count_commits, Merger};
16+
use glob::glob;
1517
use std::fs;
1618
use structopt::StructOpt;
1719

1820
mod options;
1921
use options::Command;
2022

21-
const OUTPUT_FILE: &'static str = "codeprints.json";
23+
fn write(timeline: &Timeline, output_file: &str) -> Result<()> {
24+
let output = serde_json::to_string_pretty(&timeline)?;
25+
fs::write(output_file, output)?;
26+
println!("done!");
27+
println!("Output file: {}", output_file);
28+
Ok(())
29+
}
2230

2331
fn main() -> Result<()> {
2432
let opt = Command::from_args();
@@ -42,18 +50,26 @@ fn main() -> Result<()> {
4250
parser.set_after(after)?;
4351
}
4452
let timeline = parser.parse()?;
45-
let output = serde_json::to_string_pretty(&timeline)?;
46-
fs::write(OUTPUT_FILE, output)?;
47-
println!("done!");
48-
println!("Output file: {}", OUTPUT_FILE);
53+
54+
write(&timeline, "codeprints.json")?;
4955
}
5056
Command::Merge {} => {
5157
// Find all `codeprints*.json` files in the current directory
5258
// using glob.
53-
// Read each one into memory
54-
// Merge the results together
55-
// Write a `codeprints_merged.json` file
56-
unimplemented!();
59+
let mut merger = Merger::new();
60+
for entry in glob("codeprints*.json")? {
61+
match entry {
62+
Ok(path) => {
63+
println!("Merging {}", path.display());
64+
let input = fs::read_to_string(path)?;
65+
let mut parser = Parser::new(input);
66+
let timeline = parser.parse()?;
67+
merger.merge_timeline(&timeline)?;
68+
}
69+
Err(e) => println!("{:?}", e),
70+
}
71+
}
72+
write(&merger.timeline()?, "codeprints_merged.json")?;
5773
}
5874
};
5975
Ok(())

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ mod types;
2121
pub use crate::git::count_commits;
2222
pub use crate::merge::Merger;
2323
pub use crate::parser::Parser;
24+
pub use crate::types::Timeline as Timeline;

src/merge.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ impl Merger {
1616
Merger { state }
1717
}
1818

19-
fn merge_timeline(&mut self, timeline: &Timeline) -> Result<()> {
19+
/// Merge a single timeline
20+
pub fn merge_timeline(&mut self, timeline: &Timeline) -> Result<()> {
2021
for contribution in &timeline.contributions {
2122
let date = contribution.date.clone();
2223
let date = git::parse_date(&date)?;
@@ -42,8 +43,14 @@ impl Merger {
4243
}
4344
Ok(Timeline::try_from(&self.state)?)
4445
}
46+
47+
/// Return the merged timeline of all inputs
48+
pub fn timeline(&self) -> Result<Timeline> {
49+
Ok(Timeline::try_from(&self.state)?)
50+
}
4551
}
4652

53+
4754
#[cfg(test)]
4855
mod test {
4956
use super::*;

src/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub struct Year {
1818
pub type Years = Vec<Year>;
1919
pub type Contributions = Vec<Contribution>;
2020

21+
/// A timeline represents a codeprints.json file's contents
2122
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
2223
#[serde(rename_all = "camelCase")]
2324
pub struct Timeline {

0 commit comments

Comments
 (0)