macros: Remove dependency to once_cell

Use OnceLock from std instead.
This commit is contained in:
Kévin Commaille 2024-11-27 18:01:14 +01:00 committed by strawberry
parent d82e2a02d5
commit f2b58e5e07
2 changed files with 29 additions and 27 deletions

View File

@ -22,7 +22,6 @@ __internal_macro_expand = ["syn/visit-mut"]
[dependencies] [dependencies]
cfg-if = "1.0.0" cfg-if = "1.0.0"
once_cell = "1.13.0"
proc-macro-crate = "3.1.0" proc-macro-crate = "3.1.0"
proc-macro2 = "1.0.24" proc-macro2 = "1.0.24"
quote = "1.0.8" quote = "1.0.8"

View File

@ -1,8 +1,7 @@
//! Methods and types for generating API endpoints. //! Methods and types for generating API endpoints.
use std::{env, fs, path::Path}; use std::{env, fs, path::Path, sync::OnceLock};
use once_cell::sync::Lazy;
use proc_macro2::Span; use proc_macro2::Span;
use serde::{de::IgnoredAny, Deserialize}; use serde::{de::IgnoredAny, Deserialize};
@ -29,37 +28,41 @@ fn ensure_feature_presence() -> Option<&'static syn::Error> {
server: Option<IgnoredAny>, server: Option<IgnoredAny>,
} }
static RESULT: Lazy<Result<(), syn::Error>> = Lazy::new(|| { static RESULT: OnceLock<Result<(), syn::Error>> = OnceLock::new();
let manifest_dir = env::var("CARGO_MANIFEST_DIR")
.map_err(|_| syn::Error::new(Span::call_site(), "Failed to read CARGO_MANIFEST_DIR"))?;
let manifest_file = Path::new(&manifest_dir).join("Cargo.toml"); RESULT
let manifest_bytes = fs::read_to_string(manifest_file) .get_or_init(|| {
.map_err(|_| syn::Error::new(Span::call_site(), "Failed to read Cargo.toml"))?; let manifest_dir = env::var("CARGO_MANIFEST_DIR").map_err(|_| {
syn::Error::new(Span::call_site(), "Failed to read CARGO_MANIFEST_DIR")
})?;
let manifest_parsed: CargoToml = toml::from_str(&manifest_bytes) let manifest_file = Path::new(&manifest_dir).join("Cargo.toml");
.map_err(|_| syn::Error::new(Span::call_site(), "Failed to parse Cargo.toml"))?; let manifest_bytes = fs::read_to_string(manifest_file)
.map_err(|_| syn::Error::new(Span::call_site(), "Failed to read Cargo.toml"))?;
if manifest_parsed.features.client.is_none() { let manifest_parsed: CargoToml = toml::from_str(&manifest_bytes)
return Err(syn::Error::new( .map_err(|_| syn::Error::new(Span::call_site(), "Failed to parse Cargo.toml"))?;
Span::call_site(),
"This crate doesn't define a `client` feature in its `Cargo.toml`.\n\ if manifest_parsed.features.client.is_none() {
return Err(syn::Error::new(
Span::call_site(),
"This crate doesn't define a `client` feature in its `Cargo.toml`.\n\
Please add a `client` feature such that generated `OutgoingRequest` and \ Please add a `client` feature such that generated `OutgoingRequest` and \
`IncomingResponse` implementations can be enabled.", `IncomingResponse` implementations can be enabled.",
)); ));
} }
if manifest_parsed.features.server.is_none() { if manifest_parsed.features.server.is_none() {
return Err(syn::Error::new( return Err(syn::Error::new(
Span::call_site(), Span::call_site(),
"This crate doesn't define a `server` feature in its `Cargo.toml`.\n\ "This crate doesn't define a `server` feature in its `Cargo.toml`.\n\
Please add a `server` feature such that generated `IncomingRequest` and \ Please add a `server` feature such that generated `IncomingRequest` and \
`OutgoingResponse` implementations can be enabled.", `OutgoingResponse` implementations can be enabled.",
)); ));
} }
Ok(()) Ok(())
}); })
.as_ref()
RESULT.as_ref().err() .err()
} }