| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use std::cmp::Ordering; |
| 6 |
use std::collections::HashSet; |
| 7 |
|
| 8 |
use crate::bindgen::ir::{ItemContainer, Path}; |
| 9 |
|
| 10 |
/// A dependency list is used for gathering what order to output the types. |
| 11 |
#[derive(Default)] |
| 12 |
pub struct Dependencies { |
| 13 |
pub order: Vec<ItemContainer>, |
| 14 |
pub items: HashSet<Path>, |
| 15 |
} |
| 16 |
|
| 17 |
impl Dependencies { |
| 18 |
pub fn new() -> Dependencies { |
| 19 |
Dependencies { |
| 20 |
order: Vec::new(), |
| 21 |
items: HashSet::new(), |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
pub fn sort(&mut self) { |
| 26 |
// Sort untagged enums and opaque structs into their own layers because they don't |
| 27 |
// depend on each other or anything else. |
| 28 |
let ordering = |a: &ItemContainer, b: &ItemContainer| match (a, b) { |
| 29 |
(ItemContainer::Enum(x), ItemContainer::Enum(y)) |
| 30 |
if x.tag.is_none() && y.tag.is_none() => |
| 31 |
{ |
| 32 |
x.path.cmp(&y.path) |
| 33 |
} |
| 34 |
(ItemContainer::Enum(x), _) if x.tag.is_none() => Ordering::Less, |
| 35 |
(_, ItemContainer::Enum(x)) if x.tag.is_none() => Ordering::Greater, |
| 36 |
|
| 37 |
(ItemContainer::OpaqueItem(x), ItemContainer::OpaqueItem(y)) => x.path.cmp(&y.path), |
| 38 |
(&ItemContainer::OpaqueItem(_), _) => Ordering::Less, |
| 39 |
(_, &ItemContainer::OpaqueItem(_)) => Ordering::Greater, |
| 40 |
|
| 41 |
_ => Ordering::Equal, |
| 42 |
}; |
| 43 |
|
| 44 |
self.order.sort_by(ordering); |
| 45 |
} |
| 46 |
} |