diff options
| author | Max Audron <me@audron.dev> | 2026-02-22 16:18:37 +0100 |
|---|---|---|
| committer | Max Audron <me@audron.dev> | 2026-02-22 16:18:37 +0100 |
| commit | ff14a566f3d3d67a35cb3803542e9ab45489eef3 (patch) | |
| tree | 4dba6db09e18e5ce184617ea9a0762b0875f3c65 /src/message/signalproxy/objects | |
| parent | NetworkMap error handling (diff) | |
improve network from_network_list impl
it makes a lot more sense to convert the VariantList to a VariantMap
first, as we then don't have to iterate ove the whole thing a few times,
and we can simply use the from_network_map impl to convert it to the
types object
still need to make this change for other types
Diffstat (limited to '')
| -rw-r--r-- | src/message/signalproxy/objects/network.rs | 108 |
1 files changed, 8 insertions, 100 deletions
diff --git a/src/message/signalproxy/objects/network.rs b/src/message/signalproxy/objects/network.rs index fac68a7..996dd80 100644 --- a/src/message/signalproxy/objects/network.rs +++ b/src/message/signalproxy/objects/network.rs @@ -370,108 +370,16 @@ impl crate::message::signalproxy::NetworkList for Network { // TODO VariantList -> VariantMap conversion fn from_network_list(input: &mut VariantList) -> Result<Self> { - let mut i = input.iter().cycle(); + let mut i = input.into_iter(); + let mut map: VariantMap = VariantMap::new(); - let users_and_channels: VariantMap = { - i.position(|x| *x == Variant::ByteArray(String::from("IrcUsersAndChannels"))) - .unwrap(); - - i.next().unwrap().try_into().unwrap() - }; - - log::trace!("users and channels: {:#?}", users_and_channels); - - let mut network = Self { - my_nick: { - i.position(|x| *x == Variant::ByteArray(String::from("myNick"))) - .unwrap(); - - i.next().unwrap().clone().try_into().unwrap() - }, - latency: { - i.position(|x| *x == Variant::ByteArray(String::from("latency"))) - .unwrap(); - - i.next().unwrap().try_into().unwrap() - }, - current_server: { - i.position(|x| *x == Variant::ByteArray(String::from("currentServer"))) - .unwrap(); - - i.next().unwrap().clone().try_into().unwrap() - }, - is_connected: { - i.position(|x| *x == Variant::ByteArray(String::from("isConnected"))) - .unwrap(); - - i.next().unwrap().try_into().unwrap() - }, - connection_state: ConnectionState::from_i32({ - i.position(|x| *x == Variant::ByteArray(String::from("connectionState"))) - .unwrap(); - - i.next().unwrap().try_into().unwrap() - }) - .unwrap(), - prefixes: Vec::new(), - prefix_modes: Vec::new(), - channel_modes: HashMap::with_capacity(4), - irc_users: { - match users_and_channels.get("Users") { - Some(users) => { - let users: Vec<IrcUser> = Vec::<IrcUser>::from_network_map( - &mut users.try_into().expect("failed to convert Users"), - )?; - - users.into_iter().map(|user| (user.nick.clone(), user)).collect() - } - None => HashMap::new(), - } - }, - irc_channels: { - match users_and_channels.get("Channels") { - Some(channels) => { - let channels: Vec<IrcChannel> = - Vec::<IrcChannel>::from_network_map(&mut channels.try_into()?)?; - channels - .into_iter() - .map(|channel| (channel.name.clone(), channel)) - .collect() - } - None => HashMap::new(), - } - }, - supports: { - i.position(|x| *x == Variant::ByteArray(String::from("Supports"))) - .unwrap(); - - let var: VariantMap = i.next().unwrap().try_into().unwrap(); - - var.into_iter().map(|(k, v)| (k, v.try_into().unwrap())).collect() - }, - caps: { - i.position(|x| *x == Variant::ByteArray(String::from("Caps"))) - .unwrap(); - - let var: VariantMap = i.next().unwrap().try_into().unwrap(); - - var.into_iter().map(|(k, v)| (k, v.try_into().unwrap())).collect() - }, - caps_enabled: { - i.position(|x| *x == Variant::ByteArray(String::from("CapsEnabled"))) - .unwrap(); - - let var: VariantList = i.next().unwrap().try_into().unwrap(); - - var.into_iter().map(|v| v.try_into().unwrap()).collect() - }, - network_info: NetworkInfo::from_network_list(input).unwrap(), - }; - - network.determine_channel_mode_types(); - network.determine_prefixes(); + while let Some(key) = i.next() { + let key: String = key.clone().try_into()?; + let value = i.next().ok_or(ProtocolError::MissingField(key.clone()))?; + map.insert(key, value.clone()); + } - Ok(network) + Self::from_network_map(&mut map) } } |
