use alloc::{boxed::Box, vec::Vec};
use syn::{Generics, Ident, Path, Type};
use crate::{
field::{FieldId, Fields},
format::Resolved,
source::Source,
syntax::{Import, Inline},
};
#[derive(Clone, Debug)]
pub enum Target {
Struct(Box<Structure>),
Enum(Enumeration),
}
#[derive(Clone, Debug)]
pub struct Header {
inline: Option<Inline>,
import: Option<Import>,
name: Ident,
generics: Generics,
}
impl Header {
#[must_use]
#[inline]
pub const fn new(inline: Option<Inline>, import: Option<Import>, name: Ident, generics: Generics) -> Self {
Self {
inline,
import,
name,
generics,
}
}
#[inline]
pub fn parts(self) -> (Option<Inline>, Option<Import>, Ident, Generics) {
let Self {
inline,
import,
name,
generics,
} = self;
(inline, import, name, generics)
}
}
#[derive(Clone, Debug)]
pub struct Structure {
header: Header,
fields: Fields,
display: Display,
source: ErrorSource,
conversion: Option<Conversion>,
}
impl Structure {
#[must_use]
#[inline]
pub const fn new(header: Header, fields: Fields, display: Display, source: ErrorSource, conversion: Option<Conversion>) -> Self {
Self {
header,
fields,
display,
source,
conversion,
}
}
#[inline]
pub fn parts(self) -> (Header, Fields, Display, ErrorSource, Option<Conversion>) {
let Self {
header,
fields,
display,
source,
conversion,
} = self;
(header, fields, display, source, conversion)
}
}
#[derive(Clone, Debug)]
pub struct Enumeration {
header: Header,
variants: Vec<Variant>,
}
impl Enumeration {
#[inline]
#[must_use]
pub const fn new(header: Header, variants: Vec<Variant>) -> Self {
Self { header, variants }
}
#[inline]
pub fn parts(self) -> (Header, Vec<Variant>) {
let Self { header, variants } = self;
(header, variants)
}
}
#[derive(Clone, Debug)]
pub struct Variant {
name: Ident,
fields: Fields,
display: Display,
source: ErrorSource,
conversion: Option<Conversion>,
}
impl Variant {
#[must_use]
#[inline]
pub const fn new(name: Ident, fields: Fields, display: Display, source: ErrorSource, conversion: Option<Conversion>) -> Self {
Self {
name,
fields,
display,
source,
conversion,
}
}
#[inline]
#[must_use]
pub const fn name(&self) -> &Ident {
let Self { name, .. } = self;
name
}
#[inline]
#[must_use]
pub const fn fields(&self) -> &Fields {
let Self { fields, .. } = self;
fields
}
#[inline]
#[must_use]
pub const fn display(&self) -> &Display {
let Self { display, .. } = self;
display
}
#[inline]
#[must_use]
pub const fn source(&self) -> &ErrorSource {
let Self { source, .. } = self;
source
}
#[inline]
#[must_use]
pub const fn conversion(&self) -> Option<&Conversion> {
let Self { conversion, .. } = self;
conversion.as_ref()
}
}
#[derive(Clone, Debug)]
pub enum Display {
Format(Resolved),
Transparent(FieldId),
Custom(Path),
}
#[derive(Clone, Debug)]
pub enum ErrorSource {
None,
Field(Source),
Transparent(Source),
}
#[derive(Clone, Debug)]
pub struct Conversion {
field: FieldId,
field_type: Type,
}
impl Conversion {
#[inline]
#[must_use]
pub const fn new(field: FieldId, field_type: Type) -> Self {
Self { field, field_type }
}
#[inline]
#[must_use]
pub const fn field(&self) -> FieldId {
let Self { field, .. } = self;
*field
}
#[inline]
#[must_use]
pub const fn field_type(&self) -> &Type {
let Self { field_type, .. } = self;
field_type
}
}