Skip to content

Commit

Permalink
CI and such (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomcc authored Dec 1, 2024
1 parent 833a88a commit e9e9c02
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 15 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: CI

on:
pull_request:
push: { branches: [main] }

env:
RUSTFLAGS: -Dwarnings

jobs:
test:
name: cargo test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: cargo test --all-features
fmt:
name: cargo fmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: cargo fmt --check
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "envparse"
version = "0.0.1"
version = "0.1.0"
edition = "2021"
authors = ["Thom Chiovoloni <thom@shift.click>"]
readme = "README.md"
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# `envparse`

Quick and dirty crate for parsing values out of an environment var provided at
compile time.
compile time. See also: [docs](https://docs.rs/envparse).

## Usage
Here's an example
```rs
use envparse::parse_env;
const MAX_LEN: usize = parse_env!("MYCRATE_MAX_THING_LEN" as usize else 64);
const MAX_LEN: usize = envparse::parse_env!("MYCRATE_MAX_THING_LEN" as usize else 64);
struct Thing {
len: [u8; MAX_LEN],
}
Expand All @@ -17,19 +16,20 @@ You can bound by ranges too. This one will fail because the
`MUST_BE_USER_PROVIDED` var isn't provided.

```rs
use envparse::parse_env;
const MAX_LEN_LOG2: u32 = envparse::parse_env!("MYCRATE_MAX_LEN_LOG2" as u32 in 0..32);
const MAX_LEN: usize = 1 << MAX_LEN_LOG2;
struct Thing {
len: [u8; MAX_LEN],
}
```

You can also
You can also `try`

```
use envparse::parse_env;
const MAX_LEN_LOG2: u32 = envparse::parse_env!(try "OPTIONAL_MAX_LEN_LOG2" as u32 in 1..32);
```rs
const MAX_LEN_LOG2: u32 = match envparse::parse_env!(try "OPTIONAL_MAX_LEN_LOG2" as u32 in 0..32) {
Some(v) => v,
None => 5,
}
const MAX_LEN: usize = 1 << MAX_LEN_LOG2;
struct Thing {
len: [u8; MAX_LEN],
Expand Down
2 changes: 1 addition & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
version = "Two"
style_edition = "2021"
use_small_heuristics = "Max"
max_width = 120
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub mod __priv {
pub use core;
pub use core::option::Option::{self, None, Some};

pub use crate::privat::{RangeWrap, parse_bounded, parsers};
pub use crate::privat::{parse_bounded, parsers, RangeWrap};
}

/// Parse an environment variable into some value. The main entry-point of this
Expand Down
12 changes: 10 additions & 2 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ pub(crate) const fn number_parse(s: &[u8], skip_sign: bool) -> Result<(u128, boo
},
}
}
if ever_saw_digits { Ok((accum, neg)) } else { Err(ParseError::NoDigits) }
if ever_saw_digits {
Ok((accum, neg))
} else {
Err(ParseError::NoDigits)
}
}

const fn trim_ws(s: &[u8]) -> Option<(usize, usize)> {
Expand All @@ -100,7 +104,11 @@ const fn trim_ws(s: &[u8]) -> Option<(usize, usize)> {
end -= 1;
}
end += 1;
if end <= start { None } else { Some((start, end)) }
if end <= start {
None
} else {
Some((start, end))
}
}

/// Parse a `u128` from a byte slice in const.
Expand Down
2 changes: 1 addition & 1 deletion src/privat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ macro_rules! unwrap_or {
}

pub mod parse_bounded {
use crate::parse::{ParseError::Empty, parse_signed, parse_unsigned};
use crate::parse::{parse_signed, parse_unsigned, ParseError::Empty};

// unsigned
pub const fn usize(
Expand Down
2 changes: 1 addition & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use compiletest_rs::{Config, common::Mode, run_tests};
use compiletest_rs::{common::Mode, run_tests, Config};

fn run_mode(mode: &'static str) {
let root = std::env::var_os("CARGO_MANIFEST_DIR")
Expand Down

0 comments on commit e9e9c02

Please sign in to comment.