11//! Module for structuring the Sql input
2+ #[ cfg( feature = "std" ) ]
23use crate :: files:: { SqlContent , SqlContentSet } ;
4+ #[ cfg( feature = "std" ) ]
35use std:: {
46 io,
57 path:: { Path , PathBuf } ,
68} ;
79
10+ use alloc:: { borrow:: ToOwned , string:: String , vec:: Vec } ;
11+
812/// Holds the optional path and content of the `sql` to be parsed
913#[ derive( Debug ) ]
1014pub struct SqlSource {
15+ #[ cfg( feature = "std" ) ]
1116 path : Option < PathBuf > ,
1217 content : String ,
1318}
1419
20+ #[ cfg( not( feature = "std" ) ) ]
21+ impl From < String > for SqlSource {
22+ /// Creates an [`SqlSource`] from a a [`String`]
23+ fn from ( content : String ) -> Self {
24+ Self { content }
25+ }
26+ }
27+
28+ #[ cfg( feature = "std" ) ]
29+ impl From < String > for SqlSource {
30+ /// Creates an [`SqlSource`] from a a [`String`]
31+ fn from ( content : String ) -> Self {
32+ Self { content, path : None }
33+ }
34+ }
35+
36+ #[ cfg( feature = "std" ) ]
1537impl SqlSource {
1638 /// Loads a [`SqlSource`] from the given path.
1739 ///
@@ -25,7 +47,7 @@ impl SqlSource {
2547
2648 /// Creates an [`SqlSource`] from a a [`String`] and a [`Option<PathBuf>`]
2749 #[ must_use]
28- pub const fn from_str ( content : String , path : Option < PathBuf > ) -> Self {
50+ pub const fn from_str_with_path ( content : String , path : Option < PathBuf > ) -> Self {
2951 Self { path, content }
3052 }
3153
@@ -40,12 +62,6 @@ impl SqlSource {
4062 self . path . clone ( )
4163 }
4264
43- /// Returns the raw SQL text contained in this file.
44- #[ must_use]
45- pub fn content ( & self ) -> & str {
46- & self . content
47- }
48-
4965 /// Recursively discovers `.sql` files under `path`, applies the optional
5066 /// deny list, and loads the contents of each file.
5167 ///
@@ -58,6 +74,7 @@ impl SqlSource {
5874 ///
5975 /// Returns an [`io::Error`] if directory traversal fails or if any of the
6076 /// discovered SQL files cannot be read.
77+ #[ cfg( feature = "std" ) ]
6178 pub fn sql_sources ( path : & Path , deny_list : & [ String ] ) -> io:: Result < Vec < Self > > {
6279 let sql_content_set = SqlContentSet :: new ( path, deny_list) ?;
6380
@@ -70,14 +87,22 @@ impl SqlSource {
7087 }
7188}
7289
90+ impl SqlSource {
91+ /// Returns the raw SQL text contained in this file.
92+ #[ must_use]
93+ pub fn content ( & self ) -> & str {
94+ & self . content
95+ }
96+ }
97+
7398#[ cfg( test) ]
7499mod tests {
75- use std:: { env, fs, path:: PathBuf } ;
76-
77100 use crate :: source:: SqlSource ;
78101
102+ #[ cfg( feature = "std" ) ]
79103 #[ test]
80104 fn test_sql_file_read ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
105+ use std:: { env, fs, path:: PathBuf } ;
81106 let base = env:: temp_dir ( ) . join ( "recursive_scan_test3" ) ;
82107 let _ = fs:: remove_dir_all ( & base) ;
83108 fs:: create_dir_all ( & base) ?;
@@ -109,8 +134,12 @@ mod tests {
109134 Ok ( ( ) )
110135 }
111136
137+ #[ cfg( not( feature = "std" ) ) ]
112138 #[ test]
113139 fn test_sql_file_new_from_str_has_no_path_and_preserves_content ( ) {
140+ use alloc:: boxed:: Box ;
141+ use alloc:: vec;
142+ use alloc:: vec:: Vec ;
114143 let sql = "SELECT * FROM users;" ;
115144 let file = SqlSource :: from_str ( sql. to_owned ( ) , None ) ;
116145 assert ! ( file. path( ) . is_none( ) ) ;
0 commit comments