Skip to content

Commit 132fe86

Browse files
feat(filter): Add support for no_std environments
Co-authored-by: WolverinDEV <git@did.science>
1 parent 4feafa4 commit 132fe86

7 files changed

Lines changed: 25 additions & 13 deletions

File tree

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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ pre-release-replacements = [
2626
]
2727

2828
[features]
29-
default = ["regex"]
29+
default = ["std", "regex"]
3030
regex = ["dep:regex"]
31+
std = ["log/std", "regex?/std"]
3132

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

3637
[dev-dependencies]
3738
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: 5 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,6 +108,7 @@ impl Builder {
108108
} = parse_spec(filters);
109109

110110
for error in errors {
111+
#[cfg(feature = "std")]
111112
eprintln!("warning: {error}, ignoring it");
112113
}
113114

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: 6 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,13 @@ 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+
impl std::error::Error for ParseError {}
5557

5658
/// Parse a logging specification string (e.g: `crate1,crate2::mod3,crate3::x=error/foo`)
5759
/// and return a vector with log directives.

0 commit comments

Comments
 (0)