Skip to content

Support no_std #332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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-default-features --target thumbv6m-none-eabi

test:
strategy:
matrix:
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ include = [
"Cargo.toml",
]
edition = "2018"
resolver = "2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the change to resolver v2 needed for this change, or is it a nice to have / drive by cleanup?

reference https://doc.rust-lang.org/cargo/reference/resolver.html#feature-resolver-version-2

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it was added to pass CI 'compile-no-std' check

Because the dev-dependency 'simple_logger' introduces the std feature of log, if resolver=2 is not used, libstd will be implicitly introduced in the CI 'compile-no-std'


[lib]
name = "sqlparser"
path = "src/lib.rs"

[features]
default = ["std"]
std = []
# Enable JSON output in the `cli` example:
json_example = ["serde_json", "serde"]

Expand Down
4 changes: 3 additions & 1 deletion src/ast/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
4 changes: 3 additions & 1 deletion src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
8 changes: 7 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion src/ast/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
3 changes: 3 additions & 0 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
4 changes: 3 additions & 1 deletion src/ast/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 11 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Token>,
Expand Down
10 changes: 9 additions & 1 deletion src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
14 changes: 11 additions & 3 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down