codama-nodes 0.9.3

Node specifications and helpers for the Codama standard
Documentation
use crate::{ConstantDiscriminatorNode, ConstantValueNode};

impl ConstantDiscriminatorNode {
    pub fn new<T>(constant: T, offset: u64) -> Self
    where
        T: Into<ConstantValueNode>,
    {
        Self {
            constant: constant.into(),
            offset,
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        Base16, ConstantDiscriminatorNode, ConstantValueNode, NumberTypeNode, NumberValueNode, U32,
    };

    #[test]
    fn new() {
        let node = ConstantDiscriminatorNode::new(
            ConstantValueNode::new(NumberTypeNode::le(U32), NumberValueNode::new(42u32)),
            0,
        );
        assert_eq!(
            node.constant,
            ConstantValueNode::new(NumberTypeNode::le(U32), NumberValueNode::new(42u32))
        );
        assert_eq!(node.offset, 0);
    }

    #[test]
    fn to_json() {
        let node =
            ConstantDiscriminatorNode::new(ConstantValueNode::bytes(Base16, "deadb0d1e5"), 0);
        let json = serde_json::to_string(&node).unwrap();
        assert_eq!(
            json,
            r#"{"kind":"constantDiscriminatorNode","offset":0,"constant":{"kind":"constantValueNode","type":{"kind":"bytesTypeNode"},"value":{"kind":"bytesValueNode","data":"deadb0d1e5","encoding":"base16"}}}"#
        );
    }

    #[test]
    fn from_json() {
        let json = r#"{"kind":"constantDiscriminatorNode","offset":0,"constant":{"kind":"constantValueNode","type":{"kind":"bytesTypeNode"},"value":{"kind":"bytesValueNode","data":"deadb0d1e5","encoding":"base16"}}}"#;
        let node: ConstantDiscriminatorNode = serde_json::from_str(json).unwrap();
        assert_eq!(
            node,
            ConstantDiscriminatorNode::new(ConstantValueNode::bytes(Base16, "deadb0d1e5"), 0)
        );
    }
}