foundry_cheatcodes_spec/
items.rs

1use serde::{Deserialize, Serialize};
2use std::{borrow::Cow, fmt};
3
4/// A Solidity custom error.
5#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
6#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
7#[serde(rename_all = "camelCase")]
8pub struct Error<'a> {
9    /// The name of the error.
10    pub name: &'a str,
11    /// The description of the error.
12    /// This is a markdown string derived from the NatSpec documentation.
13    pub description: &'a str,
14    /// The Solidity error declaration, including full type, parameter names, etc.
15    pub declaration: &'a str,
16}
17
18impl fmt::Display for Error<'_> {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        f.write_str(self.declaration)
21    }
22}
23
24/// A Solidity event.
25#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
26#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
27#[serde(rename_all = "camelCase")]
28pub struct Event<'a> {
29    /// The name of the event.
30    pub name: &'a str,
31    /// The description of the event.
32    /// This is a markdown string derived from the NatSpec documentation.
33    pub description: &'a str,
34    /// The Solidity event declaration, including full type, parameter names, etc.
35    pub declaration: &'a str,
36}
37
38impl fmt::Display for Event<'_> {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        f.write_str(self.declaration)
41    }
42}
43
44/// A Solidity enumeration.
45#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
46#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
47#[serde(rename_all = "camelCase")]
48pub struct Enum<'a> {
49    /// The name of the enum.
50    pub name: &'a str,
51    /// The description of the enum.
52    /// This is a markdown string derived from the NatSpec documentation.
53    pub description: &'a str,
54    /// The variants of the enum.
55    #[serde(borrow)]
56    pub variants: Cow<'a, [EnumVariant<'a>]>,
57}
58
59impl fmt::Display for Enum<'_> {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        write!(f, "enum {} {{ ", self.name)?;
62        for (i, variant) in self.variants.iter().enumerate() {
63            if i > 0 {
64                f.write_str(", ")?;
65            }
66            f.write_str(variant.name)?;
67        }
68        f.write_str(" }")
69    }
70}
71
72/// A variant of an [`Enum`].
73#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
74#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
75#[serde(rename_all = "camelCase")]
76pub struct EnumVariant<'a> {
77    /// The name of the variant.
78    pub name: &'a str,
79    /// The description of the variant.
80    /// This is a markdown string derived from the NatSpec documentation.
81    pub description: &'a str,
82}
83
84/// A Solidity struct.
85#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
86#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
87#[serde(rename_all = "camelCase")]
88pub struct Struct<'a> {
89    /// The name of the struct.
90    pub name: &'a str,
91    /// The description of the struct.
92    /// This is a markdown string derived from the NatSpec documentation.
93    pub description: &'a str,
94    /// The fields of the struct.
95    #[serde(borrow)]
96    pub fields: Cow<'a, [StructField<'a>]>,
97}
98
99impl fmt::Display for Struct<'_> {
100    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101        write!(f, "struct {} {{ ", self.name)?;
102        for field in self.fields.iter() {
103            write!(f, "{} {}; ", field.ty, field.name)?;
104        }
105        f.write_str("}")
106    }
107}
108
109/// A [`Struct`] field.
110#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
111#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
112#[serde(rename_all = "camelCase")]
113pub struct StructField<'a> {
114    /// The name of the field.
115    pub name: &'a str,
116    /// The type of the field.
117    pub ty: &'a str,
118    /// The description of the field.
119    /// This is a markdown string derived from the NatSpec documentation.
120    pub description: &'a str,
121}