Skip to content
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

Add quote integration as optional printing feature #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "litrs"
version = "0.4.0"
version = "0.4.1"
Copy link
Owner

Choose a reason for hiding this comment

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

I will bump the version as part of my release process. At least in my workflow, this doesn't belong into feature commits.

authors = ["Lukas Kalbertodt <[email protected]>"]
edition = "2018"
rust-version = "1.54"
Expand All @@ -25,9 +25,11 @@ exclude = [".github"]


[features]
default = ["proc-macro2"]
default = ["proc-macro2", "printing"]
Copy link
Owner

Choose a reason for hiding this comment

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

Don't make it a default feature. In fact, I consider removing proc-macro2 from the default features as well.

check_suffix = ["unicode-xid"]
printing = ["dep:quote"]
Copy link
Owner

Choose a reason for hiding this comment

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

Mh this feature requires Rust 1.60, but currently this is targetting 1.54. So if we want this, I would only release that with a major version bump. While I dislike implicit features and do think dep: is a step in the right direction, I think this isn't worth it. So please change this to just ["quote"].

Also, independently: I guess this should also mention "proc-macro2" as that's required to implement the printing feature.


[dependencies]
proc-macro2 = { version = "1", optional = true }
proc-macro2 = { version = "1", optional = true }
unicode-xid = { version = "0.2.4", optional = true }
quote = { version = "1", optional = true, no-default-features = true }
Comment on lines -32 to +35
Copy link
Owner

Choose a reason for hiding this comment

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

Please don't align it like that. Just one space after the comma. and before the =.

7 changes: 4 additions & 3 deletions src/bool/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::fmt;

use crate::{ParseError, err::{perr, ParseErrorKind::*}};

use crate::{
err::{perr, ParseErrorKind::*},
ParseError,
};

/// A bool literal: `true` or `false`. Also see [the reference][ref].
///
Expand Down Expand Up @@ -50,6 +52,5 @@ impl fmt::Display for BoolLit {
}
}


#[cfg(test)]
mod tests;
13 changes: 6 additions & 7 deletions src/bool/tests.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use crate::{
Literal, BoolLit,
test_util::assert_parse_ok_eq,
};
use crate::{test_util::assert_parse_ok_eq, BoolLit, Literal};

macro_rules! assert_bool_parse {
($input:literal, $expected:expr) => {
assert_parse_ok_eq(
$input, Literal::parse($input), Literal::Bool($expected), "Literal::parse");
$input,
Literal::parse($input),
Literal::Bool($expected),
"Literal::parse",
);
assert_parse_ok_eq($input, BoolLit::parse($input), $expected, "BoolLit::parse");
};
}



#[test]
fn parse_ok() {
assert_bool_parse!("false", BoolLit::False);
Expand Down
14 changes: 9 additions & 5 deletions src/byte/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use core::fmt;

use crate::{
Buffer, ParseError,
err::{perr, ParseErrorKind::*},
escape::unescape,
parse::check_suffix,
Buffer, ParseError,
};


/// A (single) byte literal, e.g. `b'k'` or `b'!'`.
///
/// See [the reference][ref] for more information.
Expand All @@ -33,7 +32,11 @@ impl<B: Buffer> ByteLit<B> {
}

let (value, start_suffix) = parse_impl(&input)?;
Ok(Self { raw: input, value, start_suffix })
Ok(Self {
raw: input,
value,
start_suffix,
})
}

/// Returns the byte value that this literal represents.
Expand All @@ -55,7 +58,6 @@ impl<B: Buffer> ByteLit<B> {
pub fn into_raw_input(self) -> B {
self.raw
}

}

impl ByteLit<&str> {
Expand All @@ -80,7 +82,9 @@ impl<B: Buffer> fmt::Display for ByteLit<B> {
#[inline(never)]
pub(crate) fn parse_impl(input: &str) -> Result<(u8, usize), ParseError> {
let input_bytes = input.as_bytes();
let first = input_bytes.get(2).ok_or(perr(None, UnterminatedByteLiteral))?;
let first = input_bytes
.get(2)
.ok_or(perr(None, UnterminatedByteLiteral))?;
let (c, len) = match first {
b'\'' if input_bytes.get(3) == Some(&b'\'') => return Err(perr(2, UnescapedSingleQuote)),
b'\'' => return Err(perr(None, EmptyByteLiteral)),
Expand Down
24 changes: 19 additions & 5 deletions src/byte/tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use crate::{ByteLit, Literal, test_util::{assert_parse_ok_eq, assert_roundtrip}};
use crate::{
test_util::{assert_parse_ok_eq, assert_roundtrip},
ByteLit, Literal,
};

// ===== Utility functions =======================================================================

macro_rules! check {
($lit:literal) => { check!($lit, stringify!($lit), "") };
($lit:literal) => {
check!($lit, stringify!($lit), "")
};
($lit:literal, $input:expr, $suffix:literal) => {
let input = $input;
let expected = ByteLit {
Expand All @@ -12,16 +17,25 @@ macro_rules! check {
value: $lit,
};

assert_parse_ok_eq(input, ByteLit::parse(input), expected.clone(), "ByteLit::parse");
assert_parse_ok_eq(input, Literal::parse(input), Literal::Byte(expected), "Literal::parse");
assert_parse_ok_eq(
input,
ByteLit::parse(input),
expected.clone(),
"ByteLit::parse",
);
assert_parse_ok_eq(
input,
Literal::parse(input),
Literal::Byte(expected),
"Literal::parse",
);
let lit = ByteLit::parse(input).unwrap();
assert_eq!(lit.value(), $lit);
assert_eq!(lit.suffix(), $suffix);
assert_roundtrip(expected.to_owned(), input);
};
}


// ===== Actual tests ============================================================================

#[test]
Expand Down
19 changes: 13 additions & 6 deletions src/bytestr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::{fmt, ops::Range};

use crate::{
Buffer, ParseError,
err::{perr, ParseErrorKind::*},
escape::{scan_raw_string, unescape_string},
Buffer, ParseError,
};


/// A byte string or raw byte string literal, e.g. `b"hello"` or `br#"abc"def"#`.
///
/// See [the reference][ref] for more information.
Expand Down Expand Up @@ -41,13 +40,20 @@ impl<B: Buffer> ByteStringLit<B> {
}

let (value, num_hashes, start_suffix) = parse_impl(&input)?;
Ok(Self { raw: input, value, num_hashes, start_suffix })
Ok(Self {
raw: input,
value,
num_hashes,
start_suffix,
})
}

/// Returns the string value this literal represents (where all escapes have
/// been turned into their respective values).
pub fn value(&self) -> &[u8] {
self.value.as_deref().unwrap_or(&self.raw.as_bytes()[self.inner_range()])
self.value
.as_deref()
.unwrap_or(&self.raw.as_bytes()[self.inner_range()])
}

/// Like `value` but returns a potentially owned version of the value.
Expand All @@ -57,7 +63,9 @@ impl<B: Buffer> ByteStringLit<B> {
pub fn into_value(self) -> B::ByteCow {
let inner_range = self.inner_range();
let Self { raw, value, .. } = self;
value.map(B::ByteCow::from).unwrap_or_else(|| raw.cut(inner_range).into_byte_cow())
value
.map(B::ByteCow::from)
.unwrap_or_else(|| raw.cut(inner_range).into_byte_cow())
}

/// The optional suffix. Returns `""` if the suffix is empty/does not exist.
Expand Down Expand Up @@ -109,7 +117,6 @@ impl<B: Buffer> fmt::Display for ByteStringLit<B> {
}
}


/// Precondition: input has to start with either `b"` or `br`.
#[inline(never)]
fn parse_impl(input: &str) -> Result<(Option<Vec<u8>>, Option<u32>, usize), ParseError> {
Expand Down
Loading