Skip to content

[BUG] Formatter cannot be implemented due to ResponseUnit being private #32

@RAnders00

Description

@RAnders00

Describe the bug
The Formatter trait is public in the library, it looks to me like you are expected to be able to implement it for custom logic or working with collections other than Vec and ArrayVec.

However, this is not possible, because:

  1. Implementing Formatter requires that you implement response_unit(&mut self) -> scpi::error::Result<scpi::tree::prelude::ResponseUnit> (link: https://docs.rs/scpi/latest/scpi/parser/response/trait.Formatter.html#tymethod.response_unit)
  2. Constructing a new, blank ResponseUnit is not possible, because the fields are private and no new() function is provided. Link: https://docs.rs/scpi/latest/scpi/parser/response/struct.ResponseUnit.html

To Reproduce
For example, with this snippet:

use heapless::Vec;
use scpi::{
    error::{Error as ScpiError, ErrorCode},
    tree::prelude::ResponseUnit,
};

struct ScpiResponse<const N: usize>(Vec<u8, N>);

impl<const N: usize> scpi::parser::response::Formatter for ScpiResponse<N> {
    fn push_str(&mut self, s: &[u8]) -> scpi::error::Result<()> {
        self.0
            .extend_from_slice(s)
            .map_err(|_| ScpiError::from(ErrorCode::OutOfMemory))
    }

    fn push_byte(&mut self, b: u8) -> scpi::error::Result<()> {
        self.0
            .push(b)
            .map_err(|_| ScpiError::from(ErrorCode::OutOfMemory))
    }

    fn as_slice(&self) -> &[u8] {
        self.0.as_slice()
    }

    fn clear(&mut self) {
        self.0.clear();
    }

    fn len(&self) -> usize {
        self.0.len()
    }

    fn message_start(&mut self) -> scpi::error::Result<()> {
        Ok(())
    }

    fn message_end(&mut self) -> scpi::error::Result<()> {
        self.push_byte(b'\n')
    }

    fn response_unit(&mut self) -> scpi::error::Result<scpi::tree::prelude::ResponseUnit> {
        if !self.0.is_empty() {
            self.push_byte(b';')?;
        }
        Ok(ResponseUnit {
            fmt: self,
            result: Ok(()),
            has_header: false,
            has_data: false,
        })
    }
}

Expected behavior
It is possible to make an implementation of Formatter in your own crate that successfully compiles and works correctly.

Library and tool versions:

  • scpi version 1.0.1
  • rust version 1.86.0
  • cargo version 1.86.0

Additional context
N/A

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingenhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions