pub struct CanwuError {
pub code: ErrorCode,
pub message: String,
pub related_entities: Vec<EntityRef>,
}Fields§
§code: ErrorCode§message: StringImplementations§
Source§impl CanwuError
impl CanwuError
Sourcepub fn new(code: ErrorCode, message: impl Into<String>) -> CanwuError
pub fn new(code: ErrorCode, message: impl Into<String>) -> CanwuError
Examples found in repository?
examples/plugin.rs (lines 23-26)
11fn set_stance(
12 view: &SimulationView<'_>,
13 context: &CommandContext,
14 payload: &Value,
15) -> Result<Vec<SystemDirective>, CanwuError> {
16 let army = ArmyId::new(
17 payload
18 .get("army")
19 .and_then(Value::as_u64)
20 .expect("payload was validated before the handler ran"),
21 );
22 let Some(army_state) = view.army(army)? else {
23 return Err(CanwuError::new(
24 ErrorCode::ArmyNotFound,
25 format!("army {army} was not found"),
26 ));
27 };
28 if context.issuer != Issuer::Actor(army_state.commander) {
29 return Err(CanwuError::new(
30 ErrorCode::InvalidAuthority,
31 "only the army commander may set its stance",
32 ));
33 }
34 Ok(vec![SystemDirective::SetComponent {
35 state: StateKey::new("military", "stance"),
36 entity: EntityRef::Army(army),
37 component: "stance".to_owned(),
38 value: payload["stance"].clone(),
39 summary: format!("Army {army} changed stance"),
40 }])
41}More examples
examples/phased_boundary.rs (lines 54-57)
48fn apply_grain(
49 view: &SimulationView<'_>,
50 _context: &BoundaryContext,
51) -> Result<BoundaryProposal, CanwuError> {
52 let reservation = ReservationRef::new("example-demand", "request", "daily-grain");
53 let allocation = view.reservation(&reservation)?.ok_or_else(|| {
54 CanwuError::new(
55 ErrorCode::InvalidBoundary,
56 "the declared grain reservation did not produce allocation evidence",
57 )
58 })?;
59 let territory = EntityRef::Territory(TerritoryId::new(1));
60 Ok(BoundaryProposal {
61 directives: vec![
62 BoundaryDirective::SetComponent {
63 state: StateKey::new("garrison", "grain"),
64 entity: territory.clone(),
65 component: "daily_grant".to_owned(),
66 value: Value::from(allocation.granted),
67 summary: format!(
68 "Granted {} grain to the western garrison",
69 allocation.granted
70 ),
71 },
72 BoundaryDirective::Emit {
73 event_type: "grain_allocated".to_owned(),
74 summary: "The daily grain allocation settled".to_owned(),
75 affected: vec![territory],
76 },
77 ],
78 ..BoundaryProposal::default()
79 })
80}pub fn with_entity(self, entity: EntityRef) -> CanwuError
Trait Implementations§
Source§impl Clone for CanwuError
impl Clone for CanwuError
Source§fn clone(&self) -> CanwuError
fn clone(&self) -> CanwuError
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for CanwuError
impl Debug for CanwuError
Source§impl<'de> Deserialize<'de> for CanwuError
impl<'de> Deserialize<'de> for CanwuError
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<CanwuError, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<CanwuError, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl Display for CanwuError
impl Display for CanwuError
impl Eq for CanwuError
Source§impl Error for CanwuError
impl Error for CanwuError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0:
use the Display impl or to_string()
Source§impl PartialEq for CanwuError
impl PartialEq for CanwuError
Source§impl Serialize for CanwuError
impl Serialize for CanwuError
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Serialize this value into the given Serde serializer. Read more
impl StructuralPartialEq for CanwuError
Auto Trait Implementations§
impl Freeze for CanwuError
impl RefUnwindSafe for CanwuError
impl Send for CanwuError
impl Sync for CanwuError
impl Unpin for CanwuError
impl UnsafeUnpin for CanwuError
impl UnwindSafe for CanwuError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more