History

Full error handling for CommitRefIter (#364)

Sebastian Thiel committed 4 years ago

Commit b94471a1 parent 612960712 files · +66 −44Browse files

Showing the changes to git-object/src/commit (1 of 12 files). Show all

Unified · Split

git-object/src/commit/ref_iter.rs+20−8
@@ -49,8 +49,9 @@ impl<'a> CommitRefIter<'a> { · 48 unmodified lines
4949 /// Errors are coerced into options, hiding whether there was an error or not. The caller should assume an error if they
5050 /// call the method as intended. Such a squelched error cannot be recovered unless the objects data is retrieved and parsed again.
5151 /// `next()`.
52− pub fn tree_id(&mut self) -> Option<ObjectId> {
53− self.next().and_then(Result::ok).and_then(Token::try_into_id)
52+ pub fn tree_id(&mut self) -> Result<ObjectId, crate::decode::Error> {
53+ let tree_id = self.next().ok_or_else(missing_field)??;
54+ Token::try_into_id(tree_id).ok_or_else(missing_field)
5455 }
5556
5657 /// Return all parent_ids as iterator.
@@ -77,32 +78,43 @@ impl<'a> CommitRefIter<'a> { · 20 unmodified lines
7778
7879 /// Returns the committer signature if there is no decoding error.
7980 /// Errors are coerced into options, hiding whether there was an error or not. The caller knows if there was an error or not.
80− pub fn committer(mut self) -> Option<git_actor::SignatureRef<'a>> {
81+ pub fn committer(mut self) -> Result<git_actor::SignatureRef<'a>, crate::decode::Error> {
8182 self.find_map(|t| match t {
82− Ok(Token::Committer { signature }) => Some(signature),
83+ Ok(Token::Committer { signature }) => Some(Ok(signature)),
84+ Err(err) => Some(Err(err)),
8385 _ => None,
8486 })
87+ .ok_or_else(missing_field)?
8588 }
8689
8790 /// Returns the author signature if there is no decoding error.
8891 /// Errors are coerced into options, hiding whether there was an error or not. The caller knows if there was an error or not.
89− pub fn author(mut self) -> Option<git_actor::SignatureRef<'a>> {
92+ pub fn author(mut self) -> Result<git_actor::SignatureRef<'a>, crate::decode::Error> {
9093 self.find_map(|t| match t {
91− Ok(Token::Author { signature }) => Some(signature),
94+ Ok(Token::Author { signature }) => Some(Ok(signature)),
95+ Err(err) => Some(Err(err)),
9296 _ => None,
9397 })
98+ .ok_or_else(missing_field)?
9499 }
95100
96101 /// Returns the message if there is no decoding error.
97102 /// Errors are coerced into options, hiding whether there was an error or not. The caller knows if there was an error or not.
98− pub fn message(mut self) -> Option<&'a BStr> {
103+ pub fn message(mut self) -> Result<&'a BStr, crate::decode::Error> {
99104 self.find_map(|t| match t {
100− Ok(Token::Message(msg)) => Some(msg),
105+ Ok(Token::Message(msg)) => Some(Ok(msg)),
106+ Err(err) => Some(Err(err)),
101107 _ => None,
102108 })
109+ .transpose()
110+ .map(|msg| msg.unwrap_or_default())
103111 }
104112 }
105113
114+fn missing_field() -> crate::decode::Error {
115+ crate::decode::empty_error()
116+}
117+
106118 impl<'a> CommitRefIter<'a> {
107119 fn next_inner(i: &'a [u8], state: &mut State) -> Result<(&'a [u8], Token<'a>), crate::decode::Error> {
108120 use State::*;