#
cargo/ops/tree/format/
mod.rs

    1use std::fmt;
    2
    3use anyhow::{bail, Error};
    4
    5use self::parse::{Parser, RawChunk};
    6use super::{Graph, Node, NodeId};
    7
    8mod parse;
    9
    10enum Chunk {
    11    Raw(String),
    12    Package,
    13    License,
    14    Repository,
    15    Features,
    16    LibName,
    17}
    18
    19pub struct Pattern(Vec<Chunk>);
    20
    21impl Pattern {
    22    pub fn new(format: &str) -> Result<Pattern, Error> {
    23        let mut chunks = vec![];
    24
    25        for raw in Parser::new(format) {
    26            let chunk = match raw {
    27                RawChunk::Text(text) => Chunk::Raw(text.to_owned()),
    28                RawChunk::Argument("p") => Chunk::Package,
    29                RawChunk::Argument("l") => Chunk::License,
    30                RawChunk::Argument("r") => Chunk::Repository,
    31                RawChunk::Argument("f") => Chunk::Features,
    32                RawChunk::Argument("lib") => Chunk::LibName,
    33                RawChunk::Argument(a) => {
    34                    bail!("unsupported pattern `{}`", a);
    35                }
    36                RawChunk::Error(err) => bail!("{}", err),
    37            };
    38            chunks.push(chunk);
    39        }
    40
    41        Ok(Pattern(chunks))
    42    }
    43
    44    pub fn display<'a>(&'a self, graph: &'a Graph<'a>, node_index: NodeId) -> Display<'a> {
    45        Display {
    46            pattern: self,
    47            graph,
    48            node_index,
    49        }
    50    }
    51}
    52
    53pub struct Display<'a> {
    54    pattern: &'a Pattern,
    55    graph: &'a Graph<'a>,
    56    node_index: NodeId,
    57}
    58
    59impl<'a> fmt::Display for Display<'a> {
    60    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
    61        let node = self.graph.node(self.node_index);
    62        match node {
    63            Node::Package {
    64                package_id,
    65                features,
    66                ..
    67            } => {
    68                let package = self.graph.package_for_id(*package_id);
    69                for chunk in &self.pattern.0 {
    70                    match chunk {
    71                        Chunk::Raw(s) => fmt.write_str(s)?,
    72                        Chunk::Package => {
    73                            let proc_macro_suffix = if package.proc_macro() {
    74                                " (proc-macro)"
    75                            } else {
    76                                ""
    77                            };
    78                            write!(
    79                                fmt,
    80                                "{} v{}{}",
    81                                package.name(),
    82                                package.version(),
    83                                proc_macro_suffix
    84                            )?;
    85
    86                            let source_id = package.package_id().source_id();
    87                            if !source_id.is_crates_io() {
    88                                write!(fmt, " ({})", source_id)?;
    89                            }
    90                        }
    91                        Chunk::License => {
    92                            if let Some(license) = &package.manifest().metadata().license {
    93                                write!(fmt, "{}", license)?;
    94                            }
    95                        }
    96                        Chunk::Repository => {
    97                            if let Some(repository) = &package.manifest().metadata().repository {
    98                                write!(fmt, "{}", repository)?;
    99                            }
    100                        }
    101                        Chunk::Features => {
    102                            write!(fmt, "{}", features.join(","))?;
    103                        }
    104                        Chunk::LibName => {
    105                            if let Some(target) = package
    106                                .manifest()
    107                                .targets()
    108                                .iter()
    109                                .find(|target| target.is_lib())
    110                            {
    111                                write!(fmt, "{}", target.crate_name())?;
    112                            }
    113                        }
    114                    }
    115                }
    116            }
    117            Node::Feature { name, node_index } => {
    118                let for_node = self.graph.node(*node_index);
    119                match for_node {
    120                    Node::Package { package_id, .. } => {
    121                        write!(fmt, "{} feature \"{}\"", package_id.name(), name)?;
    122                        if self.graph.is_cli_feature(self.node_index) {
    123                            write!(fmt, " (command-line)")?;
    124                        }
    125                    }
    126                    // The node_index in Node::Feature must point to a package
    127                    // node, see `add_feature`.
    128                    _ => panic!("unexpected feature node {:?}", for_node),
    129                }
    130            }
    131        }
    132
    133        Ok(())
    134    }
    135}