Refactor naming
This commit is contained in:
parent
a9fd240bd3
commit
ad58ab9bdd
10 changed files with 66 additions and 62 deletions
src/models
84
src/models/channel.rs
Normal file
84
src/models/channel.rs
Normal file
|
@ -0,0 +1,84 @@
|
|||
use std::fmt::{Display, Formatter};
|
||||
|
||||
use crate::parameter::{parameter_find, ParameterList};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Channel {
|
||||
pub cid: i32,
|
||||
pub pid: i32,
|
||||
pub channel_order: i32,
|
||||
pub channel_name: String,
|
||||
pub channel_topic: String,
|
||||
pub channel_flag_are_subscribed: bool,
|
||||
}
|
||||
|
||||
impl Display for Channel {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{} ({})", self.channel_name, self.cid)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ParameterList> for Channel {
|
||||
fn from(value: ParameterList) -> Self {
|
||||
Channel {
|
||||
cid: parameter_find(&value, "cid")
|
||||
.unwrap_or_default()
|
||||
.to_i32(-1),
|
||||
pid: parameter_find(&value, "pid")
|
||||
.unwrap_or_default()
|
||||
.to_i32(-1),
|
||||
channel_order: parameter_find(&value, "channel_order")
|
||||
.unwrap_or_default()
|
||||
.to_i32(-1),
|
||||
channel_name: parameter_find(&value, "channel_name")
|
||||
.unwrap_or_default()
|
||||
.value,
|
||||
channel_topic: parameter_find(&value, "channel_topic")
|
||||
.unwrap_or_default()
|
||||
.value,
|
||||
channel_flag_are_subscribed: parameter_find(&value, "channel_flag_are_subscribed")
|
||||
.unwrap_or_default()
|
||||
.to_i32(0) == 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Channel {
|
||||
pub fn is_spacer(&self) -> bool {
|
||||
self.channel_name.starts_with("[spacer")
|
||||
|| self.channel_name.starts_with("[*spacer")
|
||||
|| self.channel_name.starts_with("[lspacer")
|
||||
|| self.channel_name.starts_with("[cspacer")
|
||||
|| self.channel_name.starts_with("[rspacer")
|
||||
}
|
||||
|
||||
fn list_find_next(list: &mut Vec<Channel>, order: i32, pid: i32) -> Option<Channel> {
|
||||
let index = list.iter().position(|a| a.channel_order == order && a.pid == pid)?;
|
||||
Some(list.swap_remove(index))
|
||||
}
|
||||
|
||||
pub fn sort_list(mut list: Vec<Channel>) -> Vec<Channel> {
|
||||
let mut list_sorted: Vec<Channel> = Vec::new();
|
||||
let mut pids: Vec<i32> = Vec::new();
|
||||
let mut pid = 0;
|
||||
let mut order = 0;
|
||||
|
||||
while !list.is_empty() {
|
||||
match Channel::list_find_next(&mut list, order, pid) {
|
||||
None => {
|
||||
order = pid;
|
||||
pid = pids.pop().unwrap_or(0);
|
||||
}
|
||||
Some(elem) => {
|
||||
pids.push(pid);
|
||||
pid = elem.cid;
|
||||
order = 0;
|
||||
list_sorted.push(elem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
list_sorted
|
||||
}
|
||||
}
|
||||
|
40
src/models/client.rs
Normal file
40
src/models/client.rs
Normal file
|
@ -0,0 +1,40 @@
|
|||
use std::fmt::{Display, Formatter};
|
||||
|
||||
use crate::parameter::{parameter_find, ParameterList};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Client {
|
||||
pub cid: i32,
|
||||
pub clid: i32,
|
||||
pub client_database_id: i32,
|
||||
pub client_nickname: String,
|
||||
pub client_type: i32,
|
||||
}
|
||||
|
||||
impl Display for Client {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{} ({})", self.client_nickname, self.clid)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ParameterList> for Client {
|
||||
fn from(value: ParameterList) -> Self {
|
||||
Client {
|
||||
cid: parameter_find(&value, "cid")
|
||||
.unwrap_or_default()
|
||||
.to_i32(-1),
|
||||
clid: parameter_find(&value, "clid")
|
||||
.unwrap_or_default()
|
||||
.to_i32(-1),
|
||||
client_database_id: parameter_find(&value, "client_database_id")
|
||||
.unwrap_or_default()
|
||||
.to_i32(-1),
|
||||
client_nickname: parameter_find(&value, "client_nickname")
|
||||
.unwrap_or_default()
|
||||
.value,
|
||||
client_type: parameter_find(&value, "channel_topic")
|
||||
.unwrap_or_default()
|
||||
.to_i32(0),
|
||||
}
|
||||
}
|
||||
}
|
5
src/models/mod.rs
Normal file
5
src/models/mod.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
mod channel;
|
||||
mod client;
|
||||
|
||||
pub use channel::Channel;
|
||||
pub use client::Client;
|
Loading…
Add table
Add a link
Reference in a new issue