@@ -63,10 +63,8 @@ impl<'repo> Commit<'repo> {
6363 ///
6464 /// The returned message will be slightly prettified by removing any
6565 /// potential leading newlines.
66- ///
67- /// `None` will be returned if the message is not valid utf-8
68- pub fn message ( & self ) -> Option < & str > {
69- str:: from_utf8 ( self . message_bytes ( ) ) . ok ( )
66+ pub fn message ( & self ) -> Result < & str , Error > {
67+ str:: from_utf8 ( self . message_bytes ( ) ) . map_err ( |e| e. into ( ) )
7068 }
7169
7270 /// Get the full message of a commit as a byte slice.
@@ -80,17 +78,18 @@ impl<'repo> Commit<'repo> {
8078 /// Get the encoding for the message of a commit, as a string representing a
8179 /// standard encoding name.
8280 ///
83- /// `None` will be returned if the encoding is not known
84- pub fn message_encoding ( & self ) -> Option < & str > {
81+ /// `Ok( None) ` will be returned if the encoding is not known
82+ pub fn message_encoding ( & self ) -> Result < Option < & str > , Error > {
8583 let bytes = unsafe { crate :: opt_bytes ( self , raw:: git_commit_message_encoding ( & * self . raw ) ) } ;
86- bytes. and_then ( |b| str:: from_utf8 ( b) . ok ( ) )
84+ match bytes {
85+ Some ( b) => str:: from_utf8 ( b) . map ( |s| Some ( s) ) . map_err ( |e| e. into ( ) ) ,
86+ None => Ok ( None ) ,
87+ }
8788 }
8889
8990 /// Get the full raw message of a commit.
90- ///
91- /// `None` will be returned if the message is not valid utf-8
92- pub fn message_raw ( & self ) -> Option < & str > {
93- str:: from_utf8 ( self . message_raw_bytes ( ) ) . ok ( )
91+ pub fn message_raw ( & self ) -> Result < & str , Error > {
92+ str:: from_utf8 ( self . message_raw_bytes ( ) ) . map_err ( |e| e. into ( ) )
9493 }
9594
9695 /// Get the full raw message of a commit.
@@ -99,10 +98,8 @@ impl<'repo> Commit<'repo> {
9998 }
10099
101100 /// Get the full raw text of the commit header.
102- ///
103- /// `None` will be returned if the message is not valid utf-8
104- pub fn raw_header ( & self ) -> Option < & str > {
105- str:: from_utf8 ( self . raw_header_bytes ( ) ) . ok ( )
101+ pub fn raw_header ( & self ) -> Result < & str , Error > {
102+ str:: from_utf8 ( self . raw_header_bytes ( ) ) . map_err ( |e| e. into ( ) )
106103 }
107104
108105 /// Get an arbitrary header field.
@@ -129,10 +126,12 @@ impl<'repo> Commit<'repo> {
129126 /// The returned message is the summary of the commit, comprising the first
130127 /// paragraph of the message with whitespace trimmed and squashed.
131128 ///
132- /// `None` may be returned if an error occurs or if the summary is not valid
133- /// utf-8.
134- pub fn summary ( & self ) -> Option < & str > {
135- self . summary_bytes ( ) . and_then ( |s| str:: from_utf8 ( s) . ok ( ) )
129+ /// `Ok(None)` may be returned if there is no summary
130+ pub fn summary ( & self ) -> Result < Option < & str > , Error > {
131+ match self . summary_bytes ( ) {
132+ Some ( sb) => str:: from_utf8 ( sb) . map ( |s| Some ( s) ) . map_err ( |e| e. into ( ) ) ,
133+ None => Ok ( None ) ,
134+ }
136135 }
137136
138137 /// Get the short "summary" of the git commit message.
@@ -151,10 +150,12 @@ impl<'repo> Commit<'repo> {
151150 /// but the first paragraph of the message. Leading and trailing whitespaces
152151 /// are trimmed.
153152 ///
154- /// `None` may be returned if an error occurs or if the summary is not valid
155- /// utf-8.
156- pub fn body ( & self ) -> Option < & str > {
157- self . body_bytes ( ) . and_then ( |s| str:: from_utf8 ( s) . ok ( ) )
153+ /// `Ok(None)` may be returned if there is no body.
154+ pub fn body ( & self ) -> Result < Option < & str > , Error > {
155+ match self . body_bytes ( ) {
156+ Some ( sb) => str:: from_utf8 ( sb) . map ( |s| Some ( s) ) . map_err ( |e| e. into ( ) ) ,
157+ None => Ok ( None ) ,
158+ }
158159 }
159160
160161 /// Get the long "body" of the git commit message.
@@ -347,7 +348,7 @@ impl<'repo> std::fmt::Debug for Commit<'repo> {
347348 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> Result < ( ) , std:: fmt:: Error > {
348349 let mut ds = f. debug_struct ( "Commit" ) ;
349350 ds. field ( "id" , & self . id ( ) ) ;
350- if let Some ( summary) = self . summary ( ) {
351+ if let Ok ( Some ( summary) ) = self . summary ( ) {
351352 ds. field ( "summary" , & summary) ;
352353 }
353354 ds. finish ( )
@@ -424,12 +425,12 @@ mod tests {
424425 let head = repo. head ( ) . unwrap ( ) ;
425426 let target = head. target ( ) . unwrap ( ) ;
426427 let commit = repo. find_commit ( target) . unwrap ( ) ;
427- assert_eq ! ( commit. message( ) , Some ( "initial\n \n body" ) ) ;
428- assert_eq ! ( commit. body( ) , Some ( "body" ) ) ;
428+ assert_eq ! ( commit. message( ) , Ok ( "initial\n \n body" ) ) ;
429+ assert_eq ! ( commit. body( ) , Ok ( Some ( "body" ) ) ) ;
429430 assert_eq ! ( commit. id( ) , target) ;
430431 commit. message_raw ( ) . unwrap ( ) ;
431432 commit. raw_header ( ) . unwrap ( ) ;
432- commit. message_encoding ( ) ;
433+ commit. message_encoding ( ) . expect ( "Should not be invalid" ) ;
433434 commit. summary ( ) . unwrap ( ) ;
434435 commit. body ( ) . unwrap ( ) ;
435436 commit. tree_id ( ) ;
@@ -441,10 +442,10 @@ mod tests {
441442 crate :: Oid :: from_str( tree_header_bytes. as_str( ) . unwrap( ) ) . unwrap( ) ,
442443 commit. tree_id( )
443444 ) ;
444- assert_eq ! ( commit. author( ) . name( ) , Some ( "name" ) ) ;
445- assert_eq ! ( commit. author( ) . email( ) , Some ( "email" ) ) ;
446- assert_eq ! ( commit. committer( ) . name( ) , Some ( "name" ) ) ;
447- assert_eq ! ( commit. committer( ) . email( ) , Some ( "email" ) ) ;
445+ assert_eq ! ( commit. author( ) . name( ) , Ok ( "name" ) ) ;
446+ assert_eq ! ( commit. author( ) . email( ) , Ok ( "email" ) ) ;
447+ assert_eq ! ( commit. committer( ) . name( ) , Ok ( "name" ) ) ;
448+ assert_eq ! ( commit. committer( ) . email( ) , Ok ( "email" ) ) ;
448449
449450 let sig = repo. signature ( ) . unwrap ( ) ;
450451 let tree = repo. find_tree ( commit. tree_id ( ) ) . unwrap ( ) ;
@@ -457,7 +458,7 @@ mod tests {
457458 . amend ( Some ( "HEAD" ) , None , None , None , Some ( "new message" ) , None )
458459 . unwrap ( ) ;
459460 let new_head = repo. find_commit ( new_head) . unwrap ( ) ;
460- assert_eq ! ( new_head. message( ) , Some ( "new message" ) ) ;
461+ assert_eq ! ( new_head. message( ) , Ok ( "new message" ) ) ;
461462 new_head. into_object ( ) ;
462463
463464 repo. find_object ( target, None ) . unwrap ( ) . as_commit ( ) . unwrap ( ) ;
0 commit comments