Skip to content

Commit

Permalink
Add nix support (#54)
Browse files Browse the repository at this point in the history
* Add nix support

Co-authored-by: John-John Tedro <[email protected]>
  • Loading branch information
whs-dot-hk and udoprog committed Jan 28, 2024
1 parent 6df1ba1 commit c11d11a
Show file tree
Hide file tree
Showing 9 changed files with 442 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
rust: ["1.58", stable, nightly]
rust: ['1.66', stable, nightly]
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/weekly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
fail-fast: false
matrix:
rust: ["1.58", stable]
rust: ['1.66', stable]
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "genco"
version = "0.17.8"
authors = ["John-John Tedro <[email protected]>"]
edition = "2018"
rust-version = "1.58"
rust-version = "1.66"
description = "A whitespace-aware quasiquoter for beautiful code generation."
documentation = "https://docs.rs/genco"
readme = "README.md"
Expand Down
34 changes: 34 additions & 0 deletions examples/nix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use genco::fmt;
use genco::prelude::*;

fn main() -> anyhow::Result<()> {
let nixpkgs = &nix::inherit("inputs", "nixpkgs");
let pkgs = &nix::variable(
"pkgs",
quote! {
import $nixpkgs {
inherit ($nixpkgs) system;
config.allowUnfree = true;
overlays = [];
}
},
);
let mk_default = &nix::with("lib", "mkDefault");

let tokens = quote! {
{
imports = [];
environment.systemPackages = with $pkgs; [];
networking.useDHCP = $mk_default true;
}
};

let stdout = std::io::stdout();
let mut w = fmt::IoWriter::new(stdout.lock());

let fmt = fmt::Config::from_lang::<Nix>();
let config = nix::Config::default();

tokens.format_file(&mut w.as_formatter(&fmt), &config)?;
Ok(())
}
2 changes: 1 addition & 1 deletion genco-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "genco-macros"
version = "0.17.8"
authors = ["John-John Tedro <[email protected]>"]
edition = "2018"
rust-version = "1.58"
rust-version = "1.66"
description = """
A whitespace-aware quasiquoter for beautiful code generation.
"""
Expand Down
2 changes: 1 addition & 1 deletion src/fmt/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ where
where
P: Parse<L>,
{
if let Some(item) = self.items.get(0) {
if let Some(item) = self.items.first() {
P::peek(item)
} else {
false
Expand Down
22 changes: 11 additions & 11 deletions src/fmt/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ static TABS: &str =
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";

#[derive(Debug, Clone, Copy)]
enum Line {
enum Whitespace {
Initial,
None,
Push,
Line,
}

impl Line {
impl Whitespace {
/// Convert into an indentation level.
///
/// If we return `None`, no indentation nor lines should be written since we
Expand All @@ -35,7 +35,7 @@ impl Line {
}
}

impl Default for Line {
impl Default for Whitespace {
fn default() -> Self {
Self::None
}
Expand All @@ -50,7 +50,7 @@ pub struct Formatter<'a> {
/// How many lines we want to add to the output stream.
///
/// This will only be realized if we push non-whitespace.
line: Line,
line: Whitespace,
/// How many spaces we want to add to the output stream.
///
/// This will only be realized if we push non-whitespace, and will be reset
Expand All @@ -65,7 +65,7 @@ impl<'a> Formatter<'a> {
pub(crate) fn new(write: &'a mut (dyn fmt::Write + 'a), config: &'a Config) -> Formatter<'a> {
Formatter {
write,
line: Line::Initial,
line: Whitespace::Initial,
spaces: 0usize,
indent: 0i16,
config,
Expand All @@ -90,7 +90,7 @@ impl<'a> Formatter<'a> {
///
/// This will also reset any whitespace we have pending.
pub(crate) fn write_trailing_line(&mut self) -> fmt::Result {
self.line = Line::default();
self.line = Whitespace::default();
self.spaces = 0;
self.write.write_trailing_line(self.config)?;
Ok(())
Expand All @@ -108,9 +108,9 @@ impl<'a> Formatter<'a> {

fn push(&mut self) {
self.line = match self.line {
Line::Initial => return,
Line::Line => return,
_ => Line::Push,
Whitespace::Initial => return,
Whitespace::Line => return,
_ => Whitespace::Push,
};

self.spaces = 0;
Expand All @@ -119,8 +119,8 @@ impl<'a> Formatter<'a> {
/// Push a new line.
fn line(&mut self) {
self.line = match self.line {
Line::Initial => return,
_ => Line::Line,
Whitespace::Initial => return,
_ => Whitespace::Line,
};

self.spaces = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/lang/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod dart;
pub mod go;
pub mod java;
pub mod js;
pub mod nix;
pub mod python;
pub mod rust;
pub mod swift;
Expand All @@ -33,6 +34,7 @@ pub use self::dart::Dart;
pub use self::go::Go;
pub use self::java::Java;
pub use self::js::JavaScript;
pub use self::nix::Nix;
pub use self::python::Python;
pub use self::rust::Rust;
pub use self::swift::Swift;
Expand Down
Loading

0 comments on commit c11d11a

Please sign in to comment.