Skip to content

Commit afc9c92

Browse files
authored
Merge pull request #2596 from cpmsmith/rust-name-and-type-improvements
Improve names and types in Rust stdlib scraper
2 parents d31e81d + fbb5e61 commit afc9c92

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

lib/docs/filters/rust/entries.rb

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ def get_name
2222
else
2323
at_css('main h1').at_css('button')&.remove
2424
name = at_css('main h1').content.remove(/\A.+\s/).remove('⎘')
25-
mod = slug.split('/').first
25+
path = slug.split('/')
26+
if path.length == 2
27+
# Anything in the standard library but not in a `std::*` module is
28+
# globally available, not `use`d from the `std` crate, so we don't
29+
# prepend `std::` to their name.
30+
return name
31+
end
32+
path.pop if path.last == 'index'
33+
mod = path[0..-2].join('::')
2634
name.prepend("#{mod}::") unless name.start_with?(mod)
2735
name
2836
end
@@ -38,13 +46,12 @@ def get_type
3846
elsif slug.start_with?('error_codes')
3947
'Compiler Errors'
4048
else
41-
path = name.split('::')
42-
heading = at_css('main h1').content.strip
43-
if path.length > 2 || (path.length == 2 && (heading.start_with?('Module') || heading.start_with?('Primitive')))
44-
path[0..1].join('::')
45-
else
46-
path[0]
47-
end
49+
path = slug.split('/')
50+
# Discard the filename, and use the first two path components as the
51+
# type, or one if there is only one. This means anything in a module
52+
# `std::foo` or submodule `std::foo::bar` gets type `std::foo`, and
53+
# things not in modules, e.g. primitive types, get type `std`.
54+
path[0..-2][0..1].join('::')
4855
end
4956
end
5057

0 commit comments

Comments
 (0)