Skip to content

Commit 5bc109a

Browse files
authored
Support no_std (apache#332)
* Support `no_std` Signed-off-by: koushiro <[email protected]> * Fix typo Signed-off-by: koushiro <[email protected]>
1 parent 293133f commit 5bc109a

File tree

13 files changed

+71
-14
lines changed

13 files changed

+71
-14
lines changed

Diff for: .github/workflows/rust.yml

+10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ jobs:
3535
- uses: actions/checkout@master
3636
- run: cargo check --all-targets --all-features
3737

38+
compile-no-std:
39+
runs-on: ubuntu-latest
40+
steps:
41+
- name: Set up Rust
42+
uses: hecrj/setup-rust-action@v1
43+
with:
44+
targets: 'thumbv6m-none-eabi'
45+
- uses: actions/checkout@master
46+
- run: cargo check --no-default-features --target thumbv6m-none-eabi
47+
3848
test:
3949
strategy:
4050
matrix:

Diff for: Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ include = [
1313
"Cargo.toml",
1414
]
1515
edition = "2018"
16+
resolver = "2"
1617

1718
[lib]
1819
name = "sqlparser"
1920
path = "src/lib.rs"
2021

2122
[features]
23+
default = ["std"]
24+
std = []
2225
# Enable JSON output in the `cli` example:
2326
json_example = ["serde_json", "serde"]
2427

Diff for: src/ast/data_type.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
// See the License for the specific language governing permissions and
1111
// limitations under the License.
1212

13-
use std::fmt;
13+
#[cfg(not(feature = "std"))]
14+
use alloc::boxed::Box;
15+
use core::fmt;
1416

1517
#[cfg(feature = "serde")]
1618
use serde::{Deserialize, Serialize};

Diff for: src/ast/ddl.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
//! AST types specific to CREATE/ALTER variants of [Statement]
1414
//! (commonly referred to as Data Definition Language, or DDL)
1515
16-
use std::fmt;
16+
#[cfg(not(feature = "std"))]
17+
use alloc::{boxed::Box, string::ToString, vec::Vec};
18+
use core::fmt;
1719

1820
#[cfg(feature = "serde")]
1921
use serde::{Deserialize, Serialize};

Diff for: src/ast/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ mod operator;
1818
mod query;
1919
mod value;
2020

21-
use std::fmt;
21+
#[cfg(not(feature = "std"))]
22+
use alloc::{
23+
boxed::Box,
24+
string::{String, ToString},
25+
vec::Vec,
26+
};
27+
use core::fmt;
2228

2329
#[cfg(feature = "serde")]
2430
use serde::{Deserialize, Serialize};

Diff for: src/ast/operator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// See the License for the specific language governing permissions and
1111
// limitations under the License.
1212

13-
use std::fmt;
13+
use core::fmt;
1414

1515
#[cfg(feature = "serde")]
1616
use serde::{Deserialize, Serialize};

Diff for: src/ast/query.rs

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
// See the License for the specific language governing permissions and
1111
// limitations under the License.
1212

13+
#[cfg(not(feature = "std"))]
14+
use alloc::{boxed::Box, vec::Vec};
15+
1316
#[cfg(feature = "serde")]
1417
use serde::{Deserialize, Serialize};
1518

Diff for: src/ast/value.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
// See the License for the specific language governing permissions and
1111
// limitations under the License.
1212

13-
use std::fmt;
13+
#[cfg(not(feature = "std"))]
14+
use alloc::string::String;
15+
use core::fmt;
1416

1517
#[cfg(feature = "bigdecimal")]
1618
use bigdecimal::BigDecimal;

Diff for: src/dialect/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ mod postgresql;
2020
mod snowflake;
2121
mod sqlite;
2222

23-
use std::any::{Any, TypeId};
24-
use std::fmt::Debug;
23+
use core::any::{Any, TypeId};
24+
use core::fmt::Debug;
2525

2626
pub use self::ansi::AnsiDialect;
2727
pub use self::generic::GenericDialect;

Diff for: src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@
3232
//!
3333
//! println!("AST: {:?}", ast);
3434
//! ```
35+
36+
#![cfg_attr(not(feature = "std"), no_std)]
3537
#![allow(clippy::upper_case_acronyms)]
3638

39+
#[cfg(not(feature = "std"))]
40+
extern crate alloc;
41+
3742
pub mod ast;
3843
#[macro_use]
3944
pub mod dialect;

Diff for: src/parser.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@
1212

1313
//! SQL Parser
1414
15-
use std::error::Error;
16-
use std::fmt;
15+
#[cfg(not(feature = "std"))]
16+
use alloc::{
17+
boxed::Box,
18+
format,
19+
string::{String, ToString},
20+
vec,
21+
vec::Vec,
22+
};
23+
use core::fmt;
1724

1825
use log::debug;
1926

@@ -81,7 +88,8 @@ impl fmt::Display for ParserError {
8188
}
8289
}
8390

84-
impl Error for ParserError {}
91+
#[cfg(feature = "std")]
92+
impl std::error::Error for ParserError {}
8593

8694
pub struct Parser<'a> {
8795
tokens: Vec<Token>,

Diff for: src/test_utils.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@
1616
//
1717
// Integration tests (i.e. everything under `tests/`) import this
1818
// via `tests/test_utils/mod.rs`.
19-
use std::fmt::Debug;
19+
20+
#[cfg(not(feature = "std"))]
21+
use alloc::{
22+
boxed::Box,
23+
string::{String, ToString},
24+
vec,
25+
vec::Vec,
26+
};
27+
use core::fmt::Debug;
2028

2129
use crate::ast::*;
2230
use crate::dialect::*;

Diff for: src/tokenizer.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@
1616
//!
1717
//! The tokens then form the input for the parser, which outputs an Abstract Syntax Tree (AST).
1818
19-
use std::fmt;
20-
use std::iter::Peekable;
21-
use std::str::Chars;
19+
#[cfg(not(feature = "std"))]
20+
use alloc::{
21+
borrow::ToOwned,
22+
format,
23+
string::{String, ToString},
24+
vec,
25+
vec::Vec,
26+
};
27+
use core::fmt;
28+
use core::iter::Peekable;
29+
use core::str::Chars;
2230

2331
#[cfg(feature = "serde")]
2432
use serde::{Deserialize, Serialize};

0 commit comments

Comments
 (0)