ethers_solc/artifacts/ast/
misc.rs

1use serde::{Deserialize, Serialize};
2use std::{fmt, fmt::Write, str::FromStr};
3
4/// Represents the source location of a node: `<start byte>:<length>:<source index>`.
5///
6/// The `start`, `length` and `index` can be -1 which is represented as `None`
7#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
8pub struct SourceLocation {
9    pub start: Option<usize>,
10    pub length: Option<usize>,
11    pub index: Option<usize>,
12}
13
14impl FromStr for SourceLocation {
15    type Err = String;
16
17    fn from_str(s: &str) -> Result<Self, Self::Err> {
18        let invalid_location = move || format!("{s} invalid source location");
19
20        let mut split = s.split(':');
21        let start = split
22            .next()
23            .ok_or_else(invalid_location)?
24            .parse::<isize>()
25            .map_err(|_| invalid_location())?;
26        let length = split
27            .next()
28            .ok_or_else(invalid_location)?
29            .parse::<isize>()
30            .map_err(|_| invalid_location())?;
31        let index = split
32            .next()
33            .ok_or_else(invalid_location)?
34            .parse::<isize>()
35            .map_err(|_| invalid_location())?;
36
37        let start = if start < 0 { None } else { Some(start as usize) };
38        let length = if length < 0 { None } else { Some(length as usize) };
39        let index = if index < 0 { None } else { Some(index as usize) };
40
41        Ok(Self { start, length, index })
42    }
43}
44
45impl fmt::Display for SourceLocation {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        if let Some(start) = self.start {
48            start.fmt(f)?;
49        } else {
50            f.write_str("-1")?;
51        }
52        f.write_char(':')?;
53        if let Some(length) = self.length {
54            length.fmt(f)?;
55        } else {
56            f.write_str("-1")?;
57        }
58        f.write_char(':')?;
59        if let Some(index) = self.index {
60            index.fmt(f)?;
61        } else {
62            f.write_str("-1")?;
63        }
64        Ok(())
65    }
66}
67
68/// Function mutability specifier.
69#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
70#[serde(rename_all = "lowercase")]
71pub enum StateMutability {
72    Payable,
73    Pure,
74    Nonpayable,
75    View,
76}
77
78/// Variable mutability specifier.
79#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
80#[serde(rename_all = "lowercase")]
81pub enum Mutability {
82    Mutable,
83    Immutable,
84    Constant,
85}
86
87/// Storage location specifier.
88#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
89#[serde(rename_all = "lowercase")]
90pub enum StorageLocation {
91    Calldata,
92    Default,
93    Memory,
94    Storage,
95}
96
97/// Visibility specifier.
98#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
99#[serde(rename_all = "lowercase")]
100pub enum Visibility {
101    External,
102    Public,
103    Internal,
104    Private,
105}
106
107/// A type description.
108#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
109#[serde(rename_all = "camelCase")]
110pub struct TypeDescriptions {
111    pub type_identifier: Option<String>,
112    pub type_string: Option<String>,
113}