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 35838db
Show file tree
Hide file tree
Showing 30 changed files with 169 additions and 100 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
with:
toolchain: ${{matrix.rust}}
- run: cargo build --workspace
- run: cargo build --workspace --no-default-features --features alloc
- run: cargo build --workspace --all-targets
if: matrix.rust == 'stable'
- run: cargo test --workspace --doc
Expand Down
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: 3 additions & 3 deletions src/fmt/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ where
fn parse(item: &Item<L>) -> fmt::Result<&Self::Output> {
match item {
Item::Literal(s) => Ok(s),
_ => Err(std::fmt::Error),
_ => Err(core::fmt::Error),
}
}
}
Expand All @@ -57,7 +57,7 @@ where
fn parse(item: &Item<L>) -> fmt::Result<&Self::Output> {
match item {
Item::CloseEval => Ok(&()),
_ => Err(std::fmt::Error),
_ => Err(core::fmt::Error),
}
}
}
Expand Down Expand Up @@ -114,7 +114,7 @@ where
where
P: Parse<L>,
{
let item = self.next().ok_or(std::fmt::Error)?;
let item = self.next().ok_or(core::fmt::Error)?;
P::parse(item)
}
}
14 changes: 7 additions & 7 deletions src/fmt/fmt_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ use crate::fmt;
/// ```
pub struct FmtWriter<W>
where
W: std::fmt::Write,
W: core::fmt::Write,
{
writer: W,
}

impl<W> FmtWriter<W>
where
W: std::fmt::Write,
W: core::fmt::Write,
{
/// Construct a new line writer from the underlying writer.
pub fn new(writer: W) -> Self {
Expand All @@ -59,24 +59,24 @@ where
}
}

impl<W> std::fmt::Write for FmtWriter<W>
impl<W> core::fmt::Write for FmtWriter<W>
where
W: std::fmt::Write,
W: core::fmt::Write,
{
#[inline(always)]
fn write_char(&mut self, c: char) -> std::fmt::Result {
fn write_char(&mut self, c: char) -> core::fmt::Result {
self.writer.write_char(c)
}

#[inline(always)]
fn write_str(&mut self, s: &str) -> std::fmt::Result {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
self.writer.write_str(s)
}
}

impl<W> fmt::Write for FmtWriter<W>
where
W: std::fmt::Write,
W: core::fmt::Write,
{
#[inline(always)]
fn write_line(&mut self, config: &fmt::Config) -> fmt::Result {
Expand Down
14 changes: 8 additions & 6 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 Expand Up @@ -231,7 +233,7 @@ impl<'a> Formatter<'a> {
}
_ => {
// Anything else is an illegal state for formatting.
return Err(std::fmt::Error);
return Err(core::fmt::Error);
}
}
}
Expand Down Expand Up @@ -305,7 +307,7 @@ impl<'a> Formatter<'a> {
}
}

impl std::fmt::Write for Formatter<'_> {
impl core::fmt::Write for Formatter<'_> {
fn write_str(&mut self, s: &str) -> fmt::Result {
if !s.is_empty() {
Formatter::write_str(self, s)?;
Expand All @@ -315,8 +317,8 @@ impl std::fmt::Write for Formatter<'_> {
}
}

impl std::fmt::Debug for Formatter<'_> {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Debug for Formatter<'_> {
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
fmt.debug_struct("Formatter")
.field("line", &self.line)
.field("spaces", &self.spaces)
Expand Down
15 changes: 8 additions & 7 deletions 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 Expand Up @@ -60,22 +61,22 @@ where
}
}

impl<W> std::fmt::Write for IoWriter<W>
impl<W> core::fmt::Write for IoWriter<W>
where
W: io::Write,
{
#[inline(always)]
fn write_char(&mut self, c: char) -> std::fmt::Result {
fn write_char(&mut self, c: char) -> core::fmt::Result {
self.writer
.write_all(c.encode_utf8(&mut [0; 4]).as_bytes())
.map_err(|_| std::fmt::Error)
.map_err(|_| core::fmt::Error)
}

#[inline(always)]
fn write_str(&mut self, s: &str) -> std::fmt::Result {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
self.writer
.write_all(s.as_bytes())
.map_err(|_| std::fmt::Error)
.map_err(|_| core::fmt::Error)
}
}

Expand All @@ -87,6 +88,6 @@ where
fn write_line(&mut self, config: &fmt::Config) -> fmt::Result {
self.writer
.write_all(config.newline.as_bytes())
.map_err(|_| std::fmt::Error)
.map_err(|_| core::fmt::Error)
}
}
9 changes: 6 additions & 3 deletions src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,29 @@ mod config;
mod cursor;
mod fmt_writer;
mod formatter;
#[cfg(feature = "std")]
mod io_writer;
mod vec_writer;

pub use self::config::{Config, Indentation};
pub use self::fmt_writer::FmtWriter;
pub use self::formatter::Formatter;
#[cfg(feature = "std")]
pub use self::io_writer::IoWriter;
pub use self::vec_writer::VecWriter;

/// Result type for the `fmt` module.
pub type Result<T = ()> = std::result::Result<T, std::fmt::Error>;
pub type Result<T = ()> = core::result::Result<T, core::fmt::Error>;
/// Error for the `fmt` module.
pub type Error = std::fmt::Error;
pub type Error = core::fmt::Error;

/// Trait that defines a line writer.
pub(crate) trait Write: std::fmt::Write {
pub(crate) trait Write: core::fmt::Write {
/// Implement for writing a line.
fn write_line(&mut self, config: &Config) -> Result;

/// Implement for writing the trailing line ending of the file.
#[inline]
fn write_trailing_line(&mut self, config: &Config) -> Result {
self.write_line(config)
}
Expand Down
9 changes: 6 additions & 3 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 Expand Up @@ -61,14 +64,14 @@ impl VecWriter {
}
}

impl std::fmt::Write for VecWriter {
impl core::fmt::Write for VecWriter {
#[inline(always)]
fn write_char(&mut self, c: char) -> std::fmt::Result {
fn write_char(&mut self, c: char) -> core::fmt::Result {
self.line_buffer.write_char(c)
}

#[inline(always)]
fn write_str(&mut self, s: &str) -> std::fmt::Result {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
self.line_buffer.write_str(s)
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/lang/c.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! Specialization for C code generation.
use core::fmt::Write as _;

use alloc::collections::BTreeSet;

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

/// Tokens container specialization for C.
pub type Tokens = crate::Tokens<C>;
Expand Down
13 changes: 8 additions & 5 deletions src/lang/csharp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
mod block_comment;
mod comment;

use core::fmt::Write as _;

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

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 Expand Up @@ -119,7 +122,7 @@ pub struct Format {
/// their use has to be qualified or not.
///
/// A missing name means that it has to be used in a qualified manner.
imported_names: HashMap<String, String>,
imported_names: BTreeMap<String, String>,
}

/// Config data for Csharp formatting.
Expand Down Expand Up @@ -170,7 +173,7 @@ impl Csharp {
out: &mut Tokens,
tokens: &Tokens,
config: &Config,
imported_names: &mut HashMap<String, String>,
imported_names: &mut BTreeMap<String, String>,
) {
let mut modules = BTreeSet::new();

Expand All @@ -182,7 +185,7 @@ impl Csharp {
return;
}

let mut imported = HashSet::new();
let mut imported = BTreeSet::new();

for (namespace, name) in modules {
if Some(namespace) == config.namespace.as_deref() {
Expand Down
8 changes: 4 additions & 4 deletions src/lang/dart/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@
//! ```
mod doc_comment;

pub use self::doc_comment::DocComment;

use core::fmt::Write as _;

use alloc::collections::BTreeSet;

use crate as genco;
use crate::fmt;
use crate::quote_in;
use crate::tokens::{quoted, ItemStr};
use std::fmt::Write as _;

const SEP: &str = ".";
/// dart:core package.
Expand Down Expand Up @@ -188,8 +190,6 @@ impl Import {
impl Dart {
/// Resolve all imports.
fn imports(out: &mut Tokens, input: &Tokens, _: &Config) {
use std::collections::BTreeSet;

let mut modules = BTreeSet::new();

for import in input.walk_imports() {
Expand Down
6 changes: 4 additions & 2 deletions src/lang/go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@
//! # }
//! ```
use core::fmt::Write as _;

use alloc::collections::BTreeSet;

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

const MODULE_SEP: &str = "/";
const SEP: &str = ".";
Expand Down
Loading

0 comments on commit 35838db

Please sign in to comment.