@@ -470,22 +470,21 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
470470 // if require_used, create a temporary directory and copy date.lua there.
471471 // we do this outside the "require_used" setup below as the tempdir
472472 // needs to persist until the end of the program.
473- let temp_dir =
474- if require_used {
475- match tempfile:: tempdir ( ) {
476- Ok ( temp_dir) => {
477- let temp_dir_path = temp_dir. into_path ( ) ;
478- Some ( temp_dir_path)
479- } ,
480- Err ( e) => {
481- return fail_clierror ! (
482- "Cannot create temporary directory to copy luadate library to: {e}"
483- )
484- } ,
485- }
486- } else {
487- None
488- } ;
473+ let temp_dir = if require_used {
474+ match tempfile:: tempdir ( ) {
475+ Ok ( temp_dir) => {
476+ let temp_dir_path = temp_dir. into_path ( ) ;
477+ Some ( temp_dir_path)
478+ } ,
479+ Err ( e) => {
480+ return fail_clierror ! (
481+ "Cannot create temporary directory to copy luadate library to: {e}"
482+ )
483+ } ,
484+ }
485+ } else {
486+ None
487+ } ;
489488
490489 // "require " was used in the scripts, so we need to prepare luadate library and setup LUAU_PATH
491490 if require_used {
@@ -528,12 +527,11 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
528527 let globals = luau. globals ( ) ;
529528
530529 // check the QSV_CKAN_API environment variable
531- let ckan_api =
532- if let Ok ( api) = std:: env:: var ( "QSV_CKAN_API" ) {
533- api
534- } else {
535- args. flag_ckan_api . clone ( )
536- } ;
530+ let ckan_api = if let Ok ( api) = std:: env:: var ( "QSV_CKAN_API" ) {
531+ api
532+ } else {
533+ args. flag_ckan_api . clone ( )
534+ } ;
537535
538536 // check the QSV_CKAN_TOKEN environment variable
539537 let ckan_token = if let Ok ( token) = std:: env:: var ( "QSV_CKAN_TOKEN" ) {
@@ -1480,17 +1478,21 @@ fn setup_helpers(
14801478
14811479 // this is a helper function that can be called from Luau scripts
14821480 // to coalesce - return the first non-null value in a list
1483- let qsv_coalesce =
1484- luau. create_function ( |luau, mut args : mlua:: MultiValue | {
1485- while let Some ( val) = args. pop_front ( ) {
1486- let val = luau. from_value :: < serde_json:: Value > ( val) ?;
1487- let val_str = val. as_str ( ) . unwrap_or_default ( ) ;
1488- if !val_str. is_empty ( ) {
1489- return Ok ( val_str. to_string ( ) ) ;
1490- }
1481+ //
1482+ // qsv_coalesce(arg1, .., argN)
1483+ // returns: first non-null value of the arguments
1484+ // or an empty string if all arguments are null
1485+ //
1486+ let qsv_coalesce = luau. create_function ( |luau, mut args : mlua:: MultiValue | {
1487+ while let Some ( val) = args. pop_front ( ) {
1488+ let val = luau. from_value :: < serde_json:: Value > ( val) ?;
1489+ let val_str = val. as_str ( ) . unwrap_or_default ( ) ;
1490+ if !val_str. is_empty ( ) {
1491+ return Ok ( val_str. to_string ( ) ) ;
14911492 }
1492- Ok ( String :: new ( ) )
1493- } ) ?;
1493+ }
1494+ Ok ( String :: new ( ) )
1495+ } ) ?;
14941496 luau. globals ( ) . set ( "qsv_coalesce" , qsv_coalesce) ?;
14951497
14961498 // this is a helper function that can be called from the BEGIN and MAIN script
@@ -1537,20 +1539,24 @@ fn setup_helpers(
15371539 // qsv_sleep(milliseconds: number)
15381540 // returns: None
15391541 //
1540- let qsv_sleep =
1541- luau. create_function ( |_, args : mlua:: Number | {
1542- let sleep_time = args as u64 ;
1543- if sleep_time > 0 {
1544- log:: info!( "sleeping for {} milliseconds" , sleep_time) ;
1545- std:: thread:: sleep ( std:: time:: Duration :: from_millis ( sleep_time) ) ;
1546- }
1542+ let qsv_sleep = luau. create_function ( |_, args : mlua:: Number | {
1543+ let sleep_time = args as u64 ;
1544+ if sleep_time > 0 {
1545+ log:: info!( "sleeping for {} milliseconds" , sleep_time) ;
1546+ std:: thread:: sleep ( std:: time:: Duration :: from_millis ( sleep_time) ) ;
1547+ }
15471548
1548- Ok ( ( ) )
1549- } ) ?;
1549+ Ok ( ( ) )
1550+ } ) ?;
15501551 luau. globals ( ) . set ( "qsv_sleep" , qsv_sleep) ?;
15511552
15521553 // this is a helper function that can be called from the MAIN script
15531554 // to skip writing the current row's output when processing CSVs.
1555+ //
1556+ // qsv_skip()
1557+ // returns: None
1558+ // or Luau runtime error if called from BEGIN or END scripts
1559+ //
15541560 let qsv_skip = luau. create_function ( |_, ( ) | {
15551561 if LUAU_STAGE . load ( Ordering :: Relaxed ) != Stage :: Main as i8 {
15561562 return helper_err ! (
@@ -1578,17 +1584,16 @@ fn setup_helpers(
15781584 // as soon as the BEGIN script is actually executed.
15791585 // A Luau runtime error is also returned if called from MAIN or END.
15801586 //
1581- let qsv_autoindex =
1582- luau. create_function ( |_, ( ) | {
1583- if LUAU_STAGE . load ( Ordering :: Relaxed ) != Stage :: Begin as i8 {
1584- return helper_err ! (
1585- "qsv_autoindex" ,
1586- "qsv_autoindex() can only be called from the BEGIN script."
1587- ) ;
1588- }
1587+ let qsv_autoindex = luau. create_function ( |_, ( ) | {
1588+ if LUAU_STAGE . load ( Ordering :: Relaxed ) != Stage :: Begin as i8 {
1589+ return helper_err ! (
1590+ "qsv_autoindex" ,
1591+ "qsv_autoindex() can only be called from the BEGIN script."
1592+ ) ;
1593+ }
15891594
1590- Ok ( ( ) )
1591- } ) ?;
1595+ Ok ( ( ) )
1596+ } ) ?;
15921597 luau. globals ( ) . set ( "qsv_autoindex" , qsv_autoindex) ?;
15931598
15941599 // this is a helper function to set an environment variable.
@@ -1602,24 +1607,30 @@ fn setup_helpers(
16021607 // returns: None
16031608 // A Luau runtime error if the envvar is empty.
16041609 //
1605- let qsv_setenv =
1606- luau. create_function ( |_, ( envvar, value) : ( String , String ) | {
1607- if envvar. is_empty ( ) {
1608- return helper_err ! ( "qsv_setenv" , "envvar cannot be empty." ) ;
1609- }
1610+ let qsv_setenv = luau. create_function ( |_, ( envvar, value) : ( String , String ) | {
1611+ if envvar. is_empty ( ) {
1612+ return helper_err ! ( "qsv_setenv" , "envvar cannot be empty." ) ;
1613+ }
16101614
1611- if value. is_empty ( ) {
1612- std:: env:: remove_var ( envvar) ;
1613- } else {
1614- std:: env:: set_var ( envvar, value) ;
1615- }
1615+ if value. is_empty ( ) {
1616+ std:: env:: remove_var ( envvar) ;
1617+ } else {
1618+ std:: env:: set_var ( envvar, value) ;
1619+ }
16161620
1617- Ok ( ( ) )
1618- } ) ?;
1621+ Ok ( ( ) )
1622+ } ) ?;
16191623 luau. globals ( ) . set ( "qsv_setenv" , qsv_setenv) ?;
16201624
16211625 // this is a helper function to get the value of an environment variable.
16221626 // Note that the environment variable is read from the parent AND current processes.
1627+ //
1628+ // qsv_getenv(envar: string)
1629+ // envvar: the name of the environment variable to get
1630+ // returns: The value of the environment variable or an empty string if the
1631+ // environment variable is not set.
1632+ // A Luau runtime error if the envvar argument is empty.
1633+ //
16231634 let qsv_getenv = luau. create_function ( |_, envvar : String | {
16241635 if envvar. is_empty ( ) {
16251636 return helper_err ! ( "qsv_getenv" , "envvar cannot be empty." ) ;
@@ -1639,15 +1650,14 @@ fn setup_helpers(
16391650 // returns: true if the file exists, false otherwise.
16401651 // A Luau runtime error if the filepath argument is empty.
16411652 //
1642- let qsv_fileexists =
1643- luau. create_function ( |_, filepath : String | {
1644- if filepath. is_empty ( ) {
1645- return helper_err ! ( "qsv_fileexists" , "filepath cannot be empty." ) ;
1646- }
1653+ let qsv_fileexists = luau. create_function ( |_, filepath : String | {
1654+ if filepath. is_empty ( ) {
1655+ return helper_err ! ( "qsv_fileexists" , "filepath cannot be empty." ) ;
1656+ }
16471657
1648- let path = Path :: new ( & filepath) ;
1649- Ok ( path. exists ( ) )
1650- } ) ?;
1658+ let path = Path :: new ( & filepath) ;
1659+ Ok ( path. exists ( ) )
1660+ } ) ?;
16511661 luau. globals ( ) . set ( "qsv_fileexists" , qsv_fileexists) ?;
16521662
16531663 // this is a helper function to load a CSV into a Luau table.
@@ -1767,9 +1777,9 @@ fn setup_helpers(
17671777 let mut file = if newfile_flag {
17681778 // create a new file. If the file already exists, overwrite it.
17691779 std:: fs:: File :: create ( sanitized_filename. clone ( ) ) . map_err ( |e| {
1770- mlua:: Error :: RuntimeError (
1771- format ! ( "qsv_writefile() - Error creating a new file: {e}" )
1772- )
1780+ mlua:: Error :: RuntimeError ( format ! (
1781+ "qsv_writefile() - Error creating a new file: {e}"
1782+ ) )
17731783 } ) ?
17741784 } else {
17751785 // append to an existing file. If the file does not exist, create it.
@@ -1779,19 +1789,19 @@ fn setup_helpers(
17791789 . append ( true )
17801790 . open ( sanitized_filename. clone ( ) )
17811791 . map_err ( |e| {
1782- mlua:: Error :: RuntimeError (
1783- format ! ( "qsv_writefile() - Error opening existing file: {e}" )
1784- )
1792+ mlua:: Error :: RuntimeError ( format ! (
1793+ "qsv_writefile() - Error opening existing file: {e}"
1794+ ) )
17851795 } ) ?
17861796 } ;
17871797 if newfile_flag {
17881798 log:: info!( "qsv_writefile() - created file: {sanitized_filename}" ) ;
17891799 } else {
17901800 let data_as_bytes = data. as_bytes ( ) ;
17911801 file. write_all ( data_as_bytes) . map_err ( |e| {
1792- mlua:: Error :: RuntimeError (
1793- format ! ( "qsv_writefile() - Error appending to existing file: {e}" )
1794- )
1802+ mlua:: Error :: RuntimeError ( format ! (
1803+ "qsv_writefile() - Error appending to existing file: {e}"
1804+ ) )
17951805 } ) ?;
17961806 log:: info!(
17971807 "qsv_writefile() - appending {} bytes to file: {sanitized_filename}" ,
@@ -2287,7 +2297,6 @@ fn setup_helpers(
22872297 let download_elapsed = download_start. elapsed ( ) . as_millis ( ) ;
22882298 writeln ! ( cache_file, "# Download-duration-ms: {download_elapsed}" ) ?;
22892299 cache_file. write_all ( lookup_csv_contents. as_bytes ( ) ) ?;
2290- cache_file. flush ( ) ?;
22912300 }
22922301
22932302 lookup_table_uri = cache_file_path. to_string_lossy ( ) . to_string ( ) ;
0 commit comments