Extractor rewrite
This commit is contained in:
51
crates/pile-value/src/extract/string.rs
Normal file
51
crates/pile-value/src/extract/string.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use pile_config::Label;
|
||||
use smartstring::{LazyCompact, SmartString};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{extract::traits::ObjectExtractor, value::PileValue};
|
||||
|
||||
pub struct StringExtractor {
|
||||
item: Arc<SmartString<LazyCompact>>,
|
||||
}
|
||||
|
||||
impl StringExtractor {
|
||||
pub fn new(item: &Arc<SmartString<LazyCompact>>) -> Self {
|
||||
Self { item: item.clone() }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl ObjectExtractor for StringExtractor {
|
||||
async fn field(&self, name: &Label) -> Result<Option<PileValue>, std::io::Error> {
|
||||
Ok(match name.as_str() {
|
||||
"trim" => Some(PileValue::String(Arc::new(
|
||||
self.item.as_str().trim().into(),
|
||||
))),
|
||||
|
||||
"upper" => Some(PileValue::String(Arc::new(
|
||||
self.item.as_str().to_lowercase().into(),
|
||||
))),
|
||||
|
||||
"lower" => Some(PileValue::String(Arc::new(
|
||||
self.item.as_str().to_uppercase().into(),
|
||||
))),
|
||||
|
||||
"nonempty" => Some(match self.item.is_empty() {
|
||||
true => PileValue::Null,
|
||||
false => PileValue::String(self.item.clone()),
|
||||
}),
|
||||
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
#[expect(clippy::unwrap_used)]
|
||||
async fn fields(&self) -> Result<Vec<Label>, std::io::Error> {
|
||||
return Ok(vec![
|
||||
Label::new("trim").unwrap(),
|
||||
Label::new("upper").unwrap(),
|
||||
Label::new("lower").unwrap(),
|
||||
Label::new("nonempty").unwrap(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user