lib_ruby_parser/source/
input.rs1use crate::source::Decoder;
2use crate::source::SourceLine;
3use crate::source::{decode_input, DecodedInput, InputError};
4
5#[derive(Debug, Default)]
7#[repr(C)]
8pub struct Input {
9 pub(crate) decoded: DecodedInput,
10 decoder: Option<Decoder>,
11}
12
13impl Input {
14 pub fn new<Name>(name: Name, decoder: Option<Decoder>) -> Self
16 where
17 Name: Into<String>,
18 {
19 Self {
20 decoded: DecodedInput::named(name),
21 decoder,
22 }
23 }
24
25 pub fn update_bytes(&mut self, bytes: Vec<u8>) {
27 self.decoded.update_bytes(bytes)
28 }
29
30 pub(crate) fn byte_at(&self, idx: usize) -> Option<u8> {
31 self.decoded.bytes.get(idx).copied()
32 }
33
34 pub(crate) fn unchecked_byte_at(&self, idx: usize) -> u8 {
35 self.decoded.bytes[idx]
36 }
37
38 pub(crate) fn substr_at(&self, start: usize, end: usize) -> Option<&[u8]> {
39 self.decoded.substr_at(start, end)
40 }
41
42 pub fn line_col_for_pos(&self, pos: usize) -> Option<(usize, usize)> {
46 self.decoded.line_col_for_pos(pos)
47 }
48
49 pub(crate) fn len(&self) -> usize {
50 self.decoded.len()
51 }
52
53 pub(crate) fn line_at(&self, idx: usize) -> &SourceLine {
58 self.decoded.line_at(idx)
59 }
60
61 pub(crate) fn lines_count(&self) -> usize {
62 self.decoded.lines.len()
63 }
64
65 pub(crate) fn set_encoding(&mut self, encoding: &str) -> Result<(), InputError> {
66 let new_input = decode_input(
67 self.decoded.take_bytes(),
68 String::from(encoding),
69 &mut self.decoder,
70 )
71 .into_result()?;
72 self.update_bytes(new_input);
73 Ok(())
74 }
75
76 pub fn as_shared_bytes(&self) -> &[u8] {
78 self.decoded.as_shared_bytes()
79 }
80
81 pub fn into_bytes(self) -> Vec<u8> {
83 self.decoded.into_bytes()
84 }
85}