peace_core/lib.rs
1//! Low level data types for the peace automation framework.
2//!
3//! This crate exists because:
4//!
5//! * `peace_cfg` has a dependency on `peace_resource_rt` for `Resources`, used
6//! in `Item::setup`.
7//! * `peace_resource_rt` has a dependency on `ItemId`, as uses `TypeMap<ItemId,
8//! _>` for the `States` maps.
9//!
10//! When [peace#67] is implemented, the `progress` module can be moved out
11//! of `peace_core` into `peace_cfg`.
12//!
13//! [peace#67]: https://github.com/azriel91/peace/issues/67
14
15pub extern crate id_newtype;
16
17// Re-exports
18// needed for dependencies' usage of our `id_newtype` macro to resolve
19pub use peace_fmt;
20pub use peace_static_check_macros::{app_name, profile};
21
22pub use crate::app_name::{AppName, AppNameInvalidFmt};
23
24mod app_name;
25
26/// Implements common behaviour for an ID type.
27///
28/// The implemented behaviour includes:
29///
30/// * `IdType::new`
31/// * `IdType::new_unchecked`
32/// * `IdType::is_valid_id`
33/// * `std::ops::Deref`
34/// * `std::ops::DerefMut`
35/// * `std::fmt::Display`
36/// * `std::str::FromStr`
37/// * `TryFrom<String>`
38/// * `TryFrom<&'static str>`
39/// * `peace_fmt::Presentable`
40///
41/// A separate error type is also generated, which indicates an invalid value
42/// when the ID type is instantiated with `new`.
43///
44/// # Usage
45///
46/// ```rust
47/// use std::borrow::Cow;
48///
49/// // replace this with your ID type's macro
50/// use peace_static_check_macros::my_id_type;
51/// use serde::{Deserialize, Serialize};
52///
53/// // Rename your ID type
54/// #[derive(Clone, Debug, Hash, PartialEq, Eq, Deserialize, Serialize)]
55/// pub struct MyIdType(Cow<'static, str>);
56///
57/// crate::core_id_newtype!(
58/// MyIdType, // Name of the ID type
59/// MyIdTypeInvalidFmt, // Name of the invalid value error
60/// my_id_type, // Name of the static check macro
61/// tag, // The `peace_fmt::Presentable` method to style the ID
62/// );
63/// ```
64#[macro_export]
65macro_rules! id_newtype {
66 ($ty_name:ident, $ty_err_name:ident, $macro_name:ident, $presentable_method:ident) => {
67 use $crate::id_newtype::id_newtype;
68
69 $crate::id_newtype::id_newtype!($ty_name, $ty_err_name, $macro_name);
70
71 #[$crate::peace_fmt::async_trait(?Send)]
72 impl $crate::peace_fmt::Presentable for $ty_name {
73 async fn present<'output, PR>(&self, presenter: &mut PR) -> Result<(), PR::Error>
74 where
75 PR: $crate::peace_fmt::Presenter<'output>,
76 {
77 presenter.$presentable_method(self.as_str()).await
78 }
79 }
80 };
81}