Skip to content

Commit ffc0089

Browse files
committed
refacto
1 parent b9a1647 commit ffc0089

File tree

1 file changed

+122
-113
lines changed

1 file changed

+122
-113
lines changed

git-object/src/borrowed/mod.rs

Lines changed: 122 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,164 @@
1-
use crate::Time;
2-
use bstr::BStr;
3-
41
mod commit;
5-
mod tag;
6-
mod tree;
7-
mod util;
8-
92
pub use commit::Commit;
3+
4+
mod tag;
105
pub use tag::Tag;
6+
7+
mod tree;
118
//FIXME: keep tree mode and entry in tree export it from there? Alternatively rename to TreeMode, TreeEntry?
129
pub use tree::{Entry, Mode, Tree};
1310

14-
mod error {
15-
use nom::error::ParseError;
16-
use quick_error::quick_error;
17-
quick_error! {
18-
#[derive(Debug)]
19-
pub enum Error {
20-
ParseIntegerError(msg: &'static str, number: bstr::BString, err: btoi::ParseIntegerError) {
21-
display("{}: {:?}", msg, number)
22-
cause(err)
23-
}
24-
Nom(err_msg: String) {
25-
display("{}", err_msg)
26-
}
27-
NomDetail(input: bstr::BString, msg: &'static str) {
28-
display("{}: '{}' could not be parsed", msg, input)
29-
}
30-
ParseKindError(err: crate::types::Error) {
31-
display("{}", err)
32-
cause(err)
33-
}
34-
ObjectKind(err: crate::Error) {
35-
from()
36-
cause(err)
11+
mod util;
12+
13+
mod object {
14+
use crate::Time;
15+
use bstr::BStr;
16+
17+
mod error {
18+
use nom::error::ParseError;
19+
use quick_error::quick_error;
20+
quick_error! {
21+
#[derive(Debug)]
22+
pub enum Error {
23+
ParseIntegerError(msg: &'static str, number: bstr::BString, err: btoi::ParseIntegerError) {
24+
display("{}: {:?}", msg, number)
25+
cause(err)
26+
}
27+
Nom(err_msg: String) {
28+
display("{}", err_msg)
29+
}
30+
NomDetail(input: bstr::BString, msg: &'static str) {
31+
display("{}: '{}' could not be parsed", msg, input)
32+
}
33+
ParseKindError(err: crate::types::Error) {
34+
display("{}", err)
35+
cause(err)
36+
}
37+
ObjectKind(err: crate::Error) {
38+
from()
39+
cause(err)
40+
}
3741
}
3842
}
39-
}
4043

41-
impl Error {
42-
fn set_parse_context(mut self, ctx: &'static str) -> Self {
43-
match self {
44-
Error::NomDetail(_, ref mut message) => *message = ctx,
45-
_ => {}
46-
};
47-
self
48-
}
44+
impl Error {
45+
fn set_parse_context(mut self, ctx: &'static str) -> Self {
46+
match self {
47+
Error::NomDetail(_, ref mut message) => *message = ctx,
48+
_ => {}
49+
};
50+
self
51+
}
4952

50-
pub(crate) fn context(msg: &'static str) -> impl Fn(nom::Err<Self>) -> nom::Err<Self> {
51-
move |e: nom::Err<Self>| e.map(|e| e.set_parse_context(msg))
53+
pub(crate) fn context(msg: &'static str) -> impl Fn(nom::Err<Self>) -> nom::Err<Self> {
54+
move |e: nom::Err<Self>| e.map(|e| e.set_parse_context(msg))
55+
}
5256
}
53-
}
5457

55-
impl ParseError<&[u8]> for Error {
56-
fn from_error_kind(input: &[u8], _kind: nom::error::ErrorKind) -> Self {
57-
Error::NomDetail(input.into(), "parse error")
58-
}
58+
impl ParseError<&[u8]> for Error {
59+
fn from_error_kind(input: &[u8], _kind: nom::error::ErrorKind) -> Self {
60+
Error::NomDetail(input.into(), "parse error")
61+
}
5962

60-
fn append(_: &[u8], _: nom::error::ErrorKind, other: Self) -> Self {
61-
other
63+
fn append(_: &[u8], _: nom::error::ErrorKind, other: Self) -> Self {
64+
other
65+
}
6266
}
63-
}
6467

65-
impl From<nom::Err<Error>> for Error {
66-
fn from(e: nom::Err<Error>) -> Self {
67-
match e {
68-
nom::Err::Error(err) | nom::Err::Failure(err) => Error::Nom(err.to_string()),
69-
nom::Err::Incomplete(_) => unreachable!("we do not implement streaming parsers"),
68+
impl From<nom::Err<Error>> for Error {
69+
fn from(e: nom::Err<Error>) -> Self {
70+
match e {
71+
nom::Err::Error(err) | nom::Err::Failure(err) => Error::Nom(err.to_string()),
72+
nom::Err::Incomplete(_) => {
73+
unreachable!("we do not implement streaming parsers")
74+
}
75+
}
7076
}
7177
}
7278
}
73-
}
7479

75-
pub use error::Error;
80+
use crate::borrowed::{Commit, Tag, Tree};
81+
pub use error::Error;
7682

77-
#[derive(PartialEq, Eq, Debug, Hash)]
78-
pub struct Signature<'data> {
79-
pub name: &'data BStr,
80-
pub email: &'data BStr,
81-
pub time: Time,
82-
}
83+
#[derive(PartialEq, Eq, Debug, Hash)]
84+
pub struct Signature<'data> {
85+
pub name: &'data BStr,
86+
pub email: &'data BStr,
87+
pub time: Time,
88+
}
8389

84-
#[derive(PartialEq, Eq, Debug, Hash)]
85-
pub enum Object<'data> {
86-
Tag(Tag<'data>),
87-
Commit(Commit<'data>),
88-
Tree(Tree<'data>),
89-
}
90+
#[derive(PartialEq, Eq, Debug, Hash)]
91+
pub enum Object<'data> {
92+
Tag(Tag<'data>),
93+
Commit(Commit<'data>),
94+
Tree(Tree<'data>),
95+
}
9096

91-
impl<'data> Object<'data> {
92-
pub fn kind(&self) -> crate::Kind {
93-
match self {
94-
Object::Tag(_) => crate::Kind::Tag,
95-
Object::Commit(_) => crate::Kind::Commit,
96-
Object::Tree(_) => crate::Kind::Tree,
97+
impl<'data> Object<'data> {
98+
pub fn kind(&self) -> crate::Kind {
99+
match self {
100+
Object::Tag(_) => crate::Kind::Tag,
101+
Object::Commit(_) => crate::Kind::Commit,
102+
Object::Tree(_) => crate::Kind::Tree,
103+
}
97104
}
98105
}
99-
}
100106

101-
mod convert {
102-
use crate::borrowed::{Commit, Object, Tag, Tree};
103-
use std::convert::TryFrom;
107+
mod convert {
108+
use crate::borrowed::{Commit, Object, Tag, Tree};
109+
use std::convert::TryFrom;
104110

105-
impl<'data> From<Tag<'data>> for Object<'data> {
106-
fn from(v: Tag<'data>) -> Self {
107-
Object::Tag(v)
111+
impl<'data> From<Tag<'data>> for Object<'data> {
112+
fn from(v: Tag<'data>) -> Self {
113+
Object::Tag(v)
114+
}
108115
}
109-
}
110116

111-
impl<'data> From<Commit<'data>> for Object<'data> {
112-
fn from(v: Commit<'data>) -> Self {
113-
Object::Commit(v)
117+
impl<'data> From<Commit<'data>> for Object<'data> {
118+
fn from(v: Commit<'data>) -> Self {
119+
Object::Commit(v)
120+
}
114121
}
115-
}
116122

117-
impl<'data> From<Tree<'data>> for Object<'data> {
118-
fn from(v: Tree<'data>) -> Self {
119-
Object::Tree(v)
123+
impl<'data> From<Tree<'data>> for Object<'data> {
124+
fn from(v: Tree<'data>) -> Self {
125+
Object::Tree(v)
126+
}
120127
}
121-
}
122128

123-
impl<'data> TryFrom<Object<'data>> for Tag<'data> {
124-
type Error = Object<'data>;
129+
impl<'data> TryFrom<Object<'data>> for Tag<'data> {
130+
type Error = Object<'data>;
125131

126-
fn try_from(value: Object<'data>) -> Result<Self, Self::Error> {
127-
Ok(match value {
128-
Object::Tag(v) => v,
129-
_ => return Err(value),
130-
})
132+
fn try_from(value: Object<'data>) -> Result<Self, Self::Error> {
133+
Ok(match value {
134+
Object::Tag(v) => v,
135+
_ => return Err(value),
136+
})
137+
}
131138
}
132-
}
133139

134-
impl<'data> TryFrom<Object<'data>> for Commit<'data> {
135-
type Error = Object<'data>;
140+
impl<'data> TryFrom<Object<'data>> for Commit<'data> {
141+
type Error = Object<'data>;
136142

137-
fn try_from(value: Object<'data>) -> Result<Self, Self::Error> {
138-
Ok(match value {
139-
Object::Commit(v) => v,
140-
_ => return Err(value),
141-
})
143+
fn try_from(value: Object<'data>) -> Result<Self, Self::Error> {
144+
Ok(match value {
145+
Object::Commit(v) => v,
146+
_ => return Err(value),
147+
})
148+
}
142149
}
143-
}
144150

145-
impl<'data> TryFrom<Object<'data>> for Tree<'data> {
146-
type Error = Object<'data>;
151+
impl<'data> TryFrom<Object<'data>> for Tree<'data> {
152+
type Error = Object<'data>;
147153

148-
fn try_from(value: Object<'data>) -> Result<Self, Self::Error> {
149-
Ok(match value {
150-
Object::Tree(v) => v,
151-
_ => return Err(value),
152-
})
154+
fn try_from(value: Object<'data>) -> Result<Self, Self::Error> {
155+
Ok(match value {
156+
Object::Tree(v) => v,
157+
_ => return Err(value),
158+
})
159+
}
153160
}
154161
}
155162
}
163+
164+
pub use object::*;

0 commit comments

Comments
 (0)