Skip to content

Commit 07e8fb2

Browse files
committed
refactor (#293)
1 parent c17240d commit 07e8fb2

File tree

3 files changed

+82
-84
lines changed

3 files changed

+82
-84
lines changed

git-index/src/decode.rs

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use crate::{extension, State};
2+
use filetime::FileTime;
3+
4+
pub mod header {
5+
pub(crate) const SIZE: usize = 4 /*signature*/ + 4 /*version*/ + 4 /* num entries */;
6+
7+
mod error {
8+
use quick_error::quick_error;
9+
10+
quick_error! {
11+
#[derive(Debug)]
12+
pub enum Error {
13+
Corrupt(message: &'static str) {
14+
display("{}", message)
15+
}
16+
UnsupportedVersion(version: u32) {
17+
display("Index version {} is not supported", version)
18+
}
19+
}
20+
}
21+
}
22+
use crate::{util::read_u32, Version};
23+
pub use error::Error;
24+
25+
pub(crate) fn decode(data: &[u8], object_hash: git_hash::Kind) -> Result<(crate::Version, u32, &[u8]), Error> {
26+
if data.len() < (3 * 4) + object_hash.len_in_bytes() {
27+
return Err(Error::Corrupt(
28+
"File is too small even for header with zero entries and smallest hash",
29+
));
30+
}
31+
32+
const SIGNATURE: &[u8] = b"DIRC";
33+
let (signature, data) = data.split_at(4);
34+
if signature != SIGNATURE {
35+
return Err(Error::Corrupt(
36+
"Signature mismatch - this doesn't claim to be a header file",
37+
));
38+
}
39+
40+
let (version, data) = data.split_at(4);
41+
let version = match read_u32(version) {
42+
2 => Version::V2,
43+
3 => Version::V3,
44+
4 => Version::V4,
45+
unknown => return Err(Error::UnsupportedVersion(unknown)),
46+
};
47+
let (entries, data) = data.split_at(4);
48+
let entries = read_u32(entries);
49+
50+
Ok((version, entries, data))
51+
}
52+
}
53+
54+
mod error {
55+
use quick_error::quick_error;
56+
57+
use crate::decode;
58+
59+
quick_error! {
60+
#[derive(Debug)]
61+
pub enum Error {
62+
Header(err: decode::header::Error) {
63+
display("The header could not be decoded")
64+
source(err)
65+
from()
66+
}
67+
}
68+
}
69+
}
70+
71+
pub use error::Error;
72+
73+
impl State {
74+
pub fn from_bytes(data: &[u8], timestamp: FileTime, object_hash: git_hash::Kind) -> Result<Self, Error> {
75+
let (version, num_entries, post_header_data) = header::decode(&data, object_hash)?;
76+
let start_of_extensions = extension::end_of_index_entry::decode(&data, object_hash);
77+
78+
Ok(State { timestamp, version })
79+
}
80+
}

git-index/src/extension.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn decode_header(data: &[u8]) -> (Signature, u32, &[u8]) {
1111
}
1212

1313
pub(crate) mod end_of_index_entry {
14-
use crate::{extension, extension::Signature, header, util::read_u32};
14+
use crate::{decode::header, extension, extension::Signature, util::read_u32};
1515

1616
pub const SIGNATURE: Signature = *b"EOIE";
1717
pub const SIZE: usize = 4 /* offset to extensions */ + git_hash::Kind::Sha1.len_in_bytes();

git-index/src/lib.rs

+1-83
Original file line numberDiff line numberDiff line change
@@ -42,89 +42,7 @@ pub mod init {
4242
}
4343
}
4444

45-
pub mod decode {
46-
use crate::{extension, header, State};
47-
use filetime::FileTime;
48-
49-
mod error {
50-
use quick_error::quick_error;
51-
52-
use crate::header;
53-
54-
quick_error! {
55-
#[derive(Debug)]
56-
pub enum Error {
57-
Header(err: header::decode::Error) {
58-
display("The header could not be decoded")
59-
source(err)
60-
from()
61-
}
62-
}
63-
}
64-
}
65-
pub use error::Error;
66-
67-
impl State {
68-
pub fn from_bytes(data: &[u8], timestamp: FileTime, object_hash: git_hash::Kind) -> Result<Self, Error> {
69-
let (version, num_entries, post_header_data) = header::decode(&data, object_hash)?;
70-
let start_of_extensions = extension::end_of_index_entry::decode(&data, object_hash);
71-
72-
Ok(State { timestamp, version })
73-
}
74-
}
75-
}
76-
77-
pub mod header {
78-
pub(crate) const SIZE: usize = 4 /*signature*/ + 4 /*version*/ + 4 /* num entries */;
79-
80-
pub mod decode {
81-
use quick_error::quick_error;
82-
83-
quick_error! {
84-
#[derive(Debug)]
85-
pub enum Error {
86-
Corrupt(message: &'static str) {
87-
display("{}", message)
88-
}
89-
UnsupportedVersion(version: u32) {
90-
display("Index version {} is not supported", version)
91-
}
92-
}
93-
}
94-
}
95-
use crate::{util::read_u32, Version};
96-
97-
pub(crate) fn decode(
98-
data: &[u8],
99-
object_hash: git_hash::Kind,
100-
) -> Result<(crate::Version, u32, &[u8]), decode::Error> {
101-
if data.len() < (3 * 4) + object_hash.len_in_bytes() {
102-
return Err(decode::Error::Corrupt(
103-
"File is too small even for header with zero entries and smallest hash",
104-
));
105-
}
106-
107-
const SIGNATURE: &[u8] = b"DIRC";
108-
let (signature, data) = data.split_at(4);
109-
if signature != SIGNATURE {
110-
return Err(decode::Error::Corrupt(
111-
"Signature mismatch - this doesn't claim to be a header file",
112-
));
113-
}
114-
115-
let (version, data) = data.split_at(4);
116-
let version = match read_u32(version) {
117-
2 => Version::V2,
118-
3 => Version::V3,
119-
4 => Version::V4,
120-
unknown => return Err(decode::Error::UnsupportedVersion(unknown)),
121-
};
122-
let (entries, data) = data.split_at(4);
123-
let entries = read_u32(entries);
124-
125-
Ok((version, entries, data))
126-
}
127-
}
45+
pub mod decode;
12846

12947
/// All known versions of a git index file.
13048
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]

0 commit comments

Comments
 (0)