anyxml 0.4.1

A fully spec-conformant XML library
Documentation
use std::{cell::RefCell, rc::Rc};

use crate::tree::{
    Document, NodeType,
    node::{Node, NodeCore, NodeSpec},
};

pub struct ProcessingInstructionSpec {
    target: Rc<str>,
    data: Option<Rc<str>>,
}

impl NodeSpec for ProcessingInstructionSpec {
    fn node_type(&self) -> NodeType {
        NodeType::ProcessingInstruction
    }

    fn first_child(&self) -> Option<Rc<RefCell<NodeCore<dyn NodeSpec>>>> {
        None
    }

    fn last_child(&self) -> Option<Rc<RefCell<NodeCore<dyn NodeSpec>>>> {
        None
    }
}

pub type ProcessingInstruction = Node<ProcessingInstructionSpec>;

impl ProcessingInstruction {
    pub(crate) fn new(target: Rc<str>, data: Option<Rc<str>>, owner_document: Document) -> Self {
        Node::create_node(ProcessingInstructionSpec { target, data }, owner_document)
    }

    pub fn target(&self) -> Rc<str> {
        self.core.borrow().spec.target.clone()
    }

    pub fn data(&self) -> Option<Rc<str>> {
        self.core.borrow().spec.data.clone()
    }
}

impl std::fmt::Display for ProcessingInstruction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "<?{}", self.target())?;
        if let Some(data) = self.data() {
            write!(f, " {}", data)?;
        }
        write!(f, "?>")
    }
}