Skip to content

Commit

Permalink
gccrs: Rename structure to benefit from namespacing
Browse files Browse the repository at this point in the history
Co-authored-by: flip1995 <hello@philkrones.com>
  • Loading branch information
CohenArthur and flip1995 committed Jul 22, 2021
1 parent 7fb8829 commit 0979af6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
36 changes: 18 additions & 18 deletions src/gccrs/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use std::path::{Path, PathBuf};

use getopts::Matches;

use super::{EnvArgs, Error, Result, RustcOptions};
use super::{EnvArgs, Error, Result, RustcArgs};

/// A collection containing multiple instances of `GccrsArgs`. This is necessary in order
/// A collection containing multiple instances of `Args`. This is necessary in order
/// to circumvent the fact that `rustc` can currently generate multiple types of binaries
/// with a single invokation.
///
Expand Down Expand Up @@ -43,38 +43,38 @@ use super::{EnvArgs, Error, Result, RustcOptions};
/// Therefore, we need to wrap an unknown amount of sets of `gccrs` arguments in order to
/// mimic a single `rustc` invokation. Later on, we need to iterate over those sets and
/// spawn a new `gccrs` command for each of them.
pub struct GccrsArgsCollection {
args_set: Vec<GccrsArgs>,
pub struct ArgsCollection {
args_set: Vec<Args>,
}

/// Get the corresponding set of `gccrs` arguments from a single set of `rustc` arguments
impl TryFrom<&[String]> for GccrsArgsCollection {
impl TryFrom<&[String]> for ArgsCollection {
type Error = Error;

fn try_from(rustc_args: &[String]) -> Result<GccrsArgsCollection> {
let matches = RustcOptions::new().parse(rustc_args)?;
fn try_from(rustc_args: &[String]) -> Result<ArgsCollection> {
let matches = RustcArgs::new().parse(rustc_args)?;

let args_set: Result<Vec<GccrsArgs>> = matches
let args_set: Result<Vec<Args>> = matches
.opt_strs("crate-type")
.iter()
.map(|type_str| CrateType::from(type_str.as_str()))
.map(|crate_type| format_output_filename(&matches, crate_type))
.map(|result_tuple| {
result_tuple.map(|(output_file, crate_type)| {
GccrsArgs::new(&matches.free, crate_type, output_file)
Args::new(&matches.free, crate_type, output_file)
})
})
.collect();

Ok(GccrsArgsCollection {
Ok(ArgsCollection {
args_set: args_set?,
})
}
}

/// Implement deref on the collection so we can easily iterate on it
impl Deref for GccrsArgsCollection {
type Target = Vec<GccrsArgs>;
impl Deref for ArgsCollection {
type Target = Vec<Args>;

fn deref(&self) -> &Self::Target {
&self.args_set
Expand Down Expand Up @@ -161,16 +161,16 @@ fn format_output_filename(
}

/// Structure used to represent arguments passed to `gccrs`. Convert them from `rustc`
/// arguments using [`GccrsArgs::from_rustc_arg`]
pub struct GccrsArgs {
/// arguments using [`Args::from_rustc_arg`]
pub struct Args {
source_files: Vec<String>,
crate_type: CrateType,
output_file: PathBuf,
}

impl GccrsArgs {
fn new(source_files: &[String], crate_type: CrateType, output_file: PathBuf) -> GccrsArgs {
GccrsArgs {
impl Args {
fn new(source_files: &[String], crate_type: CrateType, output_file: PathBuf) -> Args {
Args {
source_files: Vec::from(source_files),
crate_type,
output_file,
Expand All @@ -192,7 +192,7 @@ impl GccrsArgs {
self.crate_type
}

/// Create arguments usable when spawning a process from an instance of [`GccrsArgs`]
/// Create arguments usable when spawning a process from an instance of [`Args`]
pub fn as_args(&self) -> Result<Vec<String>> {
// `rustc` generates position independant code
let mut args = vec![String::from("-fPIE"), String::from("-pie")];
Expand Down
2 changes: 1 addition & 1 deletion src/gccrs/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl PartialOrd for DumpedOption {
}
}

/// Sort DumpedOptions based on syntax printing rules. `rustc` prints target options in
/// Sort DumpedArgs based on syntax printing rules. `rustc` prints target options in
/// alphabetical order, before printing OS information
impl Ord for DumpedOption {
fn cmp(&self, other: &Self) -> Ordering {
Expand Down
14 changes: 7 additions & 7 deletions src/gccrs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ mod args;
mod config;
mod env_args;
mod error;
mod rustc_options;
mod rustc_args;

use args::{CrateType, GccrsArgs, GccrsArgsCollection};
use args::{Args, ArgsCollection, CrateType};
use config::GccrsConfig;
use env_args::EnvArgs;
use error::Error;
use rustc_options::RustcOptions;
use rustc_args::RustcArgs;

use std::convert::TryFrom;
use std::process::{Command, ExitStatus, Stdio};
Expand Down Expand Up @@ -92,7 +92,7 @@ impl Gccrs {
}

/// Spawn a `gccrs` command with arguments extracted from a `rustc` invokation
fn compile(gccrs_args: &GccrsArgs) -> Result {
fn compile(gccrs_args: &Args) -> Result {
let exit_status = Gccrs::spawn_with_args(&gccrs_args.as_args()?)?;

match exit_status.success() {
Expand All @@ -103,7 +103,7 @@ impl Gccrs {

/// Execute a callback if necessary, based on the different options used to build
/// the `gccrs` arguments
fn maybe_callback(gccrs_args: &GccrsArgs) -> Result {
fn maybe_callback(gccrs_args: &Args) -> Result {
// If we are ordered to generate a static library, call `ar` after compiling
// the object files
if gccrs_args.crate_type() == CrateType::StaticLib {
Expand All @@ -114,7 +114,7 @@ impl Gccrs {
}

fn translate_and_compile(args: &[String]) -> Result {
let gccrs_args = GccrsArgsCollection::try_from(args)?;
let gccrs_args = ArgsCollection::try_from(args)?;

for arg_set in gccrs_args.iter() {
Gccrs::compile(arg_set)?;
Expand All @@ -124,7 +124,7 @@ impl Gccrs {
Ok(())
}

fn generate_static_lib(args: &GccrsArgs) -> Result {
fn generate_static_lib(args: &Args) -> Result {
let output_file = args
.output_file()
.to_str()
Expand Down
8 changes: 4 additions & 4 deletions src/gccrs/rustc_options.rs → src/gccrs/rustc_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use super::Result;
/// taken from `rustc`'s implementation
use getopts::{Matches, Options};

pub struct RustcOptions {
pub struct RustcArgs {
options: Options,
}

impl RustcOptions {
impl RustcArgs {
/// Generate a new options parser according to `rustc`'s command line options
pub fn new() -> RustcOptions {
pub fn new() -> RustcArgs {
let mut options = Options::new();
options.optopt("", "crate-name", "Name of the crate to compile", "NAME");
options.optopt("", "edition", "Rust edition to use", "YEAR");
Expand All @@ -31,7 +31,7 @@ impl RustcOptions {
);
options.optmulti("", "crate-type", "Type of binary to output", "TYPE");

RustcOptions { options }
RustcArgs { options }
}

pub fn parse(&self, args: &[String]) -> Result<Matches> {
Expand Down

0 comments on commit 0979af6

Please sign in to comment.