From ae8d7da8b9da87563dddabb76c88ef5f00031db1 Mon Sep 17 00:00:00 2001 From: koushiro Date: Fri, 20 Aug 2021 15:26:39 +0800 Subject: [PATCH 1/2] Support `no_std` Signed-off-by: koushiro --- .github/workflows/rust.yml | 10 ++++++++++ Cargo.toml | 3 +++ src/ast/data_type.rs | 4 +++- src/ast/ddl.rs | 4 +++- src/ast/mod.rs | 8 +++++++- src/ast/operator.rs | 2 +- src/ast/query.rs | 3 +++ src/ast/value.rs | 4 +++- src/dialect/mod.rs | 4 ++-- src/lib.rs | 5 +++++ src/parser.rs | 14 +++++++++++--- src/test_utils.rs | 10 +++++++++- src/tokenizer.rs | 14 +++++++++++--- 13 files changed, 71 insertions(+), 14 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 078d8bae0..44880aafd 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -35,6 +35,16 @@ jobs: - uses: actions/checkout@master - run: cargo check --all-targets --all-features + compile-no-std: + runs-on: ubuntu-latest + steps: + - name: Set up Rust + uses: hecrj/setup-rust-action@v1 + with: + targets: 'thumbv6m-none-eabi' + - uses: actions/checkout@master + - run: cargo check --no-defatult-features --target thumbv6m-none-eabi + test: strategy: matrix: diff --git a/Cargo.toml b/Cargo.toml index e0ee25ff0..0d84892c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,12 +13,15 @@ include = [ "Cargo.toml", ] edition = "2018" +resolver = "2" [lib] name = "sqlparser" path = "src/lib.rs" [features] +default = ["std"] +std = [] # Enable JSON output in the `cli` example: json_example = ["serde_json", "serde"] diff --git a/src/ast/data_type.rs b/src/ast/data_type.rs index d5fc3f292..7bf3d7f4a 100644 --- a/src/ast/data_type.rs +++ b/src/ast/data_type.rs @@ -10,7 +10,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::fmt; +#[cfg(not(feature = "std"))] +use alloc::boxed::Box; +use core::fmt; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; diff --git a/src/ast/ddl.rs b/src/ast/ddl.rs index df5603881..1d64abaa2 100644 --- a/src/ast/ddl.rs +++ b/src/ast/ddl.rs @@ -13,7 +13,9 @@ //! AST types specific to CREATE/ALTER variants of [Statement] //! (commonly referred to as Data Definition Language, or DDL) -use std::fmt; +#[cfg(not(feature = "std"))] +use alloc::{boxed::Box, string::ToString, vec::Vec}; +use core::fmt; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 407821d36..a3dcb0c18 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -18,7 +18,13 @@ mod operator; mod query; mod value; -use std::fmt; +#[cfg(not(feature = "std"))] +use alloc::{ + boxed::Box, + string::{String, ToString}, + vec::Vec, +}; +use core::fmt; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; diff --git a/src/ast/operator.rs b/src/ast/operator.rs index a9d90f7fb..97091149b 100644 --- a/src/ast/operator.rs +++ b/src/ast/operator.rs @@ -10,7 +10,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::fmt; +use core::fmt; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; diff --git a/src/ast/query.rs b/src/ast/query.rs index 857cae9d1..b42253166 100644 --- a/src/ast/query.rs +++ b/src/ast/query.rs @@ -10,6 +10,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +#[cfg(not(feature = "std"))] +use alloc::{boxed::Box, vec::Vec}; + #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; diff --git a/src/ast/value.rs b/src/ast/value.rs index 779ffa753..1cf111daa 100644 --- a/src/ast/value.rs +++ b/src/ast/value.rs @@ -10,7 +10,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::fmt; +#[cfg(not(feature = "std"))] +use alloc::string::String; +use core::fmt; #[cfg(feature = "bigdecimal")] use bigdecimal::BigDecimal; diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs index 23c7f9baa..ba6819017 100644 --- a/src/dialect/mod.rs +++ b/src/dialect/mod.rs @@ -20,8 +20,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 f9edf178c..ace6d363b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,8 +32,13 @@ //! //! println!("AST: {:?}", ast); //! ``` + +#![cfg_attr(not(feature = "std"), no_std)] #![allow(clippy::upper_case_acronyms)] +#[cfg(not(feature = "std"))] +extern crate alloc; + pub mod ast; #[macro_use] pub mod dialect; diff --git a/src/parser.rs b/src/parser.rs index b31297509..3a125de45 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -12,8 +12,15 @@ //! SQL Parser -use std::error::Error; -use std::fmt; +#[cfg(not(feature = "std"))] +use alloc::{ + boxed::Box, + format, + string::{String, ToString}, + vec, + vec::Vec, +}; +use core::fmt; use log::debug; @@ -81,7 +88,8 @@ impl fmt::Display for ParserError { } } -impl Error for ParserError {} +#[cfg(feature = "std")] +impl std::error::Error for ParserError {} pub struct Parser<'a> { tokens: Vec, diff --git a/src/test_utils.rs b/src/test_utils.rs index 6e76fef48..27eba1408 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -16,7 +16,15 @@ // // Integration tests (i.e. everything under `tests/`) import this // via `tests/test_utils/mod.rs`. -use std::fmt::Debug; + +#[cfg(not(feature = "std"))] +use alloc::{ + boxed::Box, + string::{String, ToString}, + vec, + vec::Vec, +}; +use core::fmt::Debug; use crate::ast::*; use crate::dialect::*; diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 6b5c0b410..9656f936f 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -16,9 +16,17 @@ //! //! The tokens then form the input for the parser, which outputs an Abstract Syntax Tree (AST). -use std::fmt; -use std::iter::Peekable; -use std::str::Chars; +#[cfg(not(feature = "std"))] +use alloc::{ + borrow::ToOwned, + format, + string::{String, ToString}, + vec, + vec::Vec, +}; +use core::fmt; +use core::iter::Peekable; +use core::str::Chars; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; From 67be482277adc86bad143db2c6bde1ea8b8410a3 Mon Sep 17 00:00:00 2001 From: koushiro Date: Fri, 20 Aug 2021 15:32:38 +0800 Subject: [PATCH 2/2] Fix typo Signed-off-by: koushiro --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 44880aafd..07bb67bef 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -43,7 +43,7 @@ jobs: with: targets: 'thumbv6m-none-eabi' - uses: actions/checkout@master - - run: cargo check --no-defatult-features --target thumbv6m-none-eabi + - run: cargo check --no-default-features --target thumbv6m-none-eabi test: strategy: