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
49 /// Errors are coerced into options, hiding whether there was an error or not. The caller should assume an error if they49 /// Errors are coerced into options, hiding whether there was an error or not. The caller should assume an error if they
50 /// call the method as intended. Such a squelched error cannot be recovered unless the objects data is retrieved and parsed again.50 /// call the method as intended. Such a squelched error cannot be recovered unless the objects data is retrieved and parsed again.
51 /// `next()`.51 /// `next()`.
52 pub fn tree_id(&mut self) -> Option<ObjectId> {52 pub fn tree_id(&mut self) -> Result<ObjectId, crate::decode::Error> {
53 self.next().and_then(Result::ok).and_then(Token::try_into_id)53 let tree_id = self.next().ok_or_else(missing_field)??;
54 Token::try_into_id(tree_id).ok_or_else(missing_field)
54 }55 }
55 56
56 /// Return all parent_ids as iterator.57 /// Return all parent_ids as iterator.
@@ -77,32 +78,43 @@ impl<'a> CommitRefIter<'a> { · 20 unmodified lines
77 78
78 /// Returns the committer signature if there is no decoding error.79 /// Returns the committer signature if there is no decoding error.
79 /// Errors are coerced into options, hiding whether there was an error or not. The caller knows if there was an error or not.80 /// 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> {
81 self.find_map(|t| match t {82 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)),
83 _ => None,85 _ => None,
84 })86 })
87 .ok_or_else(missing_field)?
85 }88 }
86 89
87 /// Returns the author signature if there is no decoding error.90 /// Returns the author signature if there is no decoding error.
88 /// Errors are coerced into options, hiding whether there was an error or not. The caller knows if there was an error or not.91 /// 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> {
90 self.find_map(|t| match t {93 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)),
92 _ => None,96 _ => None,
93 })97 })
98 .ok_or_else(missing_field)?
94 }99 }
95 100
96 /// Returns the message if there is no decoding error.101 /// Returns the message if there is no decoding error.
97 /// Errors are coerced into options, hiding whether there was an error or not. The caller knows if there was an error or not.102 /// 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> {
99 self.find_map(|t| match t {104 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)),
101 _ => None,107 _ => None,
102 })108 })
109 .transpose()
110 .map(|msg| msg.unwrap_or_default())
103 }111 }
104}112}
105 113
114fn missing_field() -> crate::decode::Error {
115 crate::decode::empty_error()
116}
117
106impl<'a> CommitRefIter<'a> {118impl<'a> CommitRefIter<'a> {
107 fn next_inner(i: &'a [u8], state: &mut State) -> Result<(&'a [u8], Token<'a>), crate::decode::Error> {119 fn next_inner(i: &'a [u8], state: &mut State) -> Result<(&'a [u8], Token<'a>), crate::decode::Error> {
108 use State::*;120 use State::*;