11use napi_derive:: napi;
22use std:: env;
3- use std:: fs:: { self , File } ;
3+ use std:: fs;
44use std:: io:: { self , Write } ;
55use std:: path:: Path ;
66use std:: process:: Command ;
77use serde:: { Serialize , Deserialize } ;
8- use tokio_postgres:: { NoTls , Error as PgError , Client as PgClient , Connection as PgConnection } ;
8+
9+ mod postgres;
10+ mod file;
11+
12+ use postgres:: PgResponse ;
913
1014// map std::process::Output so we could mimic the callback
1115// parameters of Node's child_process.exec()
@@ -17,75 +21,6 @@ pub struct ProcessOutput {
1721 pub stderr : String , // convert from Vec<u8>
1822}
1923
20- #[ napi( object) ]
21- #[ derive( Serialize , Deserialize ) ]
22- pub struct PgResponse {
23- pub code : String , // convert from SqlState
24- pub message : String ,
25- }
26-
27- fn get_postgres_error_message ( error : & PgError ) -> String {
28- if let Some ( db_error) = error. as_db_error ( ) {
29- db_error. message ( ) . to_string ( )
30- } else {
31- error. to_string ( )
32- }
33- }
34-
35- fn create_postgres_error_response ( error : & PgError ) -> napi:: Result < PgResponse > {
36- let ( error_code, error_details) = if let Some ( db_error) = error. as_db_error ( ) {
37- (
38- db_error. code ( ) . code ( ) . to_string ( ) ,
39- db_error. message ( ) . to_string ( )
40- )
41- } else {
42- (
43- "unknown" . to_string ( ) ,
44- error. to_string ( )
45- )
46- } ;
47-
48- let error_result = PgResponse {
49- code : error_code,
50- message : error_details
51- } ;
52-
53- Err ( napi:: Error :: new (
54- napi:: Status :: GenericFailure ,
55- serde_json:: to_string ( & error_result) . unwrap ( )
56- ) )
57- }
58-
59- async fn create_postgres_connection (
60- url : & str
61- ) -> Result < ( PgClient , PgConnection < tokio_postgres:: Socket , tokio_postgres:: tls:: NoTlsStream > ) , PgError > {
62- tokio_postgres:: connect ( url, NoTls ) . await
63- }
64-
65- async fn handle_postgres_connection (
66- conn : PgConnection < tokio_postgres:: Socket , tokio_postgres:: tls:: NoTlsStream >
67- ) {
68- if let Err ( error) = conn. await {
69- eprintln ! ( "Connection Error: {}" , get_postgres_error_message( & error) )
70- }
71- }
72-
73- fn read_file ( path : & str ) -> napi:: Result < File > {
74- fs:: File :: open ( path)
75- . map_err ( |error| napi:: Error :: new (
76- napi:: Status :: GenericFailure ,
77- format ! ( "Failed to open {}: {}" , path, error)
78- ) )
79- }
80-
81- fn create_file ( path : & str ) -> napi:: Result < File > {
82- fs:: File :: create ( path)
83- . map_err ( |error| napi:: Error :: new (
84- napi:: Status :: GenericFailure ,
85- format ! ( "Failed to create {}: {}" , path, error)
86- ) )
87- }
88-
8924#[ napi]
9025pub async fn run_npm_script ( script : String ) -> napi:: Result < ProcessOutput > {
9126 let home_dir = env:: var ( "HOME" ) . or_else ( |_| env:: var ( "USERPROFILE" ) ) . unwrap_or_default ( ) ;
@@ -121,42 +56,42 @@ pub async fn run_npm_script(script: String) -> napi::Result<ProcessOutput> {
12156
12257#[ napi]
12358pub async fn test_postgres_url ( url : String ) -> napi:: Result < PgResponse > {
124- let ( client, connection) = match create_postgres_connection ( & url) . await {
59+ let ( client, connection) = match postgres :: create_postgres_connection ( & url) . await {
12560 Ok ( ( client, connection) ) => ( client, connection) ,
12661 Err ( error) => {
127- return create_postgres_error_response ( & error) ;
62+ return postgres :: create_postgres_error_response ( & error) ;
12863 }
12964 } ;
13065
131- tokio:: spawn ( handle_postgres_connection ( connection) ) ;
66+ tokio:: spawn ( postgres :: handle_postgres_connection ( connection) ) ;
13267
13368 match client. simple_query ( "SELECT 1" ) . await {
13469 Ok ( _) => Ok ( PgResponse {
13570 code : "00000" . to_string ( ) ,
13671 message : "Success" . to_string ( )
13772 } ) ,
138- Err ( error) => create_postgres_error_response ( & error) ,
73+ Err ( error) => postgres :: create_postgres_error_response ( & error) ,
13974 }
14075}
14176
14277#[ napi]
14378pub async fn create_database ( url : String , database : String ) -> napi:: Result < PgResponse > {
144- let ( client, connection) = match create_postgres_connection ( & url) . await {
79+ let ( client, connection) = match postgres :: create_postgres_connection ( & url) . await {
14580 Ok ( ( client, connection) ) => ( client, connection) ,
14681 Err ( error) => {
147- return create_postgres_error_response ( & error) ;
82+ return postgres :: create_postgres_error_response ( & error) ;
14883 }
14984 } ;
15085
151- tokio:: spawn ( handle_postgres_connection ( connection) ) ;
86+ tokio:: spawn ( postgres :: handle_postgres_connection ( connection) ) ;
15287
15388 let query = format ! ( "CREATE DATABASE \" {}\" " , database) ;
15489 match client. simple_query ( & query) . await {
15590 Ok ( _) => Ok ( PgResponse {
15691 code : "00000" . to_string ( ) ,
15792 message : format ! ( "Successfully created database '{}'" , database)
15893 } ) ,
159- Err ( error) => create_postgres_error_response ( & error)
94+ Err ( error) => postgres :: create_postgres_error_response ( & error)
16095 }
16196}
16297
@@ -213,22 +148,22 @@ pub fn file_exists(file_path: String) -> bool {
213148
214149#[ napi]
215150pub async fn rename_database ( url : String , database : String , new_database_name : String ) -> napi:: Result < PgResponse > {
216- let ( client, connection) = match create_postgres_connection ( & url) . await {
151+ let ( client, connection) = match postgres :: create_postgres_connection ( & url) . await {
217152 Ok ( ( client, connection) ) => ( client, connection) ,
218153 Err ( error) => {
219- return create_postgres_error_response ( & error) ;
154+ return postgres :: create_postgres_error_response ( & error) ;
220155 }
221156 } ;
222157
223- tokio:: spawn ( handle_postgres_connection ( connection) ) ;
158+ tokio:: spawn ( postgres :: handle_postgres_connection ( connection) ) ;
224159
225160 let query = format ! ( "ALTER DATABASE \" {}\" RENAME TO \" {}\" ;" , database, new_database_name) ;
226161 match client. batch_execute ( & query) . await {
227162 Ok ( _) => Ok ( PgResponse {
228163 code : "00000" . to_string ( ) ,
229164 message : format ! ( "Database {} renamed to {}" , database, new_database_name)
230165 } ) ,
231- Err ( error) => create_postgres_error_response ( & error)
166+ Err ( error) => postgres :: create_postgres_error_response ( & error)
232167 }
233168}
234169
@@ -246,12 +181,12 @@ pub fn copy_file(
246181 destination : String ,
247182 create_dest_if_not_exists : Option < bool >
248183) -> napi:: Result < ( ) > {
249- let mut source_file = read_file ( & source) ?;
184+ let mut source_file = file :: read_file ( & source) ?;
250185
251186 let mut destination_file = if create_dest_if_not_exists. unwrap_or ( false ) {
252- create_file ( & destination) ?
187+ file :: create_file ( & destination) ?
253188 } else {
254- read_file ( & destination) ?
189+ file :: read_file ( & destination) ?
255190 } ;
256191
257192 io:: copy ( & mut source_file, & mut destination_file)
0 commit comments