lune/packages/cli/src/gen/definitions/item.rs

115 lines
2.9 KiB
Rust
Raw Normal View History

2023-02-21 18:47:04 +00:00
use std::cmp::Ordering;
use serde::{Deserialize, Serialize};
use super::kind::DefinitionsItemKind;
2023-02-21 18:47:04 +00:00
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DefinitionsItem {
2023-02-22 09:48:04 +00:00
#[serde(skip_serializing_if = "skip_serialize_is_false")]
pub(super) exported: bool,
pub(super) kind: DefinitionsItemKind,
2023-02-21 20:06:11 +00:00
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) meta: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
2023-02-21 18:47:04 +00:00
pub(super) value: Option<String>,
2023-02-21 20:06:11 +00:00
#[serde(skip_serializing_if = "Vec::is_empty")]
pub(super) children: Vec<DefinitionsItem>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub(super) arg_types: Vec<String>,
2023-02-21 18:47:04 +00:00
}
2023-02-22 09:48:04 +00:00
#[allow(clippy::trivially_copy_pass_by_ref)]
fn skip_serialize_is_false(b: &bool) -> bool {
!b
}
impl PartialOrd for DefinitionsItem {
2023-02-21 18:47:04 +00:00
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match self.kind.partial_cmp(&other.kind).unwrap() {
Ordering::Equal => {}
ord => return Some(ord),
}
match self.name.partial_cmp(&other.name).unwrap() {
Ordering::Equal => {}
ord => return Some(ord),
}
match (&self.value, &other.value) {
(Some(value_self), Some(value_other)) => {
match value_self.partial_cmp(value_other).unwrap() {
Ordering::Equal => {}
ord => return Some(ord),
}
}
(Some(_), None) => return Some(Ordering::Less),
(None, Some(_)) => return Some(Ordering::Greater),
(None, None) => {}
}
Some(Ordering::Equal)
}
}
impl Ord for DefinitionsItem {
2023-02-21 18:47:04 +00:00
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).unwrap()
}
}
#[allow(dead_code)]
impl DefinitionsItem {
2023-02-22 09:48:04 +00:00
pub fn is_exported(&self) -> bool {
self.exported
}
2023-02-21 18:47:04 +00:00
pub fn is_root(&self) -> bool {
self.kind.is_root()
}
2023-02-22 09:48:04 +00:00
pub fn is_table(&self) -> bool {
self.kind.is_table()
}
2023-02-21 18:47:04 +00:00
pub fn is_property(&self) -> bool {
self.kind.is_property()
}
pub fn is_function(&self) -> bool {
self.kind.is_function()
}
pub fn is_description(&self) -> bool {
self.kind.is_description()
}
pub fn is_tag(&self) -> bool {
self.kind.is_tag()
}
pub fn kind(&self) -> DefinitionsItemKind {
self.kind
}
2023-02-21 20:06:11 +00:00
pub fn get_name(&self) -> Option<&str> {
self.name.as_deref()
}
pub fn get_meta(&self) -> Option<&str> {
self.meta.as_deref()
2023-02-21 18:47:04 +00:00
}
pub fn get_value(&self) -> Option<&str> {
self.value.as_deref()
}
pub fn children(&self) -> &[DefinitionsItem] {
&self.children
2023-02-21 18:47:04 +00:00
}
pub fn arg_types(&self) -> Vec<&str> {
self.arg_types.iter().map(String::as_str).collect()
}
2023-02-21 18:47:04 +00:00
}