Skip to main content

CanwuError

Struct CanwuError 

Source
pub struct CanwuError {
    pub code: ErrorCode,
    pub message: String,
    pub related_entities: Vec<EntityRef>,
}

Fields§

§code: ErrorCode§message: String§related_entities: Vec<EntityRef>

Implementations§

Source§

impl CanwuError

Source

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
Hide additional 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}
Source

pub fn with_entity(self, entity: EntityRef) -> CanwuError

Trait Implementations§

Source§

impl Clone for CanwuError

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl Debug for CanwuError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for CanwuError

Source§

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

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Eq for CanwuError

Source§

impl Error for CanwuError

1.30.0 · Source§

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

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl PartialEq for CanwuError

Source§

fn eq(&self, other: &CanwuError) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for CanwuError

Source§

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
Source§

impl StructuralPartialEq for CanwuError

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.