Skip to content

Unexpected ParserError #12

@Tim-Paik

Description

@Tim-Paik

I'm having some issues trying to use this crate to make an interaction for my no_std project.
I tried implementing my own IO type (for my environment), but always panic when building the editor:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ParserError', src/main.rs:9:10
stack backtrace:
   0: rust_begin_unwind
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/panicking.rs:142:14
   2: core::result::unwrap_failed
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/result.rs:1814:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/result.rs:1107:23
   4: testkey::main
             at ./src/main.rs:6:22
   5: core::ops::function::FnOnce::call_once
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/ops/function.rs:248:5

here is my code:

// main.rs
use noline::builder::EditorBuilder;
use std::io::Write;

fn main() {
    let mut io = Console::new();
    let mut editor = EditorBuilder::new_unbounded()
        .with_unbounded_history()
        .build_sync(&mut io)
        .unwrap();
    loop {
        if let Ok(line) = editor.readline("> ", &mut io) {
            write!(io, "Read: '{}'\n\r", line).unwrap();
        } else {
            break;
        }
    }
}

pub struct Console(Vec<u8>);

impl Console {
    pub fn new() -> Self {
        let vec = Vec::from([b'a', b'b', b'c']);
        Self(vec)
    }
}

impl noline::sync::Read for Console {
    type Error = core::convert::Infallible;

    fn read(&mut self) -> Result<u8, Self::Error> {
        if let Some(byte) = self.0.last() {
            return Ok(*byte);
        } else {
            panic!("should panic here");
        }
    }
}

impl noline::sync::Write for Console {
    type Error = std::io::Error;

    fn write(&mut self, word: &[u8]) -> Result<(), Self::Error> {
        std::io::Write::write(&mut std::io::stdout(), word).map(|_| ())
    }

    fn flush(&mut self) -> Result<(), Self::Error> {
        Ok(())
    }
}

impl Write for Console {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        std::io::Write::write(&mut std::io::stdout(), buf)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        std::io::stdout().flush()
    }
}

Did I write something wrong? It looks like everything works fine

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions