use crate::aaml::parsing;
use crate::types::list::ListType;
use std::collections::HashMap;
use std::fmt::Display;
use std::ops::Deref;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FoundValue {
inner: String,
}
impl FoundValue {
pub fn new(value: &str) -> FoundValue {
FoundValue {
inner: value.to_string(),
}
}
pub fn remove(&mut self, target: &str) -> &mut Self {
self.inner = self.inner.replace(target, "");
self
}
pub fn as_str(&self) -> &str {
&self.inner
}
pub fn as_list(&self) -> Option<Vec<String>> {
ListType::parse_items(&self.inner)
.map(|items| items.iter().map(|s| s.to_string()).collect())
}
pub fn as_object(&self) -> Option<HashMap<String, String>> {
if !parsing::is_inline_object(&self.inner) {
return None;
}
parsing::parse_inline_object(&self.inner)
.ok()
.map(|pairs| pairs.into_iter().collect())
}
pub fn is_list(&self) -> bool {
let s = self.inner.trim();
s.starts_with('[') && s.ends_with(']')
}
pub fn is_object(&self) -> bool {
parsing::is_inline_object(&self.inner)
}
}
impl From<String> for FoundValue {
fn from(value: String) -> Self {
FoundValue { inner: value }
}
}
impl PartialEq<&str> for FoundValue {
fn eq(&self, other: &&str) -> bool {
self.inner == *other
}
}
impl Display for FoundValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.inner.clone())
}
}
impl Deref for FoundValue {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.inner
}
}