|
| 1 | +use crate::pretty_print::pretty_impl; |
| 2 | +use crate::schema::SchemaItem; |
| 3 | +use crate::stability::{Stability, StabilityStore}; |
| 4 | +use crate::store::{Store, StoreItem}; |
| 5 | +use crate::url::UrlStore; |
| 6 | +use crate::visitor::{Visitor, walk_item}; |
| 7 | +use anyhow::{Error, bail}; |
| 8 | +use rustdoc_json_types::{ItemEnum, Visibility}; |
| 9 | + |
| 10 | +pub(crate) struct ConvertToSchema<'a> { |
| 11 | + store: &'a Store, |
| 12 | + stability: &'a StabilityStore, |
| 13 | + urls: &'a UrlStore, |
| 14 | + |
| 15 | + name_stack: Vec<String>, |
| 16 | +} |
| 17 | + |
| 18 | +impl<'a> Visitor<'a> for ConvertToSchema<'a> { |
| 19 | + type Result = Vec<SchemaItem>; |
| 20 | + |
| 21 | + fn visit_item(&mut self, item: &StoreItem<'a>) -> Result<Vec<SchemaItem>, Error> { |
| 22 | + if item.visibility != expected_visibility(item) { |
| 23 | + return Ok(Vec::new()); |
| 24 | + } |
| 25 | + |
| 26 | + match (self.stability.get(item.crate_id, item.id), &item.inner) { |
| 27 | + (Some(Stability::Stable), _) => {} |
| 28 | + (Some(Stability::Unstable), _) => return Ok(Vec::new()), |
| 29 | + |
| 30 | + // `impl` blocks are not required to have stability attributes, so we should not error |
| 31 | + // out if they are missing them. |
| 32 | + (None, ItemEnum::Impl(_)) => {} |
| 33 | + |
| 34 | + (None, _) => bail!( |
| 35 | + "stability attribute is missing for {} {:?}", |
| 36 | + self.name_stack.join("::"), |
| 37 | + item.id |
| 38 | + ), |
| 39 | + } |
| 40 | + |
| 41 | + let mut pop_name = false; |
| 42 | + if let Some(name) = &item.name { |
| 43 | + self.name_stack.push(name.clone()); |
| 44 | + pop_name = true; |
| 45 | + } |
| 46 | + |
| 47 | + let result = match &item.inner { |
| 48 | + ItemEnum::AssocConst { .. } |
| 49 | + | ItemEnum::AssocType { .. } |
| 50 | + | ItemEnum::Constant { .. } |
| 51 | + | ItemEnum::Enum(_) |
| 52 | + | ItemEnum::Macro(_) |
| 53 | + | ItemEnum::Module(_) |
| 54 | + | ItemEnum::Static(_) |
| 55 | + | ItemEnum::Struct(_) |
| 56 | + | ItemEnum::StructField(_) |
| 57 | + | ItemEnum::Trait(_) |
| 58 | + | ItemEnum::TypeAlias(_) |
| 59 | + | ItemEnum::Union(_) |
| 60 | + | ItemEnum::Variant(_) |
| 61 | + | ItemEnum::ProcMacro(_) |
| 62 | + | ItemEnum::Function(_) => self.include(item), |
| 63 | + |
| 64 | + ItemEnum::Primitive(p) => { |
| 65 | + // We don't want primitives to have the `std::` prefix. |
| 66 | + let old_stack = std::mem::replace(&mut self.name_stack, vec![p.name.clone()]); |
| 67 | + let result = self.include(item); |
| 68 | + self.name_stack = old_stack; |
| 69 | + result |
| 70 | + } |
| 71 | + |
| 72 | + ItemEnum::Use(_) => walk_item(self, item), |
| 73 | + |
| 74 | + ItemEnum::Impl(impl_) => { |
| 75 | + if impl_.trait_.is_some() { |
| 76 | + Ok(vec![SchemaItem { |
| 77 | + name: pretty_impl(impl_), |
| 78 | + deprecated: item.deprecation.is_some(), |
| 79 | + url: None, // TODO: is there an URL we can put here? |
| 80 | + |
| 81 | + // We are intentionally not walking inside impls of traits: we don't want |
| 82 | + // all types in the standard library to show up in the changelog if a new |
| 83 | + // item is added in a trait. |
| 84 | + children: Vec::new(), |
| 85 | + }]) |
| 86 | + } else { |
| 87 | + walk_item(self, item) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + ItemEnum::TraitAlias(_) | ItemEnum::ExternType | ItemEnum::ExternCrate { .. } => { |
| 92 | + Ok(Vec::new()) |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + if pop_name { |
| 97 | + self.name_stack.pop(); |
| 98 | + } |
| 99 | + result |
| 100 | + } |
| 101 | + |
| 102 | + fn store(&self) -> &'a Store { |
| 103 | + self.store |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +impl<'a> ConvertToSchema<'a> { |
| 108 | + pub(crate) fn new(store: &'a Store, stability: &'a StabilityStore, urls: &'a UrlStore) -> Self { |
| 109 | + Self { store, stability, urls, name_stack: Vec::new() } |
| 110 | + } |
| 111 | + |
| 112 | + fn include(&mut self, item: &StoreItem<'a>) -> Result<Vec<SchemaItem>, Error> { |
| 113 | + let item = SchemaItem { |
| 114 | + name: self.name_stack.join("::"), |
| 115 | + url: Some(self.urls.for_item(item)?.into()), |
| 116 | + deprecated: item.deprecation.is_some(), |
| 117 | + children: walk_item(self, item)?, |
| 118 | + }; |
| 119 | + Ok(vec![item]) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +/// Some items don't have a visibility associated to them, and are instead public by default. This |
| 124 | +/// function determines what visibility a public item must have. |
| 125 | +fn expected_visibility(item: &StoreItem<'_>) -> Visibility { |
| 126 | + match &item.inner { |
| 127 | + ItemEnum::AssocType { .. } |
| 128 | + | ItemEnum::AssocConst { .. } |
| 129 | + | ItemEnum::Variant(_) |
| 130 | + | ItemEnum::Impl(_) => Visibility::Default, |
| 131 | + |
| 132 | + ItemEnum::Module(_) |
| 133 | + | ItemEnum::ExternCrate { .. } |
| 134 | + | ItemEnum::Use(_) |
| 135 | + | ItemEnum::Union(_) |
| 136 | + | ItemEnum::Struct(_) |
| 137 | + | ItemEnum::StructField(_) |
| 138 | + | ItemEnum::Enum(_) |
| 139 | + | ItemEnum::Function(_) |
| 140 | + | ItemEnum::Trait(_) |
| 141 | + | ItemEnum::TraitAlias(_) |
| 142 | + | ItemEnum::TypeAlias(_) |
| 143 | + | ItemEnum::Constant { .. } |
| 144 | + | ItemEnum::Static(_) |
| 145 | + | ItemEnum::ExternType |
| 146 | + | ItemEnum::Macro(_) |
| 147 | + | ItemEnum::ProcMacro(_) |
| 148 | + | ItemEnum::Primitive(_) => Visibility::Public, |
| 149 | + } |
| 150 | +} |
0 commit comments