Skip to content

Commit

Permalink
Refactor dependency and imports calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed May 19, 2024
1 parent 7a1e21d commit 85f1318
Show file tree
Hide file tree
Showing 91 changed files with 716 additions and 648 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 9 additions & 11 deletions crates/header-translator/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use heck::ToTrainCase;
use semver::Version;
use serde::{de, Deserialize, Deserializer};

use crate::id::{Location, LocationLibrary};
use crate::id::Location;
use crate::stmt::{Derives, Mutability};
use crate::ItemIdentifier;

Expand All @@ -21,16 +21,14 @@ pub struct Config {
impl Config {
pub fn library(&self, location: impl AsRef<Location>) -> &LibraryConfig {
let location = location.as_ref();
match location.library() {
LocationLibrary::System => &self.system,
LocationLibrary::Library(library) => self.libraries.get(library).unwrap_or_else(|| {
let library_name = location.library_name();
if library_name == "System" || library_name == "objc2" {
&self.system
} else {
self.libraries.get(library_name).unwrap_or_else(|| {
error!("tried to get library config from {location:?}");
&self.system
}),
_ => {
error!("tried to get library config from {location:?}");
&self.system
}
})
}
}

Expand Down Expand Up @@ -310,8 +308,8 @@ impl<'de> Deserialize<'de> for Mutability {
let (library, rest) = value.split_once("::")?;
let (file_name, name) = rest.split_once("::")?;
Some(ItemIdentifier::from_raw(
name.to_string(),
vec![library.to_string(), file_name.to_string()],
name.into(),
vec![library.to_string().into(), file_name.to_string().into()],
))
}

Expand Down
16 changes: 10 additions & 6 deletions crates/header-translator/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::ops;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -30,7 +31,7 @@ impl<'a> Context<'a> {
if let Some(file) = location.get_file_location().file {
let path = file.get_path();
if let Ok(path) = path.strip_prefix(&self.framework_dir) {
let mut components: Vec<_> = path
let mut components: Vec<Cow<'_, str>> = path
.components()
.filter(|component| {
component.as_os_str() != "Headers"
Expand All @@ -39,14 +40,14 @@ impl<'a> Context<'a> {
.map(|component| component.as_os_str().to_str().expect("component to_str"))
.map(|component| component.strip_suffix(".framework").unwrap_or(component))
.map(|component| component.strip_suffix(".h").unwrap_or(component))
.map(|s| s.to_string())
.map(|s| s.to_string().into())
.collect();

// Put items in umbrella header in `mod.rs`
if let [.., innermost_framework_name, file_name] = &*components {
let umbrella_header = self
.libraries
.get(innermost_framework_name)
.get(&**innermost_framework_name)
.and_then(|lib| lib.umbrella_header.as_deref())
.unwrap_or(innermost_framework_name);

Expand All @@ -57,11 +58,14 @@ impl<'a> Context<'a> {

return Some(Location::from_components(components));
} else if let Ok(path) = path.strip_prefix(&self.include_dir) {
if path.starts_with("objc") || path == Path::new("MacTypes.h") {
return Some(Location::from_components(vec!["System".to_string()]));
if path.starts_with("objc") {
return Some(Location::from_components(vec!["objc2".into()]));
}
if path == Path::new("MacTypes.h") {
return Some(Location::from_components(vec!["System".into()]));
}
if path.starts_with("sys") {
return Some(Location::from_components(vec!["libc".to_string()]));
return Some(Location::from_components(vec!["libc".into()]));
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions crates/header-translator/src/default_cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ license = "MIT"
workspace = true

[dependencies]
objc2 = { path = "../../crates/objc2", version = "0.5.1", default-features = false }

[package.metadata.docs.rs]
default-target = "UNSET"
Expand All @@ -33,5 +32,5 @@ targets = [
default = ["std"]

# Currently not possible to turn off, put here for forwards compatibility.
std = ["alloc", "objc2/std"]
alloc = ["objc2/alloc"]
std = ["alloc"]
alloc = []
6 changes: 3 additions & 3 deletions crates/header-translator/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ impl Expr {
Self::Signed(_) => {}
Self::Unsigned(_) => {}
Self::Float(_) => {}
Self::MacroInvocation { evaluated, .. } => {
Self::MacroInvocation { evaluated, id } => {
if evaluated.is_none() {
// features.add_item(&id);
items.push(id.clone());
}
}
Self::Enum { id, .. } => {
Expand All @@ -214,7 +214,7 @@ impl Expr {
Self::Const(id) => {
items.push(id.clone());
}
Self::Var { id, ty, .. } => {
Self::Var { id, ty } => {
items.push(id.clone());
items.extend(ty.required_items());
}
Expand Down
Loading

0 comments on commit 85f1318

Please sign in to comment.