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/// Wrapper around `Path` with a more human-readable, collapsed version of
100/// `Path`.
101pub(crate) struct PrettyPath<'x>(pub &'x Path);
102
103impl fmt::Display for PrettyPath<'_> {
104    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
105        let mut first = self.0.leading_colon.is_none();
106        for segment in self.0.segments.iter() {
107            if !first {
108                f.write_str("::")?;
109            }
110            write!(f, "{}", segment.ident)?;
111            first = false;
112        }
113        Ok(())
114    }
115}
116
117/// Ephemeral struct to create a nice human-readable representation of
118/// [`syn::Member`].
119///
120/// It implements [`core::fmt::Display`] for that purpose and is otherwise of
121/// little use.
122#[repr(transparent)]
123pub(crate) struct FieldName<'x>(pub &'x Member);
124
125impl fmt::Display for FieldName<'_> {
126    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
127        match self.0 {
128            Member::Named(v) => write!(f, "field '{}'", v),
129            Member::Unnamed(v) => write!(f, "unnamed field {}", v.index),
130        }
131    }
132}
133
134/// Create a string error message for a missing attribute.
135///
136/// `parent_name` should point at the compound which is being parsed and
137/// `field` should be the field to which the attribute belongs.
138pub(super) fn on_missing_attribute(parent_name: &ParentRef, field: &Member) -> String {
139    format!(
140        "Required attribute {} on {} missing.",
141        FieldName(field),
142        parent_name
143    )
144}
145
146/// Create a string error message for a missing child element.
147///
148/// `parent_name` should point at the compound which is being parsed and
149/// `field` should be the field to which the child belongs.
150pub(super) fn on_missing_child(parent_name: &ParentRef, field: &Member) -> String {
151    format!("Missing child {} in {}.", FieldName(field), parent_name)
152}
153
154/// Create a string error message for a duplicate child element.
155///
156/// `parent_name` should point at the compound which is being parsed and
157/// `field` should be the field to which the child belongs.
158pub(super) fn on_duplicate_child(parent_name: &ParentRef, field: &Member) -> String {
159    format!(
160        "{} must not have more than one child in {}.",
161        parent_name,
162        FieldName(field)
163    )
164}