Skip to content

Commit

Permalink
license, lint, ci updates
Browse files Browse the repository at this point in the history
  • Loading branch information
andybarron committed Jul 9, 2024
1 parent 1315825 commit 603a7e9
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 75 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/ci2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: "Continuous integration"
on:
push:
pull_request:

jobs:
ci:
name: CI checks
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- stable
- beta
- nightly
- 1.61.0 # TODO: get this from Cargo.toml automatically?
steps:
- uses: actions/checkout@v4
- uses: actions-rust-lang/setup-rust-toolchain@v1
- run: cargo install cargo-binstall
- run: cargo binstall just cargo-tarpaulin
- run: just format-ci
- run: just check-ci
- run: just test-ci
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ documentation = "https://docs.rs/preferences"
repository = "https://github.com/AndyBarron/preferences-rs"
readme = "README.md"
keywords = ["preferences", "user", "data", "persistent", "storage"]
categories = ["configuration"]
license = "MIT-0"

[dependencies]
Expand Down
13 changes: 13 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
release:
cargo release

# CI tasks

check-ci:
cargo clippy --all-targets -- -Dwarnings

format-ci:
cargo fmt -- --check --verbose

test-ci:
cargo tarpaulin -v --all-features --ignore-tests --out Lcov
31 changes: 13 additions & 18 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
The MIT License (MIT)
MIT No Attribution

Copyright (c) 2016 Andy Barron
Copyright 2024 Andy Barron

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:
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.

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.
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.
42 changes: 0 additions & 42 deletions Makefile.toml

This file was deleted.

41 changes: 26 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
#![allow(clippy::enum_glob_use)]
//! *Read and write user-specific application data*
//!
//! This crate allows Rust developers to store and retrieve user-local preferences and other
Expand Down Expand Up @@ -170,21 +172,21 @@ extern crate app_dirs;
extern crate serde;
extern crate serde_json;

use app_dirs::{get_app_dir, get_data_root, AppDataType};
pub use app_dirs::{AppDirsError, AppInfo};
use app_dirs::{AppDataType, get_data_root, get_app_dir};
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::collections::HashMap;
use std::ffi::OsString;
use std::fmt;
use std::fs::{File, create_dir_all};
use std::fs::{create_dir_all, File};
use std::io::{self, ErrorKind, Read, Write};
use std::path::PathBuf;
use std::string::FromUtf8Error;

const DATA_TYPE: AppDataType = AppDataType::UserConfig;
static PREFS_FILE_EXTENSION: &'static str = ".prefs.json";
static DEFAULT_PREFS_FILENAME: &'static str = "prefs.json";
static PREFS_FILE_EXTENSION: &str = ".prefs.json";
static DEFAULT_PREFS_FILENAME: &str = "prefs.json";

/// Generic key-value store for user data.
///
Expand Down Expand Up @@ -235,7 +237,7 @@ impl std::error::Error for PreferencesError {

impl From<serde_json::Error> for PreferencesError {
fn from(e: serde_json::Error) -> Self {
PreferencesError::Json(e)
Self::Json(e)
}
}

Expand All @@ -244,19 +246,19 @@ impl From<FromUtf8Error> for PreferencesError {
let kind = ErrorKind::InvalidData;
let msg = "Preferences file contained invalid UTF-8";
let err = io::Error::new(kind, msg);
PreferencesError::Io(err)
Self::Io(err)
}
}

impl From<std::io::Error> for PreferencesError {
fn from(e: std::io::Error) -> Self {
PreferencesError::Io(e)
Self::Io(e)
}
}

impl From<AppDirsError> for PreferencesError {
fn from(e: AppDirsError) -> Self {
PreferencesError::Directory(e)
Self::Directory(e)
}
}

Expand Down Expand Up @@ -289,21 +291,27 @@ pub trait Preferences: Sized {
/// Saves the current state of this object. Implementation is platform-dependent, but the data
/// will be local to the active user.
///
/// # Failures
/// # Errors
/// If a serialization or file I/O error (e.g. permission denied) occurs.
fn save<S: AsRef<str>>(&self, app: &AppInfo, key: S) -> Result<(), PreferencesError>;
/// Loads this object's state from previously saved user data with the same `key`. This is
/// an instance method which completely overwrites the object's state with the serialized
/// data. Thus, it is recommended that you call this method immediately after instantiating
/// the preferences object.
///
/// # Failures
/// # Errors
/// If a deserialization or file I/O error (e.g. permission denied) occurs, or if no user data
/// exists at that `path`.
fn load<S: AsRef<str>>(app: &AppInfo, key: S) -> Result<Self, PreferencesError>;
/// Same as `save`, but writes the serialized preferences to an arbitrary writer.
///
/// # Errors
/// If a write or serialization error occurs.
fn save_to<W: Write>(&self, writer: &mut W) -> Result<(), PreferencesError>;
/// Same as `load`, but reads the serialized preferences from an arbitrary writer.
///
/// # Errors
/// If a read or deserialization error occurs.
fn load_from<R: Read>(reader: &mut R) -> Result<Self, PreferencesError>;
}

Expand All @@ -323,10 +331,12 @@ fn compute_file_path<S: AsRef<str>>(app: &AppInfo, key: S) -> Result<PathBuf, Pr
}

impl<T> Preferences for T
where T: Serialize + DeserializeOwned + Sized
where
T: Serialize + DeserializeOwned + Sized,
{
fn save<S>(&self, app: &AppInfo, key: S) -> Result<(), PreferencesError>
where S: AsRef<str>
where
S: AsRef<str>,
{
let path = compute_file_path(app, key.as_ref())?;
path.parent().map(create_dir_all);
Expand All @@ -351,18 +361,19 @@ impl<T> Preferences for T
/// This makes no guarantees that the specified directory path actually *exists* (though you can
/// easily use `std::fs::create_dir_all(..)`). Returns `None` if the directory cannot be determined
/// or is not available on the current platform.
#[must_use]
pub fn prefs_base_dir() -> Option<PathBuf> {
get_data_root(AppDataType::UserConfig).ok()
}

#[cfg(test)]
mod tests {
use {AppInfo, Preferences, PreferencesMap};
use super::{AppInfo, Preferences, PreferencesMap};
const APP_INFO: AppInfo = AppInfo {
name: "preferences",
author: "Rust language community",
};
const TEST_PREFIX: &'static str = "tests/module";
const TEST_PREFIX: &str = "tests/module";
fn gen_test_name(name: &str) -> String {
TEST_PREFIX.to_owned() + "/" + name
}
Expand Down

0 comments on commit 603a7e9

Please sign in to comment.