1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
use crate::{Token, TokenValue}; #[derive(Debug, Clone, PartialEq)] pub struct StringValue { pub valid: bool, pub bytes: Vec<u8>, } impl StringValue { pub fn new(token: Box<Token>) -> Self { match token.token_value { TokenValue::String(s) => StringValue { valid: true, bytes: s.as_bytes().to_owned(), }, TokenValue::InvalidString(bytes) => StringValue { valid: false, bytes, }, } } pub fn empty() -> Self { StringValue { valid: true, bytes: vec![], } } pub fn to_string_lossy(&self) -> String { String::from_utf8_lossy(&self.bytes).into_owned() } pub fn to_string(&self) -> Option<String> { String::from_utf8(self.bytes.clone()).ok() } pub fn as_bytes(&self) -> &[u8] { &self.bytes } }