@@ -15,31 +15,12 @@ use codesnap::{
1515
1616use crate :: { highlight:: HighlightLineRange , range:: Range , CLI , STDIN_CODE_DEFAULT_CHAR } ;
1717
18- pub fn create_code ( cli : & CLI , code_config : Code ) -> anyhow:: Result < Content > {
18+ pub fn create_content ( cli : & CLI , default_content : Code ) -> anyhow:: Result < Content > {
1919 let code = match cli. execute [ ..] {
20- [ ] => {
21- let range = Range :: from_opt_string ( cli. range . clone ( ) ) ?;
22- let code_snippet = get_code_snippet ( cli) ?;
23- let parsed_range = range. parse_range ( & code_snippet) ?;
24- let parsed_code_snippet = parsed_range. cut_code_snippet ( & code_snippet) ?;
25- let mut code = CodeBuilder :: default ( )
26- . content ( parsed_code_snippet)
27- . build ( ) ?;
28-
29- code. start_line_number = cli
30- . has_line_number
31- . then_some ( cli. start_line_number . unwrap_or ( parsed_range. 0 as u32 ) ) ;
32-
33- code. file_path = cli
34- . from_file
35- . clone ( )
36- . or ( cli. file_path . clone ( ) )
37- . or ( code_config. file_path ) ;
38- code. language = cli. language . clone ( ) . or ( code_config. language ) ;
39- code. highlight_lines = create_highlight_lines ( & cli, parsed_range, & code_snippet) ?;
40-
41- Content :: Code ( code)
42- }
20+ [ ] => match & cli. from_image {
21+ Some ( image) => create_image ( image) ,
22+ None => create_code ( cli, default_content) ,
23+ } ?,
4324 _ => {
4425 let command_content = cli
4526 . execute
@@ -132,17 +113,7 @@ fn get_code_snippet(cli: &CLI) -> anyhow::Result<String> {
132113 if let Some ( ref code) = cli. from_code {
133114 // Read code from pipe if the code option is "-"
134115 return Ok ( if code == STDIN_CODE_DEFAULT_CHAR {
135- // If input come from terminal, print help and exit
136- if stdin ( ) . is_terminal ( ) {
137- CLI :: command ( ) . print_help ( ) ?;
138- process:: exit ( 2 ) ;
139- }
140-
141- let mut content = String :: new ( ) ;
142-
143- BufReader :: new ( stdin ( ) . lock ( ) ) . read_to_string ( & mut content) ?;
144-
145- content
116+ String :: from_utf8 ( read_from_stdin ( ) ?) ?
146117 } else {
147118 code. clone ( )
148119 } ) ;
@@ -156,3 +127,51 @@ fn get_code_snippet(cli: &CLI) -> anyhow::Result<String> {
156127
157128 bail ! ( "No code snippet provided" ) ;
158129}
130+
131+ fn create_code ( cli : & CLI , default_content : Code ) -> anyhow:: Result < Content > {
132+ let range = Range :: from_opt_string ( cli. range . clone ( ) ) ?;
133+ let code_snippet = get_code_snippet ( cli) ?;
134+ let parsed_range = range. parse_range ( & code_snippet) ?;
135+ let parsed_code_snippet = parsed_range. cut_code_snippet ( & code_snippet) ?;
136+ let mut code = CodeBuilder :: default ( )
137+ . content ( parsed_code_snippet)
138+ . build ( ) ?;
139+
140+ code. start_line_number = cli
141+ . has_line_number
142+ . then_some ( cli. start_line_number . unwrap_or ( parsed_range. 0 as u32 ) ) ;
143+
144+ code. file_path = cli
145+ . from_file
146+ . clone ( )
147+ . or ( cli. file_path . clone ( ) )
148+ . or ( default_content. file_path ) ;
149+ code. language = cli. language . clone ( ) . or ( default_content. language ) ;
150+ code. highlight_lines = create_highlight_lines ( cli, parsed_range, & code_snippet) ?;
151+
152+ Ok ( Content :: Code ( code) )
153+ }
154+
155+ fn create_image ( image : & str ) -> anyhow:: Result < Content > {
156+ let image_data = if image == STDIN_CODE_DEFAULT_CHAR {
157+ read_from_stdin ( ) ?
158+ } else {
159+ image. as_bytes ( ) . to_owned ( )
160+ } ;
161+
162+ Ok ( Content :: Image ( image_data) )
163+ }
164+
165+ fn read_from_stdin ( ) -> anyhow:: Result < Vec < u8 > > {
166+ let mut content = vec ! [ ] ;
167+
168+ // If input come from terminal, print help and exit
169+ if stdin ( ) . is_terminal ( ) {
170+ CLI :: command ( ) . print_help ( ) ?;
171+ process:: exit ( 2 ) ;
172+ }
173+
174+ BufReader :: new ( stdin ( ) . lock ( ) ) . read_to_end ( & mut content) ?;
175+
176+ Ok ( content)
177+ }
0 commit comments