use super::Expression;
use crate::format;
use crate::Identifier;
use serde::Deserialize;
use std::fmt;
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct FuncName {
pub namespace: Vec<Identifier>,
pub name: Identifier,
}
impl FuncName {
pub fn new(name: impl Into<Identifier>) -> FuncName {
FuncName {
namespace: Vec::new(),
name: name.into(),
}
}
pub fn with_namespace<I>(mut self, namespace: I) -> FuncName
where
I: IntoIterator,
I::Item: Into<Identifier>,
{
self.namespace = namespace.into_iter().map(Into::into).collect();
self
}
pub fn is_namespaced(&self) -> bool {
!self.namespace.is_empty()
}
}
impl<T> From<T> for FuncName
where
T: Into<Identifier>,
{
fn from(name: T) -> Self {
FuncName {
namespace: Vec::new(),
name: name.into(),
}
}
}
impl fmt::Display for FuncName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let formatted = format::to_string(self).expect("a FuncName failed to format unexpectedly");
f.write_str(&formatted)
}
}
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct FuncCall {
pub name: FuncName,
pub args: Vec<Expression>,
pub expand_final: bool,
}
impl FuncCall {
pub fn new<T>(name: T) -> FuncCall
where
T: Into<FuncName>,
{
FuncCall {
name: name.into(),
args: Vec::new(),
expand_final: false,
}
}
pub fn builder<T>(name: T) -> FuncCallBuilder
where
T: Into<FuncName>,
{
FuncCallBuilder {
f: FuncCall::new(name),
}
}
}
#[derive(Debug)]
pub struct FuncCallBuilder {
f: FuncCall,
}
impl FuncCallBuilder {
pub fn arg<T>(mut self, arg: T) -> FuncCallBuilder
where
T: Into<Expression>,
{
self.f.args.push(arg.into());
self
}
pub fn expand_final(mut self, yes: bool) -> FuncCallBuilder {
self.f.expand_final = yes;
self
}
pub fn build(self) -> FuncCall {
self.f
}
}