1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//! Avahi implementation for cross-platform TXT record.

use super::string_list::{AvahiStringListNode, ManagedAvahiStringList};
use crate::txt_record::TTxtRecord;
use crate::Result;
use libc::c_char;
use std::cell::UnsafeCell;

#[derive(Debug)]
pub struct AvahiTxtRecord(UnsafeCell<ManagedAvahiStringList>);

impl TTxtRecord for AvahiTxtRecord {
    fn new() -> Self {
        Self(UnsafeCell::default())
    }

    fn insert(&mut self, key: &str, value: &str) -> Result<()> {
        unsafe {
            self.inner_mut().add_pair(
                c_string!(key).as_ptr() as *const c_char,
                c_string!(value).as_ptr() as *const c_char,
            );
        }
        Ok(())
    }

    fn get(&self, key: &str) -> Option<String> {
        unsafe {
            self.inner_mut()
                .find(c_string!(key).as_ptr() as *const c_char)?
                .get_pair()
                .value()
                .as_str()
                .map(|s| s.to_string())
        }
    }

    fn remove(&mut self, key: &str) -> Result<()> {
        let mut list = ManagedAvahiStringList::new();
        let mut map = self.to_map();

        map.remove(key);

        for (key, value) in map {
            unsafe {
                list.add_pair(
                    c_string!(key).as_ptr() as *const c_char,
                    c_string!(value).as_ptr() as *const c_char,
                );
            }
        }

        self.0 = UnsafeCell::new(list);

        Ok(())
    }

    fn contains_key(&self, key: &str) -> bool {
        unsafe {
            self.inner_mut()
                .find(c_string!(key).as_ptr() as *const c_char)
                .is_some()
        }
    }

    fn len(&self) -> usize {
        self.inner().length() as usize
    }

    fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = (String, String)> + 'a> {
        Box::new(Iter::new(self.inner_mut().head()))
    }

    fn keys<'a>(&'a self) -> Box<dyn Iterator<Item = String> + 'a> {
        Box::new(Keys(Iter::new(self.inner_mut().head())))
    }

    fn values<'a>(&'a self) -> Box<dyn Iterator<Item = String> + 'a> {
        Box::new(Values(Iter::new(self.inner_mut().head())))
    }
}

impl AvahiTxtRecord {
    #[allow(clippy::mut_from_ref)]
    fn inner_mut(&self) -> &mut ManagedAvahiStringList {
        unsafe { &mut *self.0.get() }
    }

    pub(crate) fn inner(&self) -> &ManagedAvahiStringList {
        unsafe { &*self.0.get() }
    }
}

impl From<ManagedAvahiStringList> for AvahiTxtRecord {
    fn from(list: ManagedAvahiStringList) -> Self {
        Self(UnsafeCell::new(list))
    }
}

impl Clone for AvahiTxtRecord {
    fn clone(&self) -> Self {
        Self::from(self.inner().clone())
    }
}

impl PartialEq for AvahiTxtRecord {
    fn eq(&self, other: &Self) -> bool {
        self.inner() == other.inner()
    }
}

pub struct Iter<'a> {
    node: Option<AvahiStringListNode<'a>>,
}

impl<'a> Iter<'a> {
    pub fn new(node: AvahiStringListNode<'a>) -> Self {
        Self { node: Some(node) }
    }
}

impl Iterator for Iter<'_> {
    type Item = (String, String);

    fn next(&mut self) -> Option<Self::Item> {
        let mut n = self.node.take()?;
        let pair = n.get_pair();
        self.node = n.next();

        Some((
            pair.key().as_str().unwrap().to_string(),
            pair.value().as_str().unwrap().to_string(),
        ))
    }
}

pub struct Keys<'a>(Iter<'a>);

impl Iterator for Keys<'_> {
    type Item = String;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(|e| e.0)
    }
}

pub struct Values<'a>(Iter<'a>);

impl Iterator for Values<'_> {
    type Item = String;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(|e| e.1)
    }
}