#![allow(unused, clippy::manual_range_patterns)]
use crate::profile::{ProfileType, typedef};
use crate::proto::*;
use alloc::borrow::ToOwned;
use alloc::string::String;
use alloc::vec::Vec;
#[derive(Debug, Clone)]
pub struct Video {
pub url: String,
pub hosting_provider: String,
pub duration: u32,
pub unknown_fields: Vec<Field>,
pub developer_fields: Vec<DeveloperField>,
}
impl Video {
pub const URL: u8 = 0;
pub const HOSTING_PROVIDER: u8 = 1;
pub const DURATION: u8 = 2;
pub const fn new() -> Self {
Self {
url: String::new(),
hosting_provider: String::new(),
duration: u32::MAX,
unknown_fields: Vec::new(),
developer_fields: Vec::new(),
}
}
fn count_valid_fields(&self) -> usize {
(!self.url.is_empty()) as usize
+ (!self.hosting_provider.is_empty()) as usize
+ (self.duration != u32::MAX) as usize
}
}
impl Default for Video {
fn default() -> Self {
Self::new()
}
}
impl From<&Message> for Video {
fn from(mesg: &Message) -> Self {
const KNOWN_NUMS: [u64; 4] = [7, 0, 0, 0];
let mut n = 0u64;
for field in &mesg.fields {
n += (KNOWN_NUMS[field.num as usize >> 6] >> (field.num & 63)) & 1 ^ 1
}
let mut v = Self::new();
v.unknown_fields = Vec::<Field>::with_capacity(n as usize);
v.developer_fields = mesg.developer_fields.clone();
for field in &mesg.fields {
match field.num {
0 => v.url = field.value.as_str().to_owned(),
1 => v.hosting_provider = field.value.as_str().to_owned(),
2 => v.duration = field.value.as_u32(),
_ => v.unknown_fields.push(field.clone()),
};
}
v
}
}
impl From<Video> for Message {
fn from(m: Video) -> Self {
let mut fields =
Vec::<Field>::with_capacity(m.count_valid_fields() + m.unknown_fields.len());
if !m.url.is_empty() {
fields.push(Field {
num: 0,
profile_type: ProfileType::STRING,
value: Value::String(m.url),
is_expanded: false,
});
};
if !m.hosting_provider.is_empty() {
fields.push(Field {
num: 1,
profile_type: ProfileType::STRING,
value: Value::String(m.hosting_provider),
is_expanded: false,
});
};
if m.duration != u32::MAX {
fields.push(Field {
num: 2,
profile_type: ProfileType::UINT32,
value: Value::Uint32(m.duration),
is_expanded: false,
});
};
fields.extend_from_slice(&m.unknown_fields);
Self {
header: 0,
num: typedef::MesgNum::VIDEO,
fields,
developer_fields: m.developer_fields,
}
}
}