Skip to content
This repository has been archived by the owner on Jun 5, 2020. It is now read-only.

Commit

Permalink
Add support for displaying enabled features
Browse files Browse the repository at this point in the history
Uses the {f} format specifier.
  • Loading branch information
g2p committed Oct 2, 2019
1 parent 3703074 commit 66c178b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
12 changes: 12 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use cargo::core::PackageId;
use cargo::core::manifest::ManifestMetadata;
use std::collections::HashSet;


pub struct Node<'a> {
pub id: PackageId,
pub metadata: &'a ManifestMetadata,
pub features: &'a HashSet<String>,
}


25 changes: 14 additions & 11 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use cargo::core::manifest::ManifestMetadata;
use cargo::core::PackageId;
use std::error::Error;
use std::fmt;

use crate::data::Node;
use crate::format::parse::{Parser, RawChunk};

mod parse;
Expand All @@ -12,6 +11,7 @@ enum Chunk {
Package,
License,
Repository,
Features,
}

pub struct Pattern(Vec<Chunk>);
Expand All @@ -26,6 +26,7 @@ impl Pattern {
RawChunk::Argument("p") => Chunk::Package,
RawChunk::Argument("l") => Chunk::License,
RawChunk::Argument("r") => Chunk::Repository,
RawChunk::Argument("f") => Chunk::Features,
RawChunk::Argument(ref a) => {
return Err(format!("unsupported pattern `{}`", a).into());
}
Expand All @@ -39,39 +40,41 @@ impl Pattern {

pub fn display<'a>(
&'a self,
package: &'a PackageId,
metadata: &'a ManifestMetadata,
node: &'a Node,
) -> Display<'a> {
Display {
pattern: self,
package: package,
metadata: metadata,
node,
}
}
}

pub struct Display<'a> {
pattern: &'a Pattern,
package: &'a PackageId,
metadata: &'a ManifestMetadata,
node: &'a Node<'a>,
}

impl<'a> fmt::Display for Display<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
for chunk in &self.pattern.0 {
match *chunk {
Chunk::Raw(ref s) => fmt.write_str(s)?,
Chunk::Package => write!(fmt, "{}", self.package)?,
Chunk::Package => write!(fmt, "{}", self.node.id)?,
Chunk::License => {
if let Some(ref license) = self.metadata.license {
if let Some(ref license) = self.node.metadata.license {
write!(fmt, "{}", license)?
}
}
Chunk::Repository => {
if let Some(ref repository) = self.metadata.repository {
if let Some(ref repository) = self.node.metadata.repository {
write!(fmt, "{}", repository)?
}
}
Chunk::Features => {
let mut features : Vec<_> = self.node.features.iter().cloned().collect();
features.sort();
write!(fmt, "[{}]", features.join(","))?
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/format/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Parser<'a> {
impl<'a> Parser<'a> {
pub fn new(s: &'a str) -> Parser<'a> {
Parser {
s: s,
s,
it: s.char_indices().peekable(),
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use cargo::core::dependency::Kind;
use cargo::core::manifest::ManifestMetadata;
use cargo::core::maybe_allow_nightly_features;
use cargo::core::package::PackageSet;
use cargo::core::registry::PackageRegistry;
Expand All @@ -21,8 +20,10 @@ use std::rc::Rc;
use structopt::clap::AppSettings;
use structopt::StructOpt;

use crate::data::Node;
use crate::format::Pattern;

mod data;
mod format;

#[derive(StructOpt)]
Expand Down Expand Up @@ -346,11 +347,6 @@ fn resolve<'a, 'cfg>(
Ok((packages, resolve))
}

struct Node<'a> {
id: PackageId,
metadata: &'a ManifestMetadata,
}

struct Graph<'a> {
graph: petgraph::Graph<Node<'a>, Kind>,
nodes: HashMap<PackageId, NodeIndex>,
Expand All @@ -370,6 +366,7 @@ fn build_graph<'a>(
let node = Node {
id: root.clone(),
metadata: packages.get_one(root)?.manifest().metadata(),
features: resolve.features(root),
};
graph.nodes.insert(root.clone(), graph.graph.add_node(node));

Expand Down Expand Up @@ -401,6 +398,7 @@ fn build_graph<'a>(
let node = Node {
id: dep_id,
metadata: packages.get_one(dep_id)?.manifest().metadata(),
features: resolve.features(dep_id),
};
*e.insert(graph.graph.add_node(node))
}
Expand Down Expand Up @@ -478,7 +476,7 @@ fn print_dependency<'a>(
Prefix::None => (),
}

println!("{}{}", format.display(&package.id, package.metadata), star);
println!("{}{}", format.display(&package), star);

if !new {
return;
Expand Down

0 comments on commit 66c178b

Please sign in to comment.