Skip to content

Commit

Permalink
Release 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
eckz committed Aug 14, 2024
1 parent b01c5eb commit 73c5a56
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 434 deletions.
17 changes: 15 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ env:
CARGO_TERM_COLOR: always

jobs:
# Run cargo clippy -- -D warnings
# Run cargo clippy --all-targets -- -D warnings
check:
strategy:
matrix:
Expand Down Expand Up @@ -50,7 +50,7 @@ jobs:
- name: Run cargo check
run: cargo check --target ${{ matrix.target }}
- name: Run clippy
run: cargo clippy --target ${{ matrix.target }} --tests -- -D warnings
run: cargo clippy --target ${{ matrix.target }} --all-targets -- -D warnings

# Run cargo fmt --all -- --check
format:
Expand All @@ -66,6 +66,17 @@ jobs:
- name: Run cargo fmt
run: cargo fmt --all -- --check

# Run cargo doc --no-deps --examples --all-features --document-private-items
doc-check:
name: Format
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4
- name: Install stable toolchain
uses: dtolnay/rust-toolchain@stable
- name: Run cargo fmt
run: cargo doc --no-deps --examples --all-features --document-private-items

# Run cargo test
test:
Expand All @@ -88,6 +99,8 @@ jobs:
uses: dtolnay/rust-toolchain@stable
- name: Run cargo test
run: cargo test
env:
RUSTDOCFLAGS: "-D warnings"

# Run wasm-pack test
test_wasm:
Expand Down
11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
[package]
name = "bevy_simple_preferences"
version = "0.0.1-dev"
version = "0.1.0"
edition = "2021"
description = "A simple Preferences API for Bevy"
license = "MIT OR Apache-2.0"
categories = ["games", "game-development"]
keywords = ["preferences", "bevy"]
repository = "https://github.com/eckz/bevy_simple_preferences"
rust-version = "1.80.0"

[lints.rust]
unsafe_code = "forbid"
missing_docs = "warn"

[lints.clippy]
doc_markdown = "warn"

[dependencies]
serde = { version = "1.0" }
thiserror = "1.0"
Expand Down
25 changes: 0 additions & 25 deletions LICENSE → LICENSE-APACHE
Original file line number Diff line number Diff line change
Expand Up @@ -174,28 +174,3 @@
of your accepting any such warranty or additional liability.

END OF TERMS AND CONDITIONS

APPENDIX: How to apply the Apache License to your work.

To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
19 changes: 19 additions & 0 deletions LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
39 changes: 17 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
//! relay in a simple preferences abstraction giving the final app crate full control on how
//! and where to store the preferences.
//!
//! This crate is heavily based on the [Bevy Preferences API proposal](https://github.com/bevyengine/bevy/issues/13311).
//!
//! # Examples
//!
//! You first need to define a struct / enum that represents your preferences.
//! ```
//!# use bevy::prelude::*;
//!# use bevy_simple_preferences::*;
//!
//! #[derive(Reflect, Default)]
//! struct ExampleSettings {
//! field_u32: u32,
Expand All @@ -25,12 +25,10 @@
//! ```
//!# use bevy::prelude::*;
//!# use bevy_simple_preferences::*;
//!
//!# #[derive(Reflect, Default)]
//!# struct ExampleSettings {
//!# field_u32: u32,
//!# }
//!
//! App::new()
//! .add_plugins(PreferencesPlugin::persisted_with_app_name("YourAppName"))
//! .register_preferences::<ExampleSettings>();
Expand All @@ -45,12 +43,10 @@
//! ```
//!# use bevy::prelude::*;
//!# use bevy_simple_preferences::*;
//!
//!# #[derive(Reflect, Default)]
//!# struct MyCratePreferences {
//!# some_field: i32,
//!# }
//!
//!# struct MyCratePlugin;
//!
//! impl Plugin for MyCratePlugin {
Expand All @@ -67,10 +63,9 @@
//! ```
//!
//! # Supported Bevy Versions

//! | Bevy | bevy_simple_preferences |
//! | ---- | ----------------------- |
//! | 0.14 | 0.1 |
//!| Bevy | `bevy_simple_preferences` |
//!| ---- | ----------------------- |
//!| 0.14 | 0.1 |
//!
//! # Details
//! [`PreferencesPlugin`] is responsible to define where the preferences are stored,
Expand All @@ -79,33 +74,33 @@
//! ## Storage path
//! By default, the following paths are used to store the preferences
//!
//! |Platform | Value | Example |
//! | ------- | -------------------------------------------------------- | ----------------------------------------- |
//! | Native | [`dirs::preference_dir`]/{app_name}/preferences.toml | /home/alice/.config/MyApp/preferences.toml |
//! | Wasm | LocalStorage:{app_name}_preferences | LocalStorage:MyApp_preferences |
//!|Platform | Value | Example |
//!| ------- | -------------------------------------------------------- | ----------------------------------------- |
//!| Native | `dirs::preference_dir/{app_name}/preferences.toml` | /home/alice/.config/MyApp/preferences.toml |
//!| Wasm | `LocalStorage:{app_name}_preferences` | `LocalStorage:MyApp_preferences` |
//!
//! Final user can personalize this paths by using [`PreferencesPlugin::with_storage_type`] and use any convinient
//! value of [`PreferencesStorageType`].
//!
//! ## Storage format
//!
//! By default, the following formats are used:
//! |Platform | Format | Example |
//! | ------- | ----------- | ----------------------------------------- |
//! | Native | `toml` | [MyPluginPreferences]\nvalue = 3 |
//! | Wasm | `json` | { "MyPluginPreferences": { "value": 3 } } |
//!
//!| Platform | Format | Example |
//!| -------- | ----------- | ------------------------------------------- |
//!| Native | `toml` | `[MyPluginPreferences]\nvalue = 3` |
//!| Wasm | `json` | `{ "MyPluginPreferences": { "value": 3 } }` |
//!
//! A different format (only for native) can be configured by implementing [`crate::storage::fs::FileStorageFormat`];
//!
//! Go to the [`crate::storage::fs::FileStorageFormat`] documentation for more information on how to do it.
//!
//!

use bevy::prelude::*;
use bevy::reflect::FromType;
use std::sync::Arc;
use thiserror::Error;

pub mod reflect_map;
pub mod serializable_map;

mod plugin;
mod registry;
Expand Down Expand Up @@ -157,7 +152,7 @@ pub enum PreferencesStorageType {
/// No storage of any type is used
NoStorage,
/// Default storage is used. In native, a default preferences path, and toml file format will be used.
/// In wasm, LocalStorage will be used
/// In wasm, `LocalStorage` will be used
/// See [`PreferencesPlugin`] for more info.
#[default]
DefaultStorage,
Expand Down Expand Up @@ -247,7 +242,7 @@ pub enum PreferencesSet {
Load,
/// System set used to create resources of type [`crate::resource::Preferences`]
AssignResources,
/// Assign values into [`crate::reflect_map::PreferencesReflectMap`].
/// Assign values into [`crate::serializable_map::PreferencesSerializableMap`].
SetReflectMapValues,
/// System set used to save preferences, it happens on [`Last`].
Save,
Expand Down
33 changes: 16 additions & 17 deletions src/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::reflect_map::PreferencesReflectMap;
use crate::serializable_map::PreferencesSerializableMap;
use crate::storage::{PreferencesStorage, PreferencesStorageResource};
use std::sync::Arc;

Expand Down Expand Up @@ -68,14 +68,14 @@ impl PreferencesStorageBuilder {
}
}

/// Schedule label that is executed before PreStartup
/// Schedule label that is executed before `PreStartup`
#[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash)]
pub struct LoadPreferences;

/// Preferences Plugin that configures how preferences are stored.
/// It should be only added by final applications, not libraries, since it's not their responsibility.
///
/// If you want to persist the preferences, you need to include an app_name that
/// If you want to persist the preferences, you need to include an `app_name` that
/// uniquely represents your application
/// ```
/// # use bevy::prelude::*;
Expand All @@ -96,7 +96,7 @@ pub struct LoadPreferences;
/// ```

pub struct PreferencesPlugin {
/// Name of the application, required unless storage_type is [`PreferencesStorageType::NoStorage`]
/// Name of the application, required unless `storage_type` is [`PreferencesStorageType::NoStorage`]
pub app_name: Option<&'static str>,
/// Organization name, optional. It will be used to construct the final file name.
pub org_name: Option<&'static str>,
Expand All @@ -109,8 +109,8 @@ impl PreferencesPlugin {
///
/// |Platform | Value | Example |
/// | ------- | -------------------------------------------------------- | ----------------------------------------- |
/// | Native | [`dirs::preference_dir`]/{app_name}/preferences.toml | /home/alice/.config/MyApp/preferences.toml |
/// | Wasm | LocalStorage:{app_name}_preferences | LocalStorage:MyApp_preferences |
/// | Native | `{dirs::preference_dir}/{app_name}/preferences.toml` | /home/alice/.config/MyApp/preferences.toml |
/// | Wasm | `LocalStorage:{app_name}_preferences` | `LocalStorage:MyApp_preferences` |
///
pub fn persisted_with_app_name(app_name: &'static str) -> Self {
Self {
Expand Down Expand Up @@ -140,17 +140,17 @@ impl PreferencesPlugin {
/// ```
/// # use bevy::prelude::*;
/// # use bevy_simple_preferences::PreferencesPlugin;
/// # use bevy_simple_preferences::reflect_map::{PreferencesReflectMap, PreferencesReflectMapDeserializeSeed};
/// # use bevy_simple_preferences::serializable_map::{PreferencesSerializableMap, PreferencesSerializableMapSeed};
/// # use bevy_simple_preferences::storage::PreferencesStorage;
///
/// struct MyCustomStorage;
///
/// impl PreferencesStorage for MyCustomStorage {
/// fn load_preferences(&self, deserialize_seed: PreferencesReflectMapDeserializeSeed) -> Result<PreferencesReflectMap, bevy_simple_preferences::PreferencesError> {
/// fn load_preferences(&self, deserialize_seed: PreferencesSerializableMapSeed) -> Result<PreferencesSerializableMap, bevy_simple_preferences::PreferencesError> {
/// todo!()
/// }
///
/// fn save_preferences(&self, map: &PreferencesReflectMap) -> Result<(), bevy_simple_preferences::PreferencesError> {
/// fn save_preferences(&self, map: &PreferencesSerializableMap) -> Result<(), bevy_simple_preferences::PreferencesError> {
/// todo!()
/// }
/// }
Expand Down Expand Up @@ -193,8 +193,7 @@ impl Plugin for PreferencesPlugin {
world.add_schedule(schedule);
}

app.register_type::<PreferencesReflectMap>()
.add_event::<PreferencesSaved>()
app.add_event::<PreferencesSaved>()
.add_systems(
LoadPreferences,
load_preferences(self.storage_builder()).in_set(PreferencesSet::Load),
Expand All @@ -208,7 +207,7 @@ impl Plugin for PreferencesPlugin {
Last,
save_preferences.in_set(PreferencesSet::Save).run_if(
resource_exists::<PreferencesStorageResource>
.and_then(resource_exists::<PreferencesReflectMap>),
.and_then(resource_exists::<PreferencesSerializableMap>),
),
);
}
Expand All @@ -223,7 +222,7 @@ fn load_preferences(
return;
};

let seed = PreferencesReflectMap::deserialize_seed(type_registry_arc.clone());
let seed = PreferencesSerializableMap::deserialize_seed(type_registry_arc.clone());

let preferences = match storage.load_preferences(seed) {
Ok(preferences) => preferences,
Expand All @@ -232,15 +231,15 @@ fn load_preferences(
if io_error.kind() != std::io::ErrorKind::NotFound {
error!("I/O Error loading preferences: {io_error}");
}
PreferencesReflectMap::empty(type_registry_arc)
PreferencesSerializableMap::empty(type_registry_arc)
}
#[cfg(target_family = "wasm")]
Err(crate::PreferencesError::GlooError(
gloo_storage::errors::StorageError::KeyNotFound(_),
)) => PreferencesReflectMap::empty(type_registry_arc),
)) => PreferencesSerializableMap::empty(type_registry_arc),
Err(err) => {
error!("Unknown Error loading preferences: {err:?}");
PreferencesReflectMap::empty(type_registry_arc)
PreferencesSerializableMap::empty(type_registry_arc)
}
};

Expand All @@ -256,7 +255,7 @@ pub struct PreferencesSaved;
#[allow(clippy::too_many_arguments)]
pub fn save_preferences(
time: Res<Time<Real>>,
preferences: Res<PreferencesReflectMap>,
preferences: Res<PreferencesSerializableMap>,
storage: Res<PreferencesStorageResource>,
mut last_save_tick: Local<Option<Tick>>,
mut last_save_time: Local<Duration>,
Expand Down
Loading

0 comments on commit 73c5a56

Please sign in to comment.