Skip to content

Add support for no_std + alloc #286

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
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 @@ -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)]
Expand Down
4 changes: 3 additions & 1 deletion src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
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,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::{
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,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)]
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,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)]
Expand Down
4 changes: 2 additions & 2 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 11 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -79,6 +88,7 @@ impl fmt::Display for ParserError {
}
}

#[cfg(feature = "std")]
impl Error for ParserError {}

pub struct Parser<'a> {
Expand Down
9 changes: 8 additions & 1 deletion src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
19 changes: 16 additions & 3 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down