deimos-lang/src/ast/node/d_string.rs
2025-05-26 08:30:15 -05:00

56 lines
1.2 KiB
Rust

use crate::ast::node::expression::Expression;
#[derive(Debug)]
pub struct DString(Vec<Box<DStringPart>>);
impl DString {
pub fn new(parts: Vec<Box<DStringPart>>) -> Self {
Self(parts)
}
pub fn parts(&self) -> Vec<&DStringPart> {
self.0.iter().map(Box::as_ref).collect()
}
pub fn parts_mut(&mut self) -> Vec<&mut DStringPart> {
self.0.iter_mut().map(Box::as_mut).collect()
}
}
#[derive(Debug)]
pub enum DStringPart {
String(String),
Expression(Box<Expression>),
}
impl DStringPart {
pub fn from_string(s: &str) -> DStringPart {
DStringPart::String(String::from(s))
}
pub fn from_expression(e: Expression) -> DStringPart {
DStringPart::Expression(Box::new(e))
}
pub fn is_string(&self) -> bool {
match self {
DStringPart::String(_) => true,
_ => false,
}
}
pub fn unwrap_string(self) -> String {
match self {
DStringPart::String(s) => s,
_ => panic!(),
}
}
pub fn is_expression(&self) -> bool {
match self {
DStringPart::Expression(_) => true,
_ => false,
}
}
}