nu-zenoh 0.5.1

Nu-Zenoh integration
Documentation
//
// Copyright (c) 2025 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//
use nu_engine::CallExt;
use nu_protocol::{
    engine::{Call, Command, EngineState, Stack},
    PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
use zenoh::Wait;

use crate::{cmd::liveliness::LivelinessTokenValue, signature_ext::SignatureExt, State};

#[derive(Clone)]
pub(crate) struct UndeclareToken {
    _state: State,
}

impl UndeclareToken {
    pub(crate) fn new(state: State) -> Self {
        Self { _state: state }
    }
}

impl Command for UndeclareToken {
    fn name(&self) -> &str {
        "zenoh liveliness undeclare-token"
    }

    fn signature(&self) -> nu_protocol::Signature {
        Signature::build(self.name())
            .session()
            .zenoh_category()
            .input_output_type(Type::Nothing, Type::Nothing)
            .required("token", SyntaxShape::Any, "liveliness token")
            .allowed_origin()
    }

    fn description(&self) -> &str {
        "Undeclare a liveliness token"
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        _input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let token_value = call.req::<Value>(engine_state, stack, 0)?;
        let mut custom_value = token_value.into_custom_value()?;

        let mut token_lock = custom_value
            .as_mut_any()
            .downcast_mut::<LivelinessTokenValue>()
            .unwrap()
            .handle
            .lock()
            .unwrap();

        if let Some(token) = token_lock.take() {
            token.undeclare().wait().unwrap();
        }

        Ok(PipelineData::Empty)
    }
}