xso_proc/
error_message.rs

1// Copyright (c) 2024 Jonas Schäfer <jonas@zombofant.net>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7//! Infrastructure for contextual error messages
8
9use core::fmt;
10
11use syn::*;
12
13/// Reference to a compound field's parent
14///
15/// This reference can be converted to a hopefully-useful human-readable
16/// string via [`core::fmt::Display`].
17#[derive(Clone, Debug)]
18pub(super) enum ParentRef {
19    /// The parent is addressable by a path, e.g. a struct type or enum
20    /// variant.
21    Named(Path),
22
23    /// The parent is not addressable by a path, because it is in fact an
24    /// ephemeral structure.
25    ///
26    /// Used to reference the ephemeral structures used by fields declared
27    /// with `#[xml(extract(..))]`.
28    Unnamed {
29        /// The parent's ref.
30        ///
31        /// For extracts, this refers to the compound where the field with
32        /// the extract is declared.
33        parent: Box<ParentRef>,
34
35        /// The field inside that parent.
36        ///
37        /// For extracts, this refers to the compound field where the extract
38        /// is declared.
39        field: Member,
40    },
41}
42
43impl From<Path> for ParentRef {
44    fn from(other: Path) -> Self {
45        Self::Named(other)
46    }
47}
48
49impl From<&Path> for ParentRef {
50    fn from(other: &Path) -> Self {
51        Self::Named(other.clone())
52    }
53}
54
55impl fmt::Display for ParentRef {
56    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
57        match self {
58            Self::Named(name) => {
59                let mut first = true;
60                for segment in name.segments.iter() {
61                    if !first || name.leading_colon.is_some() {
62                        write!(f, "::")?;
63                    }
64                    first = false;
65                    write!(f, "{}", segment.ident)?;
66                }
67                write!(f, " element")
68            }
69            Self::Unnamed { parent, field } => {
70                write!(f, "extraction for {} in {}", FieldName(field), parent)
71            }
72        }
73    }
74}
75
76impl ParentRef {
77    /// Create a new `ParentRef` for a member inside this one.
78    ///
79    /// Returns a [`Self::Unnamed`] with `self` as parent and `member` as
80    /// field.
81    pub(crate) fn child(&self, member: Member) -> Self {
82        match self {
83            Self::Named { .. } | Self::Unnamed { .. } => Self::Unnamed {
84                parent: Box::new(self.clone()),
85                field: member,
86            },
87        }
88    }
89
90    /// Return true if and only if this ParentRef can be addressed as a path.
91    pub(crate) fn is_path(&self) -> bool {
92        match self {
93            Self::Named { .. } => true,
94            Self::Unnamed { .. } => false,
95        }
96    }
97}
98
99/// Ephemeral struct to create a nice human-readable representation of
100/// [`syn::Member`].
101///
102/// It implements [`core::fmt::Display`] for that purpose and is otherwise of
103/// little use.
104#[repr(transparent)]
105pub(crate) struct FieldName<'x>(pub &'x Member);
106
107impl fmt::Display for FieldName<'_> {
108    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
109        match self.0 {
110            Member::Named(v) => write!(f, "field '{}'", v),
111            Member::Unnamed(v) => write!(f, "unnamed field {}", v.index),
112        }
113    }
114}
115
116/// Create a string error message for a missing attribute.
117///
118/// `parent_name` should point at the compound which is being parsed and
119/// `field` should be the field to which the attribute belongs.
120pub(super) fn on_missing_attribute(parent_name: &ParentRef, field: &Member) -> String {
121    format!(
122        "Required attribute {} on {} missing.",
123        FieldName(field),
124        parent_name
125    )
126}
127
128/// Create a string error message for a missing child element.
129///
130/// `parent_name` should point at the compound which is being parsed and
131/// `field` should be the field to which the child belongs.
132pub(super) fn on_missing_child(parent_name: &ParentRef, field: &Member) -> String {
133    format!("Missing child {} in {}.", FieldName(&field), parent_name)
134}
135
136/// Create a string error message for a duplicate child element.
137///
138/// `parent_name` should point at the compound which is being parsed and
139/// `field` should be the field to which the child belongs.
140pub(super) fn on_duplicate_child(parent_name: &ParentRef, field: &Member) -> String {
141    format!(
142        "{} must not have more than one child in {}.",
143        parent_name,
144        FieldName(&field)
145    )
146}