qubit_text_io/adapters/
str_text_reader.rs1use std::convert::Infallible;
11
12use crate::{
13 TextLineRead,
14 TextRead,
15 adapters::text_cursor::{
16 read_char_at,
17 read_chars_at,
18 read_line_at,
19 read_to_string_at,
20 },
21};
22
23#[derive(Debug)]
25pub struct StrTextReader<'a> {
26 text: &'a str,
27 position: usize,
28}
29
30impl<'a> StrTextReader<'a> {
31 #[must_use]
39 pub const fn new(text: &'a str) -> Self {
40 Self { text, position: 0 }
41 }
42
43 #[must_use]
48 pub const fn position(&self) -> usize {
49 self.position
50 }
51}
52
53impl TextRead for StrTextReader<'_> {
54 type Error = Infallible;
55
56 #[inline]
57 fn read_char(&mut self) -> Result<Option<char>, Self::Error> {
58 Ok(read_char_at(self.text, &mut self.position))
59 }
60
61 #[inline]
62 fn read_chars(&mut self, output: &mut Vec<char>, max: usize) -> Result<usize, Self::Error> {
63 Ok(read_chars_at(self.text, &mut self.position, output, max))
64 }
65
66 #[inline]
67 fn read_to_string(&mut self, output: &mut String) -> Result<usize, Self::Error> {
68 Ok(read_to_string_at(self.text, &mut self.position, output))
69 }
70}
71
72impl TextLineRead for StrTextReader<'_> {
73 #[inline]
74 fn read_line(&mut self, output: &mut String) -> Result<bool, Self::Error> {
75 Ok(read_line_at(self.text, &mut self.position, output))
76 }
77}