Skip to content

Commit e17b6e0

Browse files
committed
Add menu item called Feature flags
Feature flags should show all available features for current crate version with default being first is specified.
1 parent 89336f1 commit e17b6e0

File tree

6 files changed

+70
-5
lines changed

6 files changed

+70
-5
lines changed

src/db/add_package.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub(crate) fn add_package_into_database(
4242
let dependencies = convert_dependencies(metadata_pkg);
4343
let rustdoc = get_rustdoc(metadata_pkg, source_dir).unwrap_or(None);
4444
let readme = get_readme(metadata_pkg, source_dir).unwrap_or(None);
45+
let features = get_features(metadata_pkg);
4546
let is_library = metadata_pkg.is_library();
4647

4748
let rows = conn.query(
@@ -52,12 +53,12 @@ pub(crate) fn add_package_into_database(
5253
homepage_url, description, description_long, readme,
5354
authors, keywords, have_examples, downloads, files,
5455
doc_targets, is_library, doc_rustc_version,
55-
documentation_url, default_target
56+
documentation_url, default_target, features
5657
)
5758
VALUES (
5859
$1, $2, $3, $4, $5, $6, $7, $8, $9,
5960
$10, $11, $12, $13, $14, $15, $16, $17, $18,
60-
$19, $20, $21, $22, $23, $24, $25
61+
$19, $20, $21, $22, $23, $24, $25, $26
6162
)
6263
ON CONFLICT (crate_id, version) DO UPDATE
6364
SET release_time = $3,
@@ -82,7 +83,8 @@ pub(crate) fn add_package_into_database(
8283
is_library = $22,
8384
doc_rustc_version = $23,
8485
documentation_url = $24,
85-
default_target = $25
86+
default_target = $25,
87+
features = $26
8688
RETURNING id",
8789
&[
8890
&crate_id,
@@ -110,6 +112,7 @@ pub(crate) fn add_package_into_database(
110112
&res.rustc_version,
111113
&metadata_pkg.documentation,
112114
&default_target,
115+
&serde_json::to_value(features)?,
113116
],
114117
)?;
115118

@@ -213,6 +216,21 @@ fn convert_dependencies(pkg: &MetadataPackage) -> Vec<(String, String, String)>
213216
.collect()
214217
}
215218

219+
/// Reads features and converts them to Vec<String> with default being first
220+
fn get_features(pkg: &MetadataPackage) -> Vec<String> {
221+
let mut features = Vec::with_capacity(pkg.features.len());
222+
if let Some(default) = pkg.features.get("default") {
223+
features.push(format!("default = {:?}", default));
224+
};
225+
features.extend(
226+
pkg.features
227+
.iter()
228+
.filter(|(name, _)| !name.eq(&"default"))
229+
.map(|(name, features)| format!("{} = {:?}", name, features)),
230+
);
231+
features
232+
}
233+
216234
/// Reads readme if there is any read defined in Cargo.toml of a Package
217235
fn get_readme(pkg: &MetadataPackage, source_dir: &Path) -> Result<Option<String>> {
218236
let readme_path = source_dir.join(pkg.readme.as_deref().unwrap_or("README.md"));

src/db/migrate.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,22 @@ pub fn migrate(version: Option<Version>, conn: &mut Client) -> CratesfyiResult<(
457457
DROP COLUMN total_items_needing_examples,
458458
DROP COLUMN items_with_examples;
459459
"
460-
)
460+
),
461+
migration!(
462+
context,
463+
// version
464+
19,
465+
// description
466+
"Add features that are available for given release",
467+
// upgrade query
468+
"
469+
ALTER TABLE releases ADD COLUMN features JSON;
470+
",
471+
// downgrade query
472+
"
473+
ALTER TABLE releases DROP COLUMN features;
474+
"
475+
),
461476
];
462477

463478
for migration in migrations {

src/test/fakes.rs

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::storage::Storage;
55
use crate::utils::{Dependency, MetadataPackage, Target};
66
use chrono::{DateTime, Utc};
77
use failure::Error;
8+
use std::collections::HashMap;
89
use std::sync::Arc;
910

1011
#[must_use = "FakeRelease does nothing until you call .create()"]
@@ -53,6 +54,15 @@ impl<'a> FakeRelease<'a> {
5354
readme: None,
5455
keywords: vec!["fake".into(), "package".into()],
5556
authors: vec!["Fake Person <[email protected]>".into()],
57+
features: [
58+
("default".into(), vec!["feature1".into(), "feature3".into()]),
59+
("feature1".into(), Vec::new()),
60+
("feature2".into(), vec!["feature1".into()]),
61+
("feature3".into(), Vec::new()),
62+
]
63+
.iter()
64+
.cloned()
65+
.collect::<HashMap<String, Vec<String>>>(),
5666
},
5767
build_result: BuildResult {
5868
rustc_version: "rustc 2.0.0-nightly (000000000 1970-01-01)".into(),

src/utils/cargo_metadata.rs

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub(crate) struct Package {
7777
pub(crate) readme: Option<String>,
7878
pub(crate) keywords: Vec<String>,
7979
pub(crate) authors: Vec<String>,
80+
pub(crate) features: HashMap<String, Vec<String>>,
8081
}
8182

8283
impl Package {

src/web/crate_details.rs

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub struct CrateDetails {
4545
documented_items: Option<f32>,
4646
total_items_needing_examples: Option<f32>,
4747
items_with_examples: Option<f32>,
48+
features: Option<Value>,
4849
/// Database id for this crate
4950
pub(crate) crate_id: i32,
5051
/// Database id for this release
@@ -103,6 +104,7 @@ impl CrateDetails {
103104
releases.license,
104105
releases.documentation_url,
105106
releases.default_target,
107+
releases.features,
106108
doc_coverage.total_items,
107109
doc_coverage.documented_items,
108110
doc_coverage.total_items_needing_examples,
@@ -187,6 +189,7 @@ impl CrateDetails {
187189
total_items: total_items.map(|v| v as f32),
188190
total_items_needing_examples: total_items_needing_examples.map(|v| v as f32),
189191
items_with_examples: items_with_examples.map(|v| v as f32),
192+
features: krate.get("features"),
190193
crate_id,
191194
release_id,
192195
};

templates/rustdoc/topbar.html

+19-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,25 @@
226226
</li>
227227
{%- endfor -%}
228228
</ul>
229-
</li>
229+
</li>{#
230+
Display the features available in current build
231+
#}
232+
{% if 'krate' in __tera_context %}
233+
<li class="pure-menu-item pure-menu-has-children">
234+
<a href="#" class="pure-menu-link">
235+
<span class="title">Feature flags</span>
236+
</a>
237+
238+
{# Build the dropdown list showing available features #}
239+
<ul class="pure-menu-children">
240+
{%- for feature in krate.features -%}
241+
<li class="pure-menu-item">
242+
<a href="#" class="pure-menu-link">{{ feature }}</a>
243+
</li>
244+
{%- endfor -%}
245+
</ul>
246+
</li>
247+
{%- endif -%}
230248
</ul>
231249

232250
{%- include "header/topbar_end.html" -%}

0 commit comments

Comments
 (0)