Skip to content

Commit d6763d4

Browse files
committed
Began implementing no-std support
1 parent b7b5be3 commit d6763d4

File tree

9 files changed

+102
-31
lines changed

9 files changed

+102
-31
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ categories = [
1818
]
1919

2020
[features]
21-
default = []
21+
default = ["std"]
22+
std = []
2223
fuzzing = []
2324

2425
[dependencies]

src/ast.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
//!
33
//! This module does not interpret semantics; it only produces an AST + file metadata.
44
5-
use std::path::{Path, PathBuf};
6-
75
use sqlparser::{
86
ast::Statement,
97
dialect::Dialect,
108
parser::{Parser, ParserError},
119
};
1210

11+
use alloc::vec::Vec;
12+
1313
use crate::source::SqlSource;
1414

1515
/// A single SQL file plus all [`Statement`].
@@ -46,13 +46,13 @@ impl ParsedSqlFile {
4646

4747
/// Getter method for returning the current object's file's path
4848
#[must_use]
49-
pub fn path(&self) -> Option<&Path> {
49+
pub fn path(&self) -> Option<&std::path::Path> {
5050
self.file.path()
5151
}
5252

53-
/// Getter that returns an [`PathBuf`] for the path rather than `&Path`
53+
/// Getter that returns an [`std::path::PathBuf`] for the path rather than `&Path`
5454
#[must_use]
55-
pub fn path_into_path_buf(&self) -> Option<PathBuf> {
55+
pub fn path_into_path_buf(&self) -> Option<std::path::PathBuf> {
5656
self.file.path_into_path_buf()
5757
}
5858

@@ -102,9 +102,10 @@ impl ParsedSqlFileSet {
102102

103103
#[cfg(test)]
104104
mod tests {
105-
use std::{env, fs};
106-
105+
use alloc::borrow::ToOwned;
106+
use alloc::boxed::Box;
107107
use sqlparser::dialect::{GenericDialect, PostgreSqlDialect};
108+
use std::{env, fs};
108109

109110
use super::*;
110111
use crate::source::SqlSource;

src/comments.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ use std::fmt;
99

1010
use crate::ast::ParsedSqlFile;
1111

12+
use alloc::borrow::ToOwned;
13+
use alloc::string::String;
14+
use alloc::vec::Vec;
15+
1216
/// Represents a line/column location within a source file.
1317
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd)]
1418
pub struct Location {
@@ -466,9 +470,13 @@ pub enum MultiFlatten<'a> {
466470

467471
#[cfg(test)]
468472
mod tests {
469-
use std::{env, fs};
470-
473+
use alloc::borrow::ToOwned;
474+
use alloc::boxed::Box;
475+
use alloc::format;
476+
use alloc::string::{String, ToString};
477+
use alloc::{vec, vec::Vec};
471478
use sqlparser::dialect::GenericDialect;
479+
use std::{env, fs};
472480

473481
use crate::comments::{Comment, CommentError, CommentKind, Comments, Location, Span};
474482

src/docs.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ use std::path::{Path, PathBuf};
55

66
use sqlparser::ast::{Ident, ObjectName, ObjectNamePart, Spanned, Statement};
77

8+
use alloc::borrow::ToOwned;
9+
use alloc::string::String;
10+
use alloc::vec::Vec;
11+
812
use crate::{
913
ast::ParsedSqlFile,
1014
comments::{Comments, LeadingCommentCapture, MultiFlatten},
@@ -332,14 +336,18 @@ fn schema_and_table(name: &ObjectName) -> Result<(Option<String>, String), DocEr
332336

333337
#[cfg(test)]
334338
mod tests {
339+
use alloc::borrow::ToOwned;
340+
use alloc::boxed::Box;
341+
use alloc::string::String;
342+
use alloc::string::ToString;
343+
use alloc::{vec, vec::Vec};
335344
use core::fmt;
336-
use std::{env, fs, path::PathBuf};
337-
338345
use sqlparser::{
339346
ast::{Ident, ObjectName, ObjectNamePart, ObjectNamePartFunction},
340347
dialect::GenericDialect,
341348
tokenizer::Span,
342349
};
350+
use std::{env, fs, path::PathBuf};
343351

344352
use crate::{
345353
docs::{ColumnDoc, SqlFileDoc, TableDoc, schema_and_table},

src/error.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ use crate::{
44
comments::CommentError,
55
docs::{ColumnDoc, TableDoc},
66
};
7+
use alloc::{string::String, vec::Vec};
78
use core::fmt;
89
use sqlparser::parser::ParserError;
9-
use std::{error, fmt::Debug};
1010

1111
/// Errors that can occur while discovering, parsing, or documenting SQL files.
1212
#[non_exhaustive]
1313
#[derive(Debug)]
1414
pub enum DocError {
1515
/// Wrapper for standard [`std::io::Error`]
16+
#[cfg(feature = "std")]
1617
FileReadError(std::io::Error),
1718
/// Wrapper for [`CommentError`]
1819
CommentError(CommentError),
@@ -90,8 +91,9 @@ impl fmt::Display for DocError {
9091
}
9192
}
9293

93-
impl error::Error for DocError {
94-
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
94+
#[cfg(feature = "std")]
95+
impl std::error::Error for DocError {
96+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
9597
match self {
9698
Self::FileReadError(e) => Some(e),
9799
Self::CommentError(e) => Some(e),
@@ -129,7 +131,7 @@ mod tests {
129131
use sqlparser::parser::ParserError;
130132

131133
use crate::{comments::CommentError, docs::TableDoc, error::DocError};
132-
134+
use alloc::{borrow::ToOwned, string::ToString, vec};
133135
#[test]
134136
fn test_doc_errors() {
135137
use std::fs;

src/files.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
#![cfg(feature = "std")]
12
//! Discover and filter `.sql` files on disk.
23
//!
34
//! This module finds file paths; it does not parse SQL or extract comments.
4-
55
use std::{
66
fs, io,
77
path::{Path, PathBuf},
8+
string::String,
9+
vec::Vec,
810
};
9-
1011
/// A list of SQL files to exclude from processing.
1112
///
1213
/// Entries in the deny list are treated as full [`PathBuf`] paths.
@@ -160,9 +161,15 @@ impl SqlContentSet {
160161

161162
#[cfg(test)]
162163
mod tests {
163-
use std::{env, fs};
164-
165164
use super::*;
165+
use std::{
166+
boxed::Box,
167+
env, fs,
168+
path::{Path, PathBuf},
169+
string::ToString,
170+
vec,
171+
vec::Vec,
172+
};
166173

167174
#[test]
168175
fn test_recursive_scan_finds_only_sql_files_recursively()

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![deny(missing_docs)]
33
#![deny(rustdoc::broken_intra_doc_links)]
44
#![deny(rustdoc::private_intra_doc_links)]
5+
#![cfg_attr(not(feature = "std"), no_std)]
56
//!
67
//! ## Module layout
78
//!
@@ -12,11 +13,13 @@
1213
//! - [`sql_doc`] — Build the top-level [`SqlDoc`] and primary entry point
1314
//!
1415
//! **Start here:** [`SqlDoc::from_dir`], [`SqlDoc::from_path`], or [`SqlDoc::builder_from_str`]
16+
extern crate alloc;
1517

1618
pub mod ast;
1719
pub mod comments;
1820
pub mod docs;
1921
pub mod error;
22+
#[cfg(feature = "std")]
2023
pub mod files;
2124
pub mod source;
2225
pub mod sql_doc;

src/source.rs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
11
//! Module for structuring the Sql input
2+
#[cfg(feature = "std")]
23
use crate::files::{SqlContent, SqlContentSet};
4+
#[cfg(feature = "std")]
35
use 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)]
1014
pub 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")]
1537
impl 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)]
7499
mod 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());

src/sql_doc.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
//! Public entry point for building [`SqlDoc`] from a directory, file, or string.
2-
2+
#[cfg(feature = "std")]
33
use std::{
44
path::{Path, PathBuf},
55
vec,
66
};
77

8+
use alloc::borrow::ToOwned;
9+
use alloc::string::String;
10+
use alloc::vec::Vec;
811
use sqlparser::dialect::Dialect;
912

1013
use crate::{
@@ -18,6 +21,7 @@ use crate::{
1821

1922
/// Top-level documentation object containing all discovered [`TableDoc`] entries.
2023
#[derive(Clone, Debug, Eq, PartialEq)]
24+
2125
pub struct SqlDoc {
2226
/// Holds the [`Vec`] of all tables found in all specified files.
2327
tables: Vec<TableDoc>,
@@ -285,10 +289,14 @@ pub struct SqlDocBuilder<'a> {
285289
/// Enum for specifying a file doc source as a `directory` or a specific `file`
286290
#[derive(Debug, Eq, PartialEq)]
287291
enum SqlFileDocSource<'a> {
292+
#[cfg(feature = "std")]
288293
Dir(PathBuf),
294+
#[cfg(feature = "std")]
289295
File(PathBuf),
296+
#[cfg(feature = "std")]
290297
Files(Vec<PathBuf>),
291298
FromString(&'a str),
299+
#[cfg(feature = "std")]
292300
FromStringsWithPaths(&'a [(String, PathBuf)]),
293301
}
294302

@@ -462,6 +470,11 @@ fn generate_docs_from_strs_with_paths<D: Dialect + Default>(
462470

463471
#[cfg(test)]
464472
mod tests {
473+
use alloc::borrow::ToOwned;
474+
use alloc::boxed::Box;
475+
use alloc::format;
476+
use alloc::string::String;
477+
use alloc::vec::Vec;
465478
use std::{
466479
env, fs,
467480
path::{Path, PathBuf},
@@ -797,7 +810,6 @@ mod tests {
797810
let built = SqlDoc::builder_from_str(sql)
798811
.flatten_multiline_with(" | ")
799812
.build::<GenericDialect>()?;
800-
dbg!(&built);
801813
let t = built.table("t", None)?;
802814
assert_eq!(t.doc(), Some("hello | world"));
803815
assert_eq!(t.columns()[0].doc(), Some("x | y | z"));

0 commit comments

Comments
 (0)