Skip to content

Commit 1067f99

Browse files
committed
Add Serde Support For AST
1 parent 1285c6d commit 1067f99

File tree

9 files changed

+37
-22
lines changed

9 files changed

+37
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/Cargo.lock
22
/.vagga
33
/target
4+
/.idea

Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ version = "0.4.0"
1414
authors = ["Paul Colomiets <[email protected]>"]
1515
edition = "2018"
1616

17-
[features]
18-
serde = ["dep:serde"]
19-
2017
[dependencies]
2118
combine = "4.6.6"
2219
serde = { version = "1.0.163", features = ["derive"], optional = true }

src/common.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use std::{collections::BTreeMap, fmt};
33
use combine::easy::{Error, Info};
44
use combine::{choice, many, many1, optional, position, StdParseResult};
55
use combine::{parser, Parser};
6+
#[cfg(feature = "serde")]
7+
use serde::Serialize;
68

79
use crate::helpers::{ident, kind, name, punct};
810
use crate::position::Pos;
@@ -11,6 +13,20 @@ use crate::tokenizer::{Kind as T, Token, TokenStream};
1113
/// Text abstracts over types that hold a string value.
1214
/// It is used to make the AST generic over the string type.
1315
pub trait Text<'a>: 'a {
16+
#[cfg(feature = "serde")]
17+
type Value: 'a
18+
+ From<&'a str>
19+
+ AsRef<str>
20+
+ std::borrow::Borrow<str>
21+
+ PartialEq
22+
+ Eq
23+
+ PartialOrd
24+
+ Ord
25+
+ fmt::Debug
26+
+ Clone
27+
+ Serialize;
28+
29+
#[cfg(not(feature = "serde"))]
1430
type Value: 'a
1531
+ From<&'a str>
1632
+ AsRef<str>

src/format.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::default::Default;
33

44
use crate::common::Directive;
55

6-
76
#[derive(Debug, PartialEq)]
87
pub(crate) struct Formatter<'a> {
98
buf: String,
@@ -171,13 +170,16 @@ impl<'a> Formatter<'a> {
171170
}
172171

173172
fn dec_indent(&mut self) {
174-
self.indent = self.indent.checked_sub(self.style.indent)
173+
self.indent = self
174+
.indent
175+
.checked_sub(self.style.indent)
175176
.expect("negative indent");
176177
}
177178
}
178179

179-
pub(crate) fn format_directives<'a, T>(dirs: &[Directive<'a, T>], f: &mut Formatter)
180-
where T: crate::common::Text<'a>,
180+
pub(crate) fn format_directives<'a, T>(dirs: &[Directive<'a, T>], f: &mut Formatter)
181+
where
182+
T: crate::common::Text<'a>,
181183
{
182184
for dir in dirs {
183185
f.write(" ");
@@ -198,7 +200,7 @@ macro_rules! impl_display {
198200

199201
('a $($typ: ident, )+) => {
200202
$(
201-
impl<'a, T> fmt::Display for $typ<'a, T>
203+
impl<'a, T> fmt::Display for $typ<'a, T>
202204
where T: Text<'a>,
203205
{
204206
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,21 @@
9191
//!
9292
#![warn(missing_debug_implementations)]
9393

94-
#[cfg(test)] #[macro_use] extern crate pretty_assertions;
95-
94+
#[cfg(test)]
95+
#[macro_use]
96+
extern crate pretty_assertions;
9697

9798
mod common;
9899
#[macro_use]
99100
mod format;
100-
mod position;
101-
mod tokenizer;
102101
mod helpers;
102+
mod position;
103103
pub mod query;
104104
pub mod schema;
105+
mod tokenizer;
105106

107+
pub use crate::format::Style;
108+
pub use crate::position::Pos;
109+
pub use crate::query::minify_query;
106110
pub use crate::query::parse_query;
107111
pub use crate::schema::parse_schema;
108-
pub use crate::query::minify_query;
109-
pub use crate::position::Pos;
110-
pub use crate::format::Style;

src/query/error.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use combine::easy::Errors;
22
use thiserror::Error;
33

4-
use crate::tokenizer::Token;
54
use crate::position::Pos;
5+
use crate::tokenizer::Token;
66

77
pub type InternalError<'a> = Errors<Token<'a>, Token<'a>, Pos>;
88

9-
109
/// Error parsing query
1110
///
1211
/// This structure is opaque for forward compatibility. We are exploring a

src/query/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod format;
66
mod grammar;
77
mod minify;
88

9-
pub use self::grammar::{parse_query, consume_definition};
10-
pub use self::error::ParseError;
119
pub use self::ast::*;
10+
pub use self::error::ParseError;
11+
pub use self::grammar::{consume_definition, parse_query};
1212
pub use self::minify::minify_query;

src/schema/error.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use combine::easy::Errors;
22
use thiserror::Error;
33

4-
use crate::tokenizer::Token;
54
use crate::position::Pos;
5+
use crate::tokenizer::Token;
66

77
pub type InternalError<'a> = Errors<Token<'a>, Token<'a>, Pos>;
88

9-
109
/// Error parsing schema
1110
///
1211
/// This structure is opaque for forward compatibility. We are exploring a

src/schema/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! Schema definition language AST and utility
22
//!
33
mod ast;
4-
mod grammar;
54
mod error;
65
mod format;
6+
mod grammar;
77

88
pub use self::ast::*;
99
pub use self::error::ParseError;

0 commit comments

Comments
 (0)