History

[commitgraph] refactor Graph, Position, and access module

Sebastian Thiel committed 6 years ago

Commit 3c8640e1 parent 2ed00378 files · +47 −32Browse files

Showing the changes to git-commitgraph/src/graph (3 of 8 files). Show all

Unified · Split

3 files
  1. git-commitgraph/src/graph/access.rs +16 −10
  2. git-commitgraph/src/graph/init.rs +8 −4
  3. git-commitgraph/src/graph/mod.rs +5 −5
git-commitgraph/src/graph/access.rs+16−10
@@ -1,9 +1,12 @@
1−use crate::file::{Commit, File, LexPosition};
2−use crate::graph::{Graph, GraphPosition};
1+use crate::{
2+ file::{Commit, File, LexPosition},
3+ graph::{Graph, Position},
4+};
35 use git_object::borrowed;
46
7+/// Access convenience
58 impl Graph {
6− pub fn commit_at(&self, pos: GraphPosition) -> Commit<'_> {
9+ pub fn commit_at(&self, pos: Position) -> Commit<'_> {
710 let r = self.lookup_by_pos(pos);
811 r.file.commit_at(r.lex_pos)
912 }
@@ -13,7 +16,7 @@ impl Graph { · 3 unmodified lines
1316 Some(r.file.commit_at(r.lex_pos))
1417 }
1518
16− pub fn id_at(&self, pos: GraphPosition) -> borrowed::Id<'_> {
19+ pub fn id_at(&self, pos: Position) -> borrowed::Id<'_> {
1720 let r = self.lookup_by_pos(pos);
1821 r.file.id_at(r.lex_pos)
1922 }
@@ -28,7 +31,7 @@ impl Graph { · 8 unmodified lines
2831 self.files.iter().flat_map(|file| file.iter_ids())
2932 }
3033
31− pub fn lookup(&self, id: borrowed::Id<'_>) -> Option<GraphPosition> {
34+ pub fn lookup(&self, id: borrowed::Id<'_>) -> Option<Position> {
3235 Some(self.lookup_by_id(id)?.graph_pos)
3336 }
3437
@@ -37,15 +40,16 @@ impl Graph { · 2 unmodified lines
3740 }
3841 }
3942
43+/// Access fundamentals
4044 impl Graph {
4145 fn lookup_by_id(&self, id: borrowed::Id<'_>) -> Option<LookupByIdResult<'_>> {
4246 let mut current_file_start = 0;
43− for file in self.files.iter() {
47+ for file in &self.files {
4448 if let Some(lex_pos) = file.lookup(id) {
4549 return Some(LookupByIdResult {
4650 file,
4751 lex_pos,
48− graph_pos: GraphPosition(current_file_start + lex_pos.0),
52+ graph_pos: Position(current_file_start + lex_pos.0),
4953 });
5054 }
5155 current_file_start += file.num_commits();
@@ -53,9 +57,9 @@ impl Graph { · 1 unmodified line
5357 None
5458 }
5559
56− fn lookup_by_pos(&self, pos: GraphPosition) -> LookupByPositionResult<'_> {
60+ fn lookup_by_pos(&self, pos: Position) -> LookupByPositionResult<'_> {
5761 let mut remaining = pos.0;
58− for file in self.files.iter() {
62+ for file in &self.files {
5963 match remaining.checked_sub(file.num_commits()) {
6064 Some(v) => remaining = v,
6165 None => {
@@ -70,12 +74,14 @@ impl Graph { · 8 unmodified lines
7074 }
7175 }
7276
77+#[derive(Clone)]
7378 struct LookupByIdResult<'a> {
7479 pub file: &'a File,
75− pub graph_pos: GraphPosition,
80+ pub graph_pos: Position,
7681 pub lex_pos: LexPosition,
7782 }
7883
84+#[derive(Clone)]
7985 struct LookupByPositionResult<'a> {
8086 pub file: &'a File,
8187 pub lex_pos: LexPosition,
git-commitgraph/src/graph/init.rs+8−4
@@ -1,9 +1,13 @@
1−use crate::file::File;
2−use crate::{file, Graph, MAX_COMMITS};
1+use crate::{
2+ file::{self, File},
3+ Graph, MAX_COMMITS,
4+};
35 use git_object::HashKind;
46 use quick_error::quick_error;
5−use std::io::{BufRead, BufReader};
6−use std::path::{Path, PathBuf};
7+use std::{
8+ io::{BufRead, BufReader},
9+ path::{Path, PathBuf},
10+};
711
812 quick_error! {
913 #[derive(Debug)]
git-commitgraph/src/graph/mod.rs+5−5
@@ -3,7 +3,7 @@ mod access; · 2 unmodified lines
33 mod init;
44
55 use crate::file::File;
6−use std::fmt::{Display, Formatter};
6+use std::fmt;
77
88 /// A complete commit graph.
99 ///
@@ -14,11 +14,11 @@ pub struct Graph { · 4 unmodified lines
1414 files: Vec<File>,
1515 }
1616
17−#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
18−pub struct GraphPosition(pub u32);
17+#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
18+pub struct Position(pub u32);
1919
20−impl Display for GraphPosition {
21− fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
20+impl fmt::Display for Position {
21+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2222 self.0.fmt(f)
2323 }
2424 }