@@ -19,12 +19,15 @@ use std::fs::File;
1919use std:: io:: BufRead ;
2020use std:: path:: PathBuf ;
2121
22- use anyhow:: Context ;
22+ use exn:: ErrorExt ;
23+ use exn:: Result ;
24+ use exn:: ResultExt ;
2325use minijinja:: context;
2426use minijinja:: Environment ;
2527use serde:: Deserialize ;
2628use serde:: Serialize ;
2729
30+ use crate :: error:: Error ;
2831use crate :: header:: matcher:: HeaderMatcher ;
2932use crate :: header:: model:: HeaderDef ;
3033use crate :: header:: parser:: parse_header;
@@ -60,7 +63,7 @@ impl Document {
6063 keywords : & [ String ] ,
6164 props : HashMap < String , String > ,
6265 attrs : Attributes ,
63- ) -> anyhow :: Result < Option < Self > > {
66+ ) -> Result < Option < Self > , Error > {
6467 match FileContent :: new ( & filepath) {
6568 Ok ( content) => Ok ( Some ( Self {
6669 parser : parse_header ( content, & header_def, keywords) ,
@@ -69,13 +72,15 @@ impl Document {
6972 props,
7073 attrs,
7174 } ) ) ,
72- Err ( e ) => {
73- if matches ! ( e . kind( ) , std:: io:: ErrorKind :: InvalidData ) {
75+ Err ( err ) => {
76+ if matches ! ( err . kind( ) , std:: io:: ErrorKind :: InvalidData ) {
7477 log:: debug!( "skip non-textual file: {}" , filepath. display( ) ) ;
7578 Ok ( None )
7679 } else {
77- Err ( e)
78- . with_context ( || format ! ( "cannot create document: {}" , filepath. display( ) ) )
80+ Err ( err. raise ( ) . raise ( Error :: new ( format ! (
81+ "cannot create document: {}" ,
82+ filepath. display( )
83+ ) ) ) )
7984 }
8085 }
8186 }
@@ -95,7 +100,7 @@ impl Document {
95100 & self ,
96101 header : & HeaderMatcher ,
97102 strict_check : bool ,
98- ) -> anyhow :: Result < bool > {
103+ ) -> Result < bool , Error > {
99104 if strict_check {
100105 let file_header = {
101106 let mut lines = self . read_file_first_lines ( header) ?. join ( "\n " ) ;
@@ -115,15 +120,19 @@ impl Document {
115120 }
116121 }
117122
118- fn read_file_first_lines ( & self , header : & HeaderMatcher ) -> std:: io:: Result < Vec < String > > {
119- let file = File :: open ( & self . filepath ) ?;
123+ #[ track_caller]
124+ fn read_file_first_lines ( & self , header : & HeaderMatcher ) -> Result < Vec < String > , Error > {
125+ let make_error = || Error :: new ( "cannot read file first line" ) ;
126+ let file = File :: open ( & self . filepath ) . or_raise ( make_error) ?;
120127 std:: io:: BufReader :: new ( file)
121128 . lines ( )
122129 . take ( header. header_content_lines_count ( ) + 10 )
123130 . collect :: < std:: io:: Result < Vec < _ > > > ( )
131+ . or_raise ( make_error)
124132 }
125133
126- fn read_file_header_on_one_line ( & self , header : & HeaderMatcher ) -> std:: io:: Result < String > {
134+ #[ track_caller]
135+ fn read_file_header_on_one_line ( & self , header : & HeaderMatcher ) -> Result < String , Error > {
127136 let first_lines = self . read_file_first_lines ( header) ?;
128137 let file_header = first_lines
129138 . join ( "" )
@@ -137,7 +146,7 @@ impl Document {
137146 Ok ( file_header)
138147 }
139148
140- pub fn update_header ( & mut self , header : & HeaderMatcher ) -> anyhow :: Result < ( ) > {
149+ pub fn update_header ( & mut self , header : & HeaderMatcher ) -> Result < ( ) , Error > {
141150 let header_str = header. build_for_definition ( & self . header_def ) ;
142151 let header_str = self . merge_properties ( & header_str) ?;
143152 let begin_pos = self . parser . begin_pos ;
@@ -155,22 +164,24 @@ impl Document {
155164 }
156165 }
157166
158- pub fn save ( & mut self , filepath : Option < & PathBuf > ) -> anyhow :: Result < ( ) > {
167+ pub fn save ( & mut self , filepath : Option < & PathBuf > ) -> Result < ( ) , Error > {
159168 let filepath = filepath. unwrap_or ( & self . filepath ) ;
160169 fs:: write ( filepath, self . parser . file_content . content ( ) )
161- . context ( format ! ( "cannot save document {}" , filepath. display( ) ) )
170+ . or_raise ( || Error :: new ( format ! ( "cannot save document {}" , filepath. display( ) ) ) )
162171 }
163172
164- pub ( crate ) fn merge_properties ( & self , s : & str ) -> anyhow :: Result < String > {
173+ pub ( crate ) fn merge_properties ( & self , s : & str ) -> Result < String , Error > {
165174 let mut env = Environment :: new ( ) ;
166175 env. add_template ( "template" , s)
167- . context ( "malformed template" ) ?;
176+ . or_raise ( || Error :: new ( "malformed template" ) ) ?;
168177
169178 let tmpl = env. get_template ( "template" ) . expect ( "template must exist" ) ;
170- let mut result = tmpl. render ( context ! {
171- props => & self . props,
172- attrs => & self . attrs,
173- } ) ?;
179+ let mut result = tmpl
180+ . render ( context ! {
181+ props => & self . props,
182+ attrs => & self . attrs,
183+ } )
184+ . or_raise ( || Error :: new ( "cannot render template" ) ) ?;
174185 result. push ( '\n' ) ;
175186 Ok ( result)
176187 }
0 commit comments