Repository: nushell/nushell
File: crates/nu-command/src/bytes/at.rs (Rust)

Code before:

        let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 1)?;
        let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);

        if let PipelineData::ByteStream(stream, metadata) = input {
            let stream = stream.slice(call.head, call.arguments_span(), range)?;
            // bytes 3..5 of an image/png stream are not image/png themselves
            let metadata = metadata.map(|metadata| metadata.with_content_type(None));
            Ok(PipelineData::byte_stream(stream, metadata))
        } else {
            operate(
                map_value,
                Arguments { range, cell_paths },
                input,
                call.head,
                engine_state.signals(),
            )
            .map(|pipeline| match pipeline {
                PipelineData::ByteStream(stream, Some(metadata)) => {
                    PipelineData::byte_stream(stream, Some(metadata.with_content_type(None)))
                }
                PipelineData::Value(val, Some(metadata)) => {
                    PipelineData::Value(val, Some(metadata.with_content_type(None)))
                }
                _ => pipeline,
            })
        }
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "Extract bytes starting from a specific index",
                example: "{ data: 0x[33 44 55 10 01 13 10] } | bytes at 3.. data",
                result: Some(Value::test_record(record! {
                    "data" => Value::test_binary(vec![0x10, 0x01, 0x13, 0x10]),
                })),
            },
            Example {
                description: "Slice out `0x[10 01 13]` from `0x[33 44 55 10 01 13]`",
                example: "0x[33 44 55 10 01 13] | bytes at 3..5",
                result: Some(Value::test_binary(vec![0x10, 0x01, 0x13])),
            },
            Example {
                description: "Extract bytes from the start up to a specific index",
                example: "0x[33 44 55 10 01 13 10] | bytes at ..4",
                result: Some(Value::test_binary(vec![0x33, 0x44, 0x55, 0x10, 0x01])),
            },
            Example {
                description: "Extract byte `0x[10]` using an exclusive end index",
                example: "0x[33 44 55 10 01 13 10] | bytes at 3..<4",

Review comment (suggestion):
```suggestion
            .map(|pipeline| {
                let metadata = pipeline
                    .metadata()
                    .map(|metadata| metadata.with_content_type(None));
                pipeline.set_metadata(metadata)
            })
```
I find this easier to understand at a glance. It should be possible to apply this to the other commands as well.

Code after:

        let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 1)?;
        let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);

        if let PipelineData::ByteStream(stream, metadata) = input {
            let stream = stream.slice(call.head, call.arguments_span(), range)?;
            // bytes 3..5 of an image/png stream are not image/png themselves
            let metadata = metadata.map(|m| m.with_content_type(None));
            Ok(PipelineData::byte_stream(stream, metadata))
        } else {
            operate(
                map_value,
                Arguments { range, cell_paths },
                input,
                call.head,
                engine_state.signals(),
            )
            .map(|pipeline| {
                // bytes 3..5 of an image/png stream are not image/png themselves
                let metadata = pipeline.metadata().map(|m| m.with_content_type(None));
                pipeline.set_metadata(metadata)
            })
        }
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "Extract bytes starting from a specific index",
                example: "{ data: 0x[33 44 55 10 01 13 10] } | bytes at 3.. data",
                result: Some(Value::test_record(record! {
                    "data" => Value::test_binary(vec![0x10, 0x01, 0x13, 0x10]),
                })),
            },
            Example {
                description: "Slice out `0x[10 01 13]` from `0x[33 44 55 10 01 13]`",
                example: "0x[33 44 55 10 01 13] | bytes at 3..5",
                result: Some(Value::test_binary(vec![0x10, 0x01, 0x13])),
            },
            Example {
                description: "Extract bytes from the start up to a specific index",
                example: "0x[33 44 55 10 01 13 10] | bytes at ..4",
                result: Some(Value::test_binary(vec![0x33, 0x44, 0x55, 0x10, 0x01])),
            },
            Example {
                description: "Extract byte `0x[10]` using an exclusive end index",
                example: "0x[33 44 55 10 01 13 10] | bytes at 3..<4",
                result: Some(Value::test_binary(vec![0x10])),
            },
            Example {
                description: "Extract bytes up to a negative index (inclusive)",