diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b575b92..89bb398 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -72,7 +72,9 @@ jobs: - uses: Swatinem/rust-cache@v2 - uses: taiki-e/install-action@cargo-hack - name: Default features - run: cargo hack check --each-feature --locked --rust-version --ignore-private --workspace --all-targets --keep-going + run: | + cargo hack check --each-feature --locked --rust-version --ignore-private --package env_logger --all-targets --keep-going + cargo hack check --each-feature --locked --version-range 1.81..=1.81 --ignore-private --package env_filter --all-targets --keep-going minimal-versions: name: Minimal versions strategy: diff --git a/Cargo.toml b/Cargo.toml index 76b92e7..ea46b6d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -135,7 +135,7 @@ unstable-kv = ["kv"] [dependencies] log = { version = "0.4.29", features = ["std"] } -env_filter = { version = "1.0.0", path = "crates/env_filter", default-features = false } +env_filter = { version = "1.0.0", path = "crates/env_filter", default-features = false, features = ["std"] } jiff = { version = "0.2.22", default-features = false, features = ["std"], optional = true } anstream = { version = "1.0.0", default-features = false, features = ["wincon"], optional = true } anstyle = { version = "1.0.13", optional = true } diff --git a/crates/env_filter/Cargo.toml b/crates/env_filter/Cargo.toml index 54c1af4..54d5780 100644 --- a/crates/env_filter/Cargo.toml +++ b/crates/env_filter/Cargo.toml @@ -9,7 +9,6 @@ keywords = ["logging", "log", "logger"] repository.workspace = true license.workspace = true edition.workspace = true -rust-version.workspace = true include.workspace = true [package.metadata.docs.rs] @@ -26,12 +25,13 @@ pre-release-replacements = [ ] [features] -default = ["regex"] +default = ["std", "regex"] regex = ["dep:regex"] +std = ["regex/std"] [dependencies] -log = { version = "0.4.29", features = ["std"] } -regex = { version = "1.12.3", optional = true, default-features=false, features=["std", "perf"] } +log = { version = "0.4.29", default-features = false } +regex = { version = "1.12.3", optional = true, default-features=false, features=["perf"] } [dev-dependencies] snapbox = "1.0" diff --git a/crates/env_filter/src/directive.rs b/crates/env_filter/src/directive.rs index c24fef2..77c12ed 100644 --- a/crates/env_filter/src/directive.rs +++ b/crates/env_filter/src/directive.rs @@ -1,3 +1,5 @@ +use alloc::string::String; + use log::Level; use log::LevelFilter; diff --git a/crates/env_filter/src/filter.rs b/crates/env_filter/src/filter.rs index 60bcf7a..be94c65 100644 --- a/crates/env_filter/src/filter.rs +++ b/crates/env_filter/src/filter.rs @@ -1,6 +1,5 @@ -use std::env; -use std::fmt; -use std::mem; +use alloc::{borrow::ToOwned, string::ToString, vec::Vec}; +use core::{fmt, mem}; use log::{LevelFilter, Metadata, Record}; @@ -48,10 +47,11 @@ impl Builder { } /// Initializes the filter builder from an environment. + #[cfg(feature = "std")] pub fn from_env(env: &str) -> Builder { let mut builder = Builder::new(); - if let Ok(s) = env::var(env) { + if let Ok(s) = std::env::var(env) { builder.parse(&s); } @@ -108,7 +108,10 @@ impl Builder { } = parse_spec(filters); for error in errors { + #[cfg(feature = "std")] eprintln!("warning: {error}, ignoring it"); + #[cfg(not(feature = "std"))] + log::warn!("{error}, ignoring it"); } self.filter = filter; @@ -258,6 +261,8 @@ impl fmt::Debug for Filter { #[cfg(test)] mod tests { + use alloc::{borrow::ToOwned, vec, vec::Vec}; + use log::{Level, LevelFilter}; use snapbox::{assert_data_eq, str}; diff --git a/crates/env_filter/src/lib.rs b/crates/env_filter/src/lib.rs index 0b35088..9e7dffc 100644 --- a/crates/env_filter/src/lib.rs +++ b/crates/env_filter/src/lib.rs @@ -38,9 +38,14 @@ //! ``` #![cfg_attr(docsrs, feature(doc_cfg))] +#![cfg_attr(all(not(feature = "std"), not(test)), no_std)] #![warn(missing_docs)] #![warn(clippy::print_stderr)] #![warn(clippy::print_stdout)] +#![warn(clippy::std_instead_of_core)] +#![warn(clippy::std_instead_of_alloc)] + +extern crate alloc; mod directive; mod filter; diff --git a/crates/env_filter/src/op.rs b/crates/env_filter/src/op.rs index bfc6a5c..e805edf 100644 --- a/crates/env_filter/src/op.rs +++ b/crates/env_filter/src/op.rs @@ -1,4 +1,5 @@ -use std::fmt; +use alloc::string::{String, ToString}; +use core::fmt; #[derive(Debug, Clone)] pub(crate) struct FilterOp { @@ -24,13 +25,13 @@ impl FilterOp { #[cfg(not(feature = "regex"))] impl FilterOp { - pub fn new(spec: &str) -> Result { + pub(crate) fn new(spec: &str) -> Result { Ok(Self { inner: spec.to_string(), }) } - pub fn is_match(&self, s: &str) -> bool { + pub(crate) fn is_match(&self, s: &str) -> bool { s.contains(&self.inner) } } diff --git a/crates/env_filter/src/parser.rs b/crates/env_filter/src/parser.rs index 097058d..e016dbd 100644 --- a/crates/env_filter/src/parser.rs +++ b/crates/env_filter/src/parser.rs @@ -1,6 +1,7 @@ +use alloc::{borrow::ToOwned, format, string::String, vec::Vec}; +use core::fmt::{Display, Formatter}; + use log::LevelFilter; -use std::error::Error; -use std::fmt::{Display, Formatter}; use crate::Directive; use crate::FilterOp; @@ -46,12 +47,17 @@ pub struct ParseError { } impl Display for ParseError { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { write!(f, "error parsing logger filter: {}", self.details) } } -impl Error for ParseError {} +#[cfg(feature = "std")] +#[allow(clippy::std_instead_of_core)] +impl std::error::Error for ParseError {} + +#[cfg(not(feature = "std"))] +impl core::error::Error for ParseError {} /// Parse a logging specification string (e.g: `crate1,crate2::mod3,crate3::x=error/foo`) /// and return a vector with log directives. @@ -115,6 +121,8 @@ pub(crate) fn parse_spec(spec: &str) -> ParseResult { #[cfg(test)] mod tests { + use alloc::{borrow::ToOwned, string::ToString}; + use crate::ParseError; use log::LevelFilter; use snapbox::{assert_data_eq, str, Data, IntoData};