use core::fmt;
use alloc::vec::Vec;
use super::{Field, List};
#[derive(Debug, Clone, Eq)]
pub struct CustomObject<'a> {
name: &'a str,
fields: List<'a, Field<'a>>,
comments: List<'a, super::Comment<'a>>,
}
impl<'a> CustomObject<'a> {
pub const fn new(
name: &'a str,
fields: &'a [&'a Field<'a>],
comments: &'a [&'a super::Comment<'a>],
) -> Self {
Self {
name,
fields: List::Borrowed(fields),
comments: List::Borrowed(comments),
}
}
pub fn new_owned(
name: &'a str,
fields: Vec<Field<'a>>,
comments: Vec<super::Comment<'a>>,
) -> Self {
Self {
name,
fields: List::from(fields),
comments: List::from(comments),
}
}
pub fn name(&self) -> &'a str {
self.name
}
pub fn fields(&self) -> impl Iterator<Item = &Field<'a>> {
self.fields.iter()
}
pub fn comments(&self) -> impl Iterator<Item = &super::Comment<'a>> {
self.comments.iter()
}
}
impl<'a> fmt::Display for CustomObject<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for comment in self.comments.iter() {
writeln!(f, "{comment}")?;
}
write!(f, "type {} (", self.name)?;
let mut first = true;
for field in self.fields.iter() {
if !first {
write!(f, ", ")?;
}
first = false;
write!(f, "{field}")?;
}
write!(f, ")")
}
}
impl<'a> PartialEq for CustomObject<'a> {
fn eq(&self, other: &Self) -> bool {
self.name == other.name && self.fields == other.fields
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::idl::{Comment, Field, Type};
use core::fmt::Write;
#[test]
fn display_with_comments() {
let comment1 = Comment::new("User data structure");
let comment2 = Comment::new("Contains basic user information");
let comments = [&comment1, &comment2];
let name_field = Field::new("name", &Type::String, &[]);
let age_field = Field::new("age", &Type::Int, &[]);
let fields = [&name_field, &age_field];
let custom_object = CustomObject::new("User", &fields, &comments);
let mut displayed = String::new();
write!(&mut displayed, "{}", custom_object).unwrap();
assert_eq!(
displayed,
"# User data structure\n# Contains basic user information\ntype User (name: string, age: int)"
);
}
}