rscamper 0.2.2

Rust interface to scamper network measurement tool
Documentation
// rscamper - Rust wrapper for scamper_neighbourdisc results
//
// Copyright (C) 2026 Dimitrios Giakatos
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.

use std::ffi::CStr;

use crate::ffi::scamper_neighbourdisc::{self, ScamperNeighbourdiscT};

/// A scamper neighbour discovery measurement result.
pub struct ScamperNeighbourdisc {
    pub(crate) inner: *mut ScamperNeighbourdiscT,
}

impl ScamperNeighbourdisc {
    pub(crate) unsafe fn from_ptr(ptr: *mut ScamperNeighbourdiscT) -> Option<Self> {
        if ptr.is_null() { return None; }
        Some(ScamperNeighbourdisc { inner: ptr })
    }

    /// Convert to JSON string.
    pub fn to_json(&self) -> Option<String> {
        let mut len = 0usize;
        let ptr = unsafe { scamper_neighbourdisc::scamper_neighbourdisc_tojson(self.inner, &mut len) };
        if ptr.is_null() { return None; }
        let s = unsafe { CStr::from_ptr(ptr) }.to_string_lossy().into_owned();
        unsafe { libc::free(ptr as *mut libc::c_void) };
        Some(s)
    }
}

impl Drop for ScamperNeighbourdisc {
    fn drop(&mut self) {
        unsafe { scamper_neighbourdisc::scamper_neighbourdisc_free(self.inner) };
    }
}

unsafe impl Send for ScamperNeighbourdisc {}
unsafe impl Sync for ScamperNeighbourdisc {}