MCPcopy Index your code
hub / github.com/RustPython/RustPython / unparse_formatted

Method unparse_formatted

crates/codegen/src/unparse.rs:545–597  ·  view source on GitHub ↗
(
        &mut self,
        val: &ast::Expr,
        debug_text: Option<&ast::DebugText>,
        conversion: ast::ConversionFlag,
        spec: Option<&ast::InterpolatedStringFormatSpec>,
    )

Source from the content-addressed store, hash-verified

543 }
544
545 fn unparse_formatted(
546 &mut self,
547 val: &ast::Expr,
548 debug_text: Option<&ast::DebugText>,
549 conversion: ast::ConversionFlag,
550 spec: Option<&ast::InterpolatedStringFormatSpec>,
551 ) -> fmt::Result {
552 let buffered =
553 fmt::from_fn(|f| Unparser::new(f, self.source).unparse_expr(val, precedence::TEST + 1))
554 .to_string();
555 if let Some(ast::DebugText { leading, trailing }) = debug_text {
556 self.p(leading)?;
557 self.p(self.source.slice(val.range()))?;
558 self.p(trailing)?;
559 }
560 let brace = if buffered.starts_with('{') {
561 // put a space to avoid escaping the bracket
562 "{ "
563 } else {
564 // Preserve leading whitespace between '{' and the expression
565 let source_text = self.source.source_text();
566 let start = val.range().start().to_usize();
567 if start > 0
568 && source_text
569 .as_bytes()
570 .get(start - 1)
571 .is_some_and(|b| b.is_ascii_whitespace())
572 {
573 "{ "
574 } else {
575 "{"
576 }
577 };
578 self.p(brace)?;
579 self.p(&buffered)?;
580 drop(buffered);
581
582 if conversion != ast::ConversionFlag::None {
583 self.p("!")?;
584 let buf = &[conversion as u8];
585 let c = core::str::from_utf8(buf).unwrap();
586 self.p(c)?;
587 }
588
589 if let Some(spec) = spec {
590 self.p(":")?;
591 self.unparse_fstring_body(&spec.elements)?;
592 }
593
594 self.p("}")?;
595
596 Ok(())
597 }
598
599 fn unparse_fstring_elem(&mut self, elem: &ast::InterpolatedStringElement) -> fmt::Result {
600 match elem {

Callers 1

unparse_fstring_elemMethod · 0.80

Calls 12

newFunction · 0.85
to_stringMethod · 0.80
unparse_exprMethod · 0.80
starts_withMethod · 0.80
unparse_fstring_bodyMethod · 0.80
pMethod · 0.45
sliceMethod · 0.45
to_usizeMethod · 0.45
startMethod · 0.45
getMethod · 0.45
as_bytesMethod · 0.45
unwrapMethod · 0.45

Tested by

no test coverage detected