diff --git a/Cargo.toml b/Cargo.toml index 231d7fee5..2069454f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,8 @@ path = "src/lib.rs" [features] # Enable JSON output in the `cli` example: json_example = ["serde_json", "serde"] +std = [] +default = ["std"] [dependencies] bigdecimal = { version = "0.2", features = ["serde"], optional = true } diff --git a/src/ast/data_type.rs b/src/ast/data_type.rs index 53122ab5d..774541804 100644 --- a/src/ast/data_type.rs +++ b/src/ast/data_type.rs @@ -11,9 +11,11 @@ // limitations under the License. use super::ObjectName; +#[cfg(not(feature = "std"))] +use alloc::boxed::Box; +use core::fmt; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use std::fmt; /// SQL data types #[derive(Debug, Clone, PartialEq, Eq, Hash)] diff --git a/src/ast/ddl.rs b/src/ast/ddl.rs index 847ee71a3..4a1ddd696 100644 --- a/src/ast/ddl.rs +++ b/src/ast/ddl.rs @@ -15,9 +15,11 @@ use super::{display_comma_separated, DataType, Expr, Ident, ObjectName}; use crate::ast::display_separated; use crate::tokenizer::Token; +#[cfg(not(feature = "std"))] +use alloc::{boxed::Box, string::ToString, vec::Vec}; +use core::fmt; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use std::fmt; /// An `ALTER TABLE` (`Statement::AlterTable`) operation #[derive(Debug, Clone, PartialEq, Eq, Hash)] diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 4232ad022..e59c7470b 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -18,9 +18,15 @@ mod operator; mod query; mod value; +#[cfg(not(feature = "std"))] +use alloc::{ + boxed::Box, + string::{String, ToString}, + vec::Vec, +}; +use core::fmt; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use std::fmt; pub use self::data_type::DataType; pub use self::ddl::{ diff --git a/src/ast/operator.rs b/src/ast/operator.rs index 57e70982f..599d36984 100644 --- a/src/ast/operator.rs +++ b/src/ast/operator.rs @@ -10,9 +10,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +use core::fmt; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use std::fmt; /// Unary operators #[derive(Debug, Clone, PartialEq, Eq, Hash)] diff --git a/src/ast/value.rs b/src/ast/value.rs index 9e82c175d..1ba1bfa61 100644 --- a/src/ast/value.rs +++ b/src/ast/value.rs @@ -10,11 +10,13 @@ // See the License for the specific language governing permissions and // limitations under the License. +#[cfg(not(feature = "std"))] +use alloc::string::String; #[cfg(feature = "bigdecimal")] use bigdecimal::BigDecimal; +use core::fmt; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use std::fmt; /// Primitive SQL values such as number and string #[derive(Debug, Clone, PartialEq, Eq, Hash)] diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs index e656ab269..317f4f357 100644 --- a/src/dialect/mod.rs +++ b/src/dialect/mod.rs @@ -19,8 +19,8 @@ mod postgresql; mod snowflake; mod sqlite; -use std::any::{Any, TypeId}; -use std::fmt::Debug; +use core::any::{Any, TypeId}; +use core::fmt::Debug; pub use self::ansi::AnsiDialect; pub use self::generic::GenericDialect; diff --git a/src/lib.rs b/src/lib.rs index 5b2324579..64f282be8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,7 +33,9 @@ //! println!("AST: {:?}", ast); //! ``` #![warn(clippy::all)] - +#![cfg_attr(not(feature = "std"), no_std)] +extern crate alloc; +extern crate core; pub mod ast; #[macro_use] pub mod dialect; diff --git a/src/parser.rs b/src/parser.rs index 94afeb6e9..050acb612 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -18,8 +18,17 @@ use super::ast::*; use super::dialect::keywords::Keyword; use super::dialect::*; use super::tokenizer::*; +#[cfg(not(feature = "std"))] +use alloc::{ + boxed::Box, + format, + string::{String, ToString}, + vec, + vec::Vec, +}; +use core::fmt; +#[cfg(feature = "std")] use std::error::Error; -use std::fmt; #[derive(Debug, Clone, PartialEq)] pub enum ParserError { @@ -79,6 +88,7 @@ impl fmt::Display for ParserError { } } +#[cfg(feature = "std")] impl Error for ParserError {} pub struct Parser<'a> { diff --git a/src/test_utils.rs b/src/test_utils.rs index 2fcacffa9..0f325e360 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -10,13 +10,20 @@ // See the License for the specific language governing permissions and // limitations under the License. +#[cfg(not(feature = "std"))] +use alloc::{ + boxed::Box, + string::{String, ToString}, + vec, + vec::Vec, +}; /// This module contains internal utilities used for testing the library. /// While technically public, the library's users are not supposed to rely /// on this module, as it will change without notice. // // Integration tests (i.e. everything under `tests/`) import this // via `tests/test_utils/mod.rs`. -use std::fmt::Debug; +use core::fmt::Debug; use super::ast::*; use super::dialect::*; diff --git a/src/tokenizer.rs b/src/tokenizer.rs index bbad1a4c4..717147bf6 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -16,15 +16,24 @@ //! //! The tokens then form the input for the parser, which outputs an Abstract Syntax Tree (AST). -use std::iter::Peekable; -use std::str::Chars; +use core::iter::Peekable; +use core::str::Chars; use super::dialect::keywords::{Keyword, ALL_KEYWORDS, ALL_KEYWORDS_INDEX}; use super::dialect::Dialect; use super::dialect::SnowflakeDialect; +use core::fmt; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use std::fmt; + +#[cfg(not(feature = "std"))] +use alloc::{ + borrow::ToOwned, + format, + string::{String, ToString}, + vec, + vec::Vec, +}; /// SQL Token enumeration #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -831,6 +840,8 @@ mod tests { let dialect = GenericDialect {}; let mut tokenizer = Tokenizer::new(&dialect, &sql); let tokens = tokenizer.tokenize().unwrap(); + + #[cfg(feature = "std")] println!("tokens: {:#?}", tokens); let expected = vec![ Token::Whitespace(Whitespace::Newline), @@ -878,6 +889,8 @@ mod tests { let dialect = GenericDialect {}; let mut tokenizer = Tokenizer::new(&dialect, &sql); let tokens = tokenizer.tokenize().unwrap(); + + #[cfg(feature = "std")] println!("tokens: {:#?}", tokens); let expected = vec![ Token::Whitespace(Whitespace::Newline),