#![doc(html_root_url = "https://docs.rs/rustflags/0.1.2")]
#![allow(
clippy::cast_lossless,
clippy::derive_partial_eq_without_eq,
clippy::doc_markdown,
clippy::items_after_statements,
clippy::must_use_candidate,
clippy::too_many_lines,
clippy::type_complexity,
clippy::unnecessary_wraps
)]
mod parse;
mod render;
#[cfg(test)]
mod tests;
mod write;
use std::env;
use std::ffi::OsString;
use std::fmt::{self, Display, Write};
use std::path::PathBuf;
pub fn from_env() -> RustFlags {
let encoded = env::var_os("CARGO_ENCODED_RUSTFLAGS")
.unwrap_or_default()
.into_string()
.unwrap_or_else(|s| s.to_string_lossy().into_owned());
RustFlags {
encoded,
pos: 0,
repeat: None,
short: false,
}
}
pub struct RustFlags {
encoded: String,
pos: usize,
repeat: Option<(fn(&str) -> Option<(Flag, usize)>, usize)>,
short: bool,
}
impl Iterator for RustFlags {
type Item = Flag;
fn next(&mut self) -> Option<Self::Item> {
parse::parse(self)
}
}
#[derive(Debug, PartialEq)]
#[non_exhaustive]
pub enum Flag {
Help,
Cfg { name: String, value: Option<String> },
LibrarySearchPath { kind: LibraryKind, path: PathBuf },
Link {
kind: LinkKind,
modifiers: Vec<(LinkModifierPrefix, LinkModifier)>,
name: String,
rename: Option<String>,
},
CrateType(CrateType),
CrateName(String),
Edition(u16),
Emit(Emit),
Print(String),
Out(PathBuf),
OutDir(PathBuf),
Explain(String),
Test,
Target(String),
Allow(String),
Warn(String),
ForceWarn(String),
Deny(String),
Forbid(String),
CapLints(LintLevel),
Codegen { opt: String, value: Option<String> },
Version,
Verbose,
Extern { name: String, path: Option<PathBuf> },
ExternLocation { name: String, location: OsString },
Sysroot(PathBuf),
Z(String),
ErrorFormat(ErrorFormat),
Json(String),
Color(Color),
RemapPathPrefix { from: PathBuf, to: PathBuf },
}
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[non_exhaustive]
pub enum LibraryKind {
Dependency,
Crate,
Native,
Framework,
All,
}
impl Default for LibraryKind {
fn default() -> Self {
LibraryKind::All
}
}
impl Display for LibraryKind {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(match self {
LibraryKind::Dependency => "dependency",
LibraryKind::Crate => "crate",
LibraryKind::Native => "native",
LibraryKind::Framework => "framework",
LibraryKind::All => "all",
})
}
}
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[non_exhaustive]
pub enum LinkKind {
Static,
Framework,
Dylib,
}
impl Default for LinkKind {
fn default() -> Self {
LinkKind::Dylib
}
}
impl Display for LinkKind {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(match self {
LinkKind::Static => "static",
LinkKind::Framework => "framework",
LinkKind::Dylib => "dylib",
})
}
}
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum LinkModifierPrefix {
Enable,
Disable,
}
impl Display for LinkModifierPrefix {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_char(match self {
LinkModifierPrefix::Enable => '+',
LinkModifierPrefix::Disable => '-',
})
}
}
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[non_exhaustive]
pub enum LinkModifier {
Bundle,
Verbatim,
WholeArchive,
AsNeeded,
}
impl Display for LinkModifier {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(match self {
LinkModifier::Bundle => "bundle",
LinkModifier::Verbatim => "verbatim",
LinkModifier::WholeArchive => "whole-archive",
LinkModifier::AsNeeded => "as-needed",
})
}
}
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[non_exhaustive]
pub enum CrateType {
Bin,
Lib,
Rlib,
Dylib,
Cdylib,
Staticlib,
ProcMacro,
}
impl Display for CrateType {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(match self {
CrateType::Bin => "bin",
CrateType::Lib => "lib",
CrateType::Rlib => "rlib",
CrateType::Dylib => "dylib",
CrateType::Cdylib => "Cdylib",
CrateType::Staticlib => "staticlib",
CrateType::ProcMacro => "proc-macro",
})
}
}
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[non_exhaustive]
pub enum Emit {
Asm,
LlvmBc,
LlvmIr,
Obj,
Metadata,
Link,
DepInfo,
Mir,
}
impl Display for Emit {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(match self {
Emit::Asm => "asm",
Emit::LlvmBc => "llvm-bc",
Emit::LlvmIr => "llvm-ir",
Emit::Obj => "obj",
Emit::Metadata => "metadata",
Emit::Link => "link",
Emit::DepInfo => "dep-info",
Emit::Mir => "mir",
})
}
}
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum LintLevel {
Allow,
Warn,
Deny,
Forbid,
}
impl Display for LintLevel {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(match self {
LintLevel::Allow => "allow",
LintLevel::Warn => "warn",
LintLevel::Deny => "deny",
LintLevel::Forbid => "forbid",
})
}
}
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[non_exhaustive]
pub enum ErrorFormat {
Human,
Json,
Short,
}
impl Display for ErrorFormat {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(match self {
ErrorFormat::Human => "human",
ErrorFormat::Json => "json",
ErrorFormat::Short => "short",
})
}
}
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum Color {
Auto,
Always,
Never,
}
impl Default for Color {
fn default() -> Self {
Color::Auto
}
}
impl Display for Color {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(match self {
Color::Auto => "auto",
Color::Always => "always",
Color::Never => "never",
})
}
}