Skip to content

Commit 2a7ae94

Browse files
feat(filter): Add support for no_std environments
Co-authored-by: WolverinDEV <git@did.science>
1 parent 548e34a commit 2a7ae94

8 files changed

Lines changed: 38 additions & 15 deletions

File tree

.github/workflows/ci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ jobs:
7272
- uses: Swatinem/rust-cache@v2
7373
- uses: taiki-e/install-action@cargo-hack
7474
- name: Default features
75-
run: cargo hack check --each-feature --locked --rust-version --ignore-private --workspace --all-targets --keep-going
75+
run: |
76+
cargo hack check --each-feature --locked --rust-version --ignore-private --package env_logger --all-targets --keep-going
77+
cargo hack check --each-feature --locked --version-range 1.81..=1.81 --ignore-private --package env_filter --all-targets --keep-going
7678
minimal-versions:
7779
name: Minimal versions
7880
strategy:

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ unstable-kv = ["kv"]
135135

136136
[dependencies]
137137
log = { version = "0.4.29", features = ["std"] }
138-
env_filter = { version = "1.0.0", path = "crates/env_filter", default-features = false }
138+
env_filter = { version = "1.0.0", path = "crates/env_filter", default-features = false, features = ["std"] }
139139
jiff = { version = "0.2.22", default-features = false, features = ["std"], optional = true }
140140
anstream = { version = "1.0.0", default-features = false, features = ["wincon"], optional = true }
141141
anstyle = { version = "1.0.13", optional = true }

crates/env_filter/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ keywords = ["logging", "log", "logger"]
99
repository.workspace = true
1010
license.workspace = true
1111
edition.workspace = true
12-
rust-version.workspace = true
1312
include.workspace = true
1413

1514
[package.metadata.docs.rs]
@@ -26,12 +25,13 @@ pre-release-replacements = [
2625
]
2726

2827
[features]
29-
default = ["regex"]
28+
default = ["std", "regex"]
3029
regex = ["dep:regex"]
30+
std = ["regex/std"]
3131

3232
[dependencies]
33-
log = { version = "0.4.29", features = ["std"] }
34-
regex = { version = "1.12.3", optional = true, default-features=false, features=["std", "perf"] }
33+
log = { version = "0.4.29", default-features = false }
34+
regex = { version = "1.12.3", optional = true, default-features=false, features=["perf"] }
3535

3636
[dev-dependencies]
3737
snapbox = "1.0"

crates/env_filter/src/directive.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use alloc::string::String;
2+
13
use log::Level;
24
use log::LevelFilter;
35

crates/env_filter/src/filter.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::env;
2-
use std::fmt;
3-
use std::mem;
1+
use alloc::{borrow::ToOwned, string::ToString, vec::Vec};
2+
use core::{fmt, mem};
43

54
use log::{LevelFilter, Metadata, Record};
65

@@ -48,10 +47,11 @@ impl Builder {
4847
}
4948

5049
/// Initializes the filter builder from an environment.
50+
#[cfg(feature = "std")]
5151
pub fn from_env(env: &str) -> Builder {
5252
let mut builder = Builder::new();
5353

54-
if let Ok(s) = env::var(env) {
54+
if let Ok(s) = std::env::var(env) {
5555
builder.parse(&s);
5656
}
5757

@@ -108,7 +108,10 @@ impl Builder {
108108
} = parse_spec(filters);
109109

110110
for error in errors {
111+
#[cfg(feature = "std")]
111112
eprintln!("warning: {error}, ignoring it");
113+
#[cfg(not(feature = "std"))]
114+
log::warn!("{error}, ignoring it");
112115
}
113116

114117
self.filter = filter;
@@ -258,6 +261,8 @@ impl fmt::Debug for Filter {
258261

259262
#[cfg(test)]
260263
mod tests {
264+
use alloc::{borrow::ToOwned, vec, vec::Vec};
265+
261266
use log::{Level, LevelFilter};
262267
use snapbox::{assert_data_eq, str};
263268

crates/env_filter/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,14 @@
3838
//! ```
3939
4040
#![cfg_attr(docsrs, feature(doc_cfg))]
41+
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
4142
#![warn(missing_docs)]
4243
#![warn(clippy::print_stderr)]
4344
#![warn(clippy::print_stdout)]
45+
#![warn(clippy::std_instead_of_core)]
46+
#![warn(clippy::std_instead_of_alloc)]
47+
48+
extern crate alloc;
4449

4550
mod directive;
4651
mod filter;

crates/env_filter/src/op.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::fmt;
1+
use alloc::string::{String, ToString};
2+
use core::fmt;
23

34
#[derive(Debug, Clone)]
45
pub(crate) struct FilterOp {

crates/env_filter/src/parser.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use alloc::{borrow::ToOwned, format, string::String, vec::Vec};
2+
use core::fmt::{Display, Formatter};
3+
14
use log::LevelFilter;
2-
use std::error::Error;
3-
use std::fmt::{Display, Formatter};
45

56
use crate::Directive;
67
use crate::FilterOp;
@@ -46,12 +47,17 @@ pub struct ParseError {
4647
}
4748

4849
impl Display for ParseError {
49-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
50+
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
5051
write!(f, "error parsing logger filter: {}", self.details)
5152
}
5253
}
5354

54-
impl Error for ParseError {}
55+
#[cfg(feature = "std")]
56+
#[allow(clippy::std_instead_of_core)]
57+
impl std::error::Error for ParseError {}
58+
59+
#[cfg(not(feature = "std"))]
60+
impl core::error::Error for ParseError {}
5561

5662
/// Parse a logging specification string (e.g: `crate1,crate2::mod3,crate3::x=error/foo`)
5763
/// and return a vector with log directives.
@@ -115,6 +121,8 @@ pub(crate) fn parse_spec(spec: &str) -> ParseResult {
115121

116122
#[cfg(test)]
117123
mod tests {
124+
use alloc::{borrow::ToOwned, string::ToString};
125+
118126
use crate::ParseError;
119127
use log::LevelFilter;
120128
use snapbox::{assert_data_eq, str, Data, IntoData};

0 commit comments

Comments
 (0)