Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SCError into a union to allow user errors to be u32 #286

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 137 additions & 13 deletions src/next/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub const XDR_FILES_SHA256: [(&str, &str); 12] = [
),
(
"xdr/next/Stellar-contract.x",
"8238ab197e8755ae4691d8a66a7ff4614b54f0622263cafbcc04534dca4f6a3e",
"b00ea4057101f92e61851a79ba10812138ace1243b1a2f5ce65931210190f84a",
),
(
"xdr/next/Stellar-internal.x",
Expand Down Expand Up @@ -7350,34 +7350,146 @@ impl WriteXdr for ScErrorCode {
}
}

// ScError is an XDR Struct defines as:
// ScError is an XDR Union defines as:
//
// struct SCError
// union SCError switch (SCErrorType type)
// {
// SCErrorType type;
// case SCE_CONTRACT:
// uint32 contractCode;
// case SCE_WASM_VM:
// case SCE_CONTEXT:
// case SCE_STORAGE:
// case SCE_OBJECT:
// case SCE_CRYPTO:
// case SCE_EVENTS:
// case SCE_BUDGET:
// case SCE_VALUE:
// case SCE_AUTH:
// SCErrorCode code;
// };
//
// union with discriminant ScErrorType
#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
#[cfg_attr(
all(feature = "serde", feature = "alloc"),
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "snake_case")
)]
pub struct ScError {
pub type_: ScErrorType,
pub code: ScErrorCode,
#[allow(clippy::large_enum_variant)]
pub enum ScError {
Contract(u32),
WasmVm(ScErrorCode),
Context(ScErrorCode),
Storage(ScErrorCode),
Object(ScErrorCode),
Crypto(ScErrorCode),
Events(ScErrorCode),
Budget(ScErrorCode),
Value(ScErrorCode),
Auth(ScErrorCode),
}

impl ScError {
pub const VARIANTS: [ScErrorType; 10] = [
ScErrorType::Contract,
ScErrorType::WasmVm,
ScErrorType::Context,
ScErrorType::Storage,
ScErrorType::Object,
ScErrorType::Crypto,
ScErrorType::Events,
ScErrorType::Budget,
ScErrorType::Value,
ScErrorType::Auth,
];
pub const VARIANTS_STR: [&'static str; 10] = [
"Contract", "WasmVm", "Context", "Storage", "Object", "Crypto", "Events", "Budget",
"Value", "Auth",
];

#[must_use]
pub const fn name(&self) -> &'static str {
match self {
Self::Contract(_) => "Contract",
Self::WasmVm(_) => "WasmVm",
Self::Context(_) => "Context",
Self::Storage(_) => "Storage",
Self::Object(_) => "Object",
Self::Crypto(_) => "Crypto",
Self::Events(_) => "Events",
Self::Budget(_) => "Budget",
Self::Value(_) => "Value",
Self::Auth(_) => "Auth",
}
}

#[must_use]
pub const fn discriminant(&self) -> ScErrorType {
#[allow(clippy::match_same_arms)]
match self {
Self::Contract(_) => ScErrorType::Contract,
Self::WasmVm(_) => ScErrorType::WasmVm,
Self::Context(_) => ScErrorType::Context,
Self::Storage(_) => ScErrorType::Storage,
Self::Object(_) => ScErrorType::Object,
Self::Crypto(_) => ScErrorType::Crypto,
Self::Events(_) => ScErrorType::Events,
Self::Budget(_) => ScErrorType::Budget,
Self::Value(_) => ScErrorType::Value,
Self::Auth(_) => ScErrorType::Auth,
}
}

#[must_use]
pub const fn variants() -> [ScErrorType; 10] {
Self::VARIANTS
}
}

impl Name for ScError {
#[must_use]
fn name(&self) -> &'static str {
Self::name(self)
}
}

impl Discriminant<ScErrorType> for ScError {
#[must_use]
fn discriminant(&self) -> ScErrorType {
Self::discriminant(self)
}
}

impl Variants<ScErrorType> for ScError {
fn variants() -> slice::Iter<'static, ScErrorType> {
Self::VARIANTS.iter()
}
}

impl Union<ScErrorType> for ScError {}

impl ReadXdr for ScError {
#[cfg(feature = "std")]
fn read_xdr<R: Read>(r: &mut DepthLimitedRead<R>) -> Result<Self> {
r.with_limited_depth(|r| {
Ok(Self {
type_: ScErrorType::read_xdr(r)?,
code: ScErrorCode::read_xdr(r)?,
})
let dv: ScErrorType = <ScErrorType as ReadXdr>::read_xdr(r)?;
#[allow(clippy::match_same_arms, clippy::match_wildcard_for_single_variants)]
let v = match dv {
ScErrorType::Contract => Self::Contract(u32::read_xdr(r)?),
ScErrorType::WasmVm => Self::WasmVm(ScErrorCode::read_xdr(r)?),
ScErrorType::Context => Self::Context(ScErrorCode::read_xdr(r)?),
ScErrorType::Storage => Self::Storage(ScErrorCode::read_xdr(r)?),
ScErrorType::Object => Self::Object(ScErrorCode::read_xdr(r)?),
ScErrorType::Crypto => Self::Crypto(ScErrorCode::read_xdr(r)?),
ScErrorType::Events => Self::Events(ScErrorCode::read_xdr(r)?),
ScErrorType::Budget => Self::Budget(ScErrorCode::read_xdr(r)?),
ScErrorType::Value => Self::Value(ScErrorCode::read_xdr(r)?),
ScErrorType::Auth => Self::Auth(ScErrorCode::read_xdr(r)?),
#[allow(unreachable_patterns)]
_ => return Err(Error::Invalid),
};
Ok(v)
})
}
}
Expand All @@ -7386,8 +7498,20 @@ impl WriteXdr for ScError {
#[cfg(feature = "std")]
fn write_xdr<W: Write>(&self, w: &mut DepthLimitedWrite<W>) -> Result<()> {
w.with_limited_depth(|w| {
self.type_.write_xdr(w)?;
self.code.write_xdr(w)?;
self.discriminant().write_xdr(w)?;
#[allow(clippy::match_same_arms)]
match self {
Self::Contract(v) => v.write_xdr(w)?,
Self::WasmVm(v) => v.write_xdr(w)?,
Self::Context(v) => v.write_xdr(w)?,
Self::Storage(v) => v.write_xdr(w)?,
Self::Object(v) => v.write_xdr(w)?,
Self::Crypto(v) => v.write_xdr(w)?,
Self::Events(v) => v.write_xdr(w)?,
Self::Budget(v) => v.write_xdr(w)?,
Self::Value(v) => v.write_xdr(w)?,
Self::Auth(v) => v.write_xdr(w)?,
};
Ok(())
})
}
Expand Down
2 changes: 1 addition & 1 deletion xdr/next
Submodule next updated 1 files
+12 −2 Stellar-contract.x
2 changes: 1 addition & 1 deletion xdr/next-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
aec9d6bc63f01443eb7017d629e28d3279779ae6
34241a1eb81a715cc75777ca1ed028ea1456645b