use std::rc::Rc;

use super::{Environment, Error, Node, NodeEnum, Precedence};

#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub struct StringNode {
    value: String,
}

impl From<String> for StringNode {
    fn from(value: String) -> Self {
        Self { value }
    }
}

impl Node for StringNode {
    fn evaluate(&self, _: &mut super::Environment) -> Result<Rc<super::NodeEnum>, Error> {
        Ok(Rc::new(self.clone().into()))
    }

    fn as_string(&self, _env: Option<&Environment>) -> String {
        format!("\"{}\"", self.value.clone())
    }

    fn precedence(&self) -> Precedence {
        Precedence::Primary
    }
}

impl StringNode {
    pub fn new(value: String) -> Rc<NodeEnum> {
        Rc::new(Self { value }.into())
    }

    pub fn get_value(&self) -> &String {
        &self.value
    }
}
