Skip to content

Commit

Permalink
nit: Add std and alloc features and organize imports
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Nov 13, 2024
1 parent 3590881 commit 7c5fd09
Show file tree
Hide file tree
Showing 21 changed files with 109 additions and 50 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ license = "MIT OR Apache-2.0"
keywords = ["code-generation", "template"]
categories = ["template-engine"]

[features]
default = ["std", "alloc"]
std = []
alloc = []

[dependencies]
genco-macros = { path = "./genco-macros", version = "=0.17.9" }

Expand Down
4 changes: 2 additions & 2 deletions genco-macros/src/fake.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::cell::{RefCell, RefMut};
use std::fmt::Arguments;
use core::cell::{RefCell, RefMut};
use core::fmt::Arguments;

use proc_macro2::Span;

Expand Down
4 changes: 2 additions & 2 deletions genco-macros/src/string_parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Helper to parse quoted strings.
use std::cell::{Cell, RefCell};
use std::fmt::Write;
use core::cell::{Cell, RefCell};
use core::fmt::Write;

use proc_macro2::{Span, TokenStream, TokenTree};
use syn::parse::{ParseBuffer, ParseStream};
Expand Down
6 changes: 4 additions & 2 deletions src/fmt/formatter.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use core::mem;

use alloc::string::String;

use crate::fmt;
use crate::fmt::config::{Config, Indentation};
use crate::fmt::cursor;
use crate::lang::Lang;
use crate::tokens::Item;

use std::mem;

/// Buffer used as indentation source.
static SPACES: &str = " ";

Expand Down
3 changes: 2 additions & 1 deletion src/fmt/io_writer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::fmt;
use std::io;

use crate::fmt;

/// Helper struct to format a token stream to an underlying writer implementing
/// [io::Write][std::io::Write].
///
Expand Down
3 changes: 3 additions & 0 deletions src/fmt/vec_writer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use alloc::string::String;
use alloc::vec::Vec;

use crate::fmt;

/// Helper struct to format a token stream as a vector of strings.
Expand Down
9 changes: 7 additions & 2 deletions src/lang/csharp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@
mod block_comment;
mod comment;

use core::fmt::Write as _;

use alloc::collections::BTreeSet;
use alloc::string::{String, ToString};

use std::collections::{HashMap, HashSet};

use crate as genco;
use crate::fmt;
use crate::quote_in;
use crate::tokens::ItemStr;
use std::collections::{BTreeSet, HashMap, HashSet};
use std::fmt::Write as _;

pub use self::block_comment::BlockComment;
pub use self::comment::Comment;
Expand Down
10 changes: 7 additions & 3 deletions src/lang/java/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@
//! ```
mod block_comment;

pub use self::block_comment::BlockComment;

use core::fmt::Write as _;

use alloc::collections::BTreeSet;
use alloc::string::{String, ToString};

use std::collections::HashMap;

use crate as genco;
use crate::fmt;
use crate::tokens::ItemStr;
use crate::{quote, quote_in};
use std::collections::{BTreeSet, HashMap};
use std::fmt::Write as _;

/// Tokens container specialized for Java.
pub type Tokens = crate::Tokens<Java>;
Expand Down
12 changes: 6 additions & 6 deletions src/lang/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@
//! # }
//! ```
use core::fmt::Write as _;

use alloc::collections::{BTreeMap, BTreeSet};
use alloc::string::String;

use crate::fmt;
use crate::tokens::ItemStr;

use relative_path::{RelativePath, RelativePathBuf};
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Write as _;

/// Tokens container specialization for Rust.
pub type Tokens = crate::Tokens<JavaScript>;
Expand All @@ -74,8 +78,6 @@ impl_lang! {
_format: &Self::Format,
has_eval: bool,
) -> fmt::Result {
use std::fmt::Write as _;

if has_eval {
out.write_char('`')?;
} else {
Expand All @@ -92,8 +94,6 @@ impl_lang! {
_format: &Self::Format,
has_eval: bool,
) -> fmt::Result {
use std::fmt::Write as _;

if has_eval {
out.write_char('`')?;
} else {
Expand Down
8 changes: 2 additions & 6 deletions src/lang/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub use self::python::Python;
pub use self::rust::Rust;
pub use self::swift::Swift;

use core::fmt::Write as _;

use crate::fmt;
use crate::Tokens;

Expand Down Expand Up @@ -69,7 +71,6 @@ where
_format: &Self::Format,
_has_eval: bool,
) -> fmt::Result {
use std::fmt::Write as _;
out.write_char('"')?;
Ok(())
}
Expand All @@ -81,7 +82,6 @@ where
_format: &Self::Format,
_has_eval: bool,
) -> fmt::Result {
use std::fmt::Write as _;
out.write_char('"')?;
Ok(())
}
Expand All @@ -93,8 +93,6 @@ where
format: &Self::Format,
literal: &str,
) -> fmt::Result {
use std::fmt::Write as _;

Self::start_string_eval(out, config, format)?;
out.write_str(literal)?;
Self::end_string_eval(out, config, format)?;
Expand All @@ -121,8 +119,6 @@ where

/// Performing string quoting according to language convention.
fn write_quoted(out: &mut fmt::Formatter<'_>, input: &str) -> fmt::Result {
use std::fmt::Write as _;

out.write_str(input)
}

Expand Down
8 changes: 6 additions & 2 deletions src/lang/nix.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
//! Nix
use core::fmt::Write as _;

use alloc::collections::BTreeSet;
use alloc::string::ToString;

use crate as genco;
use crate::fmt;
use crate::quote_in;
use crate::tokens::ItemStr;
use std::collections::BTreeSet;
use std::fmt::Write as _;

/// Tokens
pub type Tokens = crate::Tokens<Nix>;
Expand Down
7 changes: 5 additions & 2 deletions src/lang/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
//! # }
//! ```
use core::fmt::Write as _;

use alloc::collections::{BTreeMap, BTreeSet};
use alloc::vec::Vec;

use crate as genco;
use crate::fmt;
use crate::tokens::ItemStr;
use crate::{quote, quote_in};
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Write as _;

/// Tokens container specialization for Python.
pub type Tokens = crate::Tokens<Python>;
Expand Down
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@
#![deny(missing_docs)]
#![deny(rustdoc::broken_intra_doc_links)]
#![allow(clippy::needless_doctest_main)]
#![no_std]

#[cfg(feature = "std")]
extern crate std;

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(not(feature = "alloc"))]
compile_error!("genco: The `alloc` feature must be enabled");

/// Whitespace sensitive quasi-quoting.
///
Expand Down
8 changes: 4 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,25 +161,25 @@ macro_rules! impl_lang {
$(
impl $crate::tokens::FormatInto<$lang> for $ty {
fn format_into(self, tokens: &mut $crate::Tokens<$lang>) {
tokens.append($crate::tokens::__lang_item::<$lang>(Box::new(self.into())));
tokens.append($crate::tokens::__lang_item::<$lang>(self.into()));
}
}

impl<'a> $crate::tokens::FormatInto<$lang> for &'a $ty {
fn format_into(self, tokens: &mut $crate::Tokens<$lang>) {
tokens.append($crate::tokens::__lang_item::<$lang>(Box::new(self.clone().into())));
tokens.append($crate::tokens::__lang_item::<$lang>(self.clone().into()));
}
}

impl $crate::tokens::Register<$lang> for $ty {
fn register(self, tokens: &mut $crate::Tokens<$lang>) {
tokens.append($crate::tokens::__lang_item_register::<$lang>(Box::new(self.into())));
tokens.append($crate::tokens::__lang_item_register::<$lang>(self.into()));
}
}

impl<'a> $crate::tokens::Register<$lang> for &'a $ty {
fn register(self, tokens: &mut $crate::Tokens<$lang>) {
tokens.append($crate::tokens::__lang_item_register::<$lang>(Box::new(self.clone().into())));
tokens.append($crate::tokens::__lang_item_register::<$lang>(self.clone().into()));
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/tokens/display.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use core::fmt;

use alloc::string::ToString;

use crate::lang::Lang;
use crate::tokens::{FormatInto, Item};
use crate::Tokens;
use std::fmt;

/// Function to build a string literal.
///
Expand Down
9 changes: 6 additions & 3 deletions src/tokens/format_into.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::borrow::Cow;
use std::fmt::Arguments;
use std::rc::Rc;
use core::fmt::Arguments;

use alloc::borrow::Cow;
use alloc::rc::Rc;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

use crate::lang::Lang;
use crate::tokens::{Item, ItemStr, Tokens};
Expand Down
2 changes: 2 additions & 0 deletions src/tokens/from_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::Tokens;
/// let _: Tokens<Rust> = quote!($c $['\n'] $c);
/// # Ok::<_, genco::fmt::Error>(())
/// ```
#[inline]
pub fn from_fn<F, L>(f: F) -> FromFn<F>
where
F: FnOnce(&mut Tokens<L>),
Expand All @@ -44,6 +45,7 @@ where
L: Lang,
F: FnOnce(&mut Tokens<L>),
{
#[inline]
fn format_into(self, tokens: &mut Tokens<L>) {
(self.f)(tokens);
}
Expand Down
14 changes: 10 additions & 4 deletions src/tokens/internal.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::boxed::Box;

use crate::lang::Lang;
use crate::tokens::{from_fn, FormatInto};

Expand All @@ -6,12 +8,14 @@ use crate::tokens::{from_fn, FormatInto};
/// This must only be used by the [impl_lang!] macro.
///
/// [impl_lang!]: crate::impl_lang!
pub fn __lang_item<L>(item: Box<L::Item>) -> impl FormatInto<L>
#[doc(hidden)]
#[inline]
pub fn __lang_item<L>(item: L::Item) -> impl FormatInto<L>
where
L: Lang,
{
from_fn(|t| {
t.lang_item(item);
t.lang_item(Box::new(item));
})
}

Expand All @@ -20,11 +24,13 @@ where
/// This must only be used by the [impl_lang!] macro.
///
/// [impl_lang!]: crate::impl_lang!
pub fn __lang_item_register<L>(item: Box<L::Item>) -> impl FormatInto<L>
#[doc(hidden)]
#[inline]
pub fn __lang_item_register<L>(item: L::Item) -> impl FormatInto<L>
where
L: Lang,
{
from_fn(|t| {
t.lang_item_register(item);
t.lang_item_register(Box::new(item));
})
}
2 changes: 2 additions & 0 deletions src/tokens/item.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! A single element
use alloc::boxed::Box;

use crate::lang::Lang;
use crate::tokens::{FormatInto, ItemStr, Tokens};

Expand Down
12 changes: 8 additions & 4 deletions src/tokens/item_str.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
//! Helper trait to take ownership of strings.
use core::fmt;
use core::ops::Deref;

use alloc::borrow::{Cow, ToOwned};
use alloc::boxed::Box;
use alloc::rc::Rc;
use alloc::string::String;

use crate::lang::Lang;
use crate::tokens::{FormatInto, Item, Tokens};
use std::borrow::Cow;
use std::fmt;
use std::ops::Deref;
use std::rc::Rc;

/// A managed string that permits immutable borrowing.
#[derive(Debug, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)]
Expand Down
Loading

0 comments on commit 7c5fd09

Please sign in to comment.