/// Called for a span of text. If the text should be highlighted differently
/// from the surrounding text, then the `Class` argument will be a value other
/// than `None`.
///
/// The following sequences of callbacks are equivalent:
/// ```plain
///     enter_span(Foo), string("text", None), exit_span()
///     string("text", Foo)
/// ```
///
/// The latter can be thought of as a shorthand for the former, which is more
/// flexible.
///
/// Note that if `context` is not `None` and that the given `klass` contains a `Span`, the function
/// will then try to find this `span` in the `span_correspondance_map`. If found, it'll then
/// generate a link for this element (which corresponds to where its definition is located).
Called for a span of text. If the text should be highlighted differently
from the surrounding text, then the `Class` argument will be a value other
than `None`.



The latter can be thought of as a shorthand for the former, which is more
flexible.


Called for a span of text . If the text should be highlighted differently from the surrounding text then the ` Class ` argument will be a value other than ` None ` . The latter can be thought of as a shorthand for the former which is more flexible .
fn string<T: Display>(
    out: &mut Buffer,
    text: T,
    klass: Option<Class>,
    context_info: &Option<ContextInfo<'_, '_, '_>>,
) {
    let klass = match klass {
        None => return write!(out, "{}", text),
        Some(klass) => klass,
    };
    let def_span = match klass.get_span() {
        Some(d) => d,
        None => {
            write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text);
            return;
        }
    };
    let mut text_s = text.to_string();
    if text_s.contains("::") {
        text_s = text_s.split("::").intersperse("::").fold(String::new(), |mut path, t| {
            match t {
                "self" | "Self" => write!(
                    &mut path,
                    "<span class=\"{}\">{}</span>",
                    Class::Self_(DUMMY_SP).as_html(),
                    t
                ),
                "crate" | "super" => {
                    write!(&mut path, "<span class=\"{}\">{}</span>", Class::KeyWord.as_html(), t)
                }
                t => write!(&mut path, "{}", t),
            }
            .expect("Failed to build source HTML path");
            path
        });
    }
    if let Some(context_info) = context_info {
        if let Some(href) =
            context_info.context.shared.span_correspondance_map.get(&def_span).and_then(|href| {
                let context = context_info.context;
                // FIXME: later on, it'd be nice to provide two links (if possible) for all items:
                // one to the documentation page and one to the source definition.
                // FIXME: currently, external items only generate a link to their documentation,
                // a link to their definition can be generated using this:
                // https://github.com/rust-lang/rust/blob/60f1a2fc4b535ead9c85ce085fdce49b1b097531/src/librustdoc/html/render/context.rs#L315-L338
                match href {
                    LinkFromSrc::Local(span) => context
                        .href_from_span(*span, true)
                        .map(|s| format!("{}{}", context_info.root_path, s)),
                    LinkFromSrc::External(def_id) => {
                        format::href_with_root_path(*def_id, context, Some(context_info.root_path))
                            .ok()
                            .map(|(url, _, _)| url)
                    }
                    LinkFromSrc::Primitive(prim) => format::href_with_root_path(
                        PrimitiveType::primitive_locations(context.tcx())[prim],
                        context,
                        Some(context_info.root_path),
                    )
                    .ok()
                    .map(|(url, _, _)| url),
                }
            })
        {
            write!(out, "<a class=\"{}\" href=\"{}\">{}</a>", klass.as_html(), href, text_s);
            return;
        }
    }
    write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text_s);
}
fn string < T : Display > ( out : & mut Buffer , text : T , klass : Option < Class > , context_info : & Option < ContextInfo < ' _ , ' _ , ' _ > > , ) { let klass = match klass { None => return write ! ( out " " text ) , Some ( klass ) => klass , } ; let def_span = match klass . get_span ( ) { Some ( d ) => d , None => { write ! ( out " \" \" " klass as_html ( ) text ) ; return ; } } ; let mut text_s = text . to_string ( ) ; if text_s . contains ( " " ) { text_s = text_s . split ( " " ) . intersperse ( " " ) . fold ( String :: new ( ) , | mut path , t | { match t { " " | " " => write ! ( mut path " \" \" " Class Self_ ( DUMMY_SP ) as_html ( ) t ) , " " | " " => { write ! ( mut path " \" \" " Class KeyWord as_html ( ) t ) } t => write ! ( mut path " " t ) , } . expect ( " " ) ; path } ) ; } if let Some ( context_info ) = context_info { if let Some ( href ) = context_info . context . shared . span_correspondance_map . get ( & def_span ) . and_then ( | href | { let context = context_info . context ; // FIXME: later on, it'd be nice to provide two links (if possible) for all items: // one to the documentation page and one to the source definition. // FIXME: currently, external items only generate a link to their documentation, // a link to their definition can be generated using this: // https://github.com/rust-lang/rust/blob/60f1a2fc4b535ead9c85ce085fdce49b1b097531/src/librustdoc/html/render/context.rs#L315-L338 match href { LinkFromSrc :: Local ( span ) => context . href_from_span ( * span , true ) . map ( | s | format ! ( " " context_info root_path s ) ) , LinkFromSrc :: External ( def_id ) => { format :: href_with_root_path ( * def_id , context , Some ( context_info . root_path ) ) . ok ( ) . map ( | ( url , _ , _ ) | url ) } LinkFromSrc :: Primitive ( prim ) => format :: href_with_root_path ( PrimitiveType :: primitive_locations ( context . tcx ( ) ) [ prim ] , context , Some ( context_info . root_path ) , ) . ok ( ) . map ( | ( url , _ , _ ) | url ) , } } ) { write ! ( out " \" \" \" \" " klass as_html ( ) href text_s ) ; return ; } } write ! ( out " \" \" " klass as_html ( ) text_s ) ; }
Called for a span of text.
Called for a span of text .
out
&mut Buffer
text
T
klass
Option<Class>
context_info
&Option<ContextInfo<'_, '_, '_>>