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

Function format

crates/vm/src/format.rs:180–220  ·  view source on GitHub ↗
(
    format: &FormatString,
    arguments: &FuncArgs,
    vm: &VirtualMachine,
)

Source from the content-addressed store, hash-verified

178}
179
180pub(crate) fn format(
181 format: &FormatString,
182 arguments: &FuncArgs,
183 vm: &VirtualMachine,
184) -> PyResult<Wtf8Buf> {
185 let mut auto_argument_index: usize = 0;
186 let mut seen_index = false;
187 format_internal(vm, format, &mut |field_type| match field_type {
188 FieldType::Auto => {
189 if seen_index {
190 return Err(vm.new_value_error(
191 "cannot switch from manual field specification to automatic field numbering",
192 ));
193 }
194 auto_argument_index += 1;
195 arguments
196 .args
197 .get(auto_argument_index - 1)
198 .cloned()
199 .ok_or_else(|| vm.new_index_error("tuple index out of range"))
200 }
201 FieldType::Index(index) => {
202 if auto_argument_index != 0 {
203 return Err(vm.new_value_error(
204 "cannot switch from automatic field numbering to manual field specification",
205 ));
206 }
207 seen_index = true;
208 arguments
209 .args
210 .get(index)
211 .cloned()
212 .ok_or_else(|| vm.new_index_error("tuple index out of range"))
213 }
214 FieldType::Keyword(keyword) => keyword
215 .as_str()
216 .ok()
217 .and_then(|keyword| arguments.get_optional_kwarg(keyword))
218 .ok_or_else(|| vm.new_key_error(vm.ctx.new_str(keyword).into())),
219 })
220}
221
222pub(crate) fn format_map(
223 format: &FormatString,

Callers 1

as_bufferMethod · 0.50

Calls 9

format_internalFunction · 0.85
ok_or_elseMethod · 0.80
okMethod · 0.80
get_optional_kwargMethod · 0.80
new_key_errorMethod · 0.80
ErrClass · 0.50
getMethod · 0.45
as_strMethod · 0.45
new_strMethod · 0.45

Tested by

no test coverage detected