Skip to content

Commit

Permalink
Make clippy happy!
Browse files Browse the repository at this point in the history
  • Loading branch information
hunger committed Dec 6, 2020
1 parent 960a2dc commit 6802083
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 219 deletions.
23 changes: 16 additions & 7 deletions gng-build-agent/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,27 @@ fn register_custom_functionality(engine: &mut rhai::Engine) {
// - Custom Functions:
// ----------------------------------------------------------------------

fn version_epoch(input: ImmutableString) -> std::result::Result<Dynamic, Box<EvalAltResult>> {
fn version_epoch(input: &ImmutableString) -> std::result::Result<Dynamic, Box<EvalAltResult>> {
let version = Version::try_from(input.to_string()).map_err(|e| e.to_string())?;
Ok(Dynamic::from(version.epoch()))
}

fn version_upstream(input: ImmutableString) -> std::result::Result<Dynamic, Box<EvalAltResult>> {
fn version_upstream(input: &ImmutableString) -> std::result::Result<Dynamic, Box<EvalAltResult>> {
let version = Version::try_from(input.to_string()).map_err(|e| e.to_string())?;
Ok(version.upstream().into())
}

fn version_release(input: ImmutableString) -> std::result::Result<Dynamic, Box<EvalAltResult>> {
fn version_release(input: &ImmutableString) -> std::result::Result<Dynamic, Box<EvalAltResult>> {
let version = Version::try_from(input.to_string()).map_err(|e| e.to_string())?;
Ok(version.release().into())
}

fn hash_algorithm(input: ImmutableString) -> std::result::Result<Dynamic, Box<EvalAltResult>> {
fn hash_algorithm(input: &ImmutableString) -> std::result::Result<Dynamic, Box<EvalAltResult>> {
let hash = Hash::try_from(input.to_string()).map_err(|e| e.to_string())?;
Ok(hash.algorithm().into())
}

fn hash_value(input: ImmutableString) -> std::result::Result<Dynamic, Box<EvalAltResult>> {
fn hash_value(input: &ImmutableString) -> std::result::Result<Dynamic, Box<EvalAltResult>> {
let hash = Hash::try_from(input.to_string()).map_err(|e| e.to_string())?;
Ok(hash.value().into())
}
Expand Down Expand Up @@ -90,6 +90,9 @@ impl<'a> EngineBuilder<'a> {
}

/// Evaluate a script file
///
/// # Errors
/// * `Error::Script`: When the build script is invalid
pub fn eval_pkgsrc_directory(&mut self) -> crate::Result<Engine<'a>> {
let mut engine = std::mem::replace(&mut self.engine, rhai::Engine::new());
let mut scope = std::mem::replace(&mut self.scope, rhai::Scope::<'a>::new());
Expand Down Expand Up @@ -117,7 +120,7 @@ impl<'a> EngineBuilder<'a> {
})?;

let ast = engine
.compile_file_with_scope(&mut scope, build_file)
.compile_file_with_scope(&scope, build_file)
.map_err(|e| {
crate::Error::Script(
format!("Compilation of build script {} failed", build_file_str),
Expand Down Expand Up @@ -150,6 +153,9 @@ pub struct Engine<'a> {

impl<'a> Engine<'a> {
/// Evaluate an expression
///
/// # Errors
/// * `Error::Script`: When the expression is invalid
pub fn evaluate<T>(&mut self, expression: &str) -> crate::Result<T>
where
T: Clone + Send + Sync + serde::de::DeserializeOwned + 'static,
Expand All @@ -167,12 +173,15 @@ impl<'a> Engine<'a> {
}

/// Call a function (without arguments!)
///
/// # Errors
/// * `Error::Script`: When the function is not defined in rhai script
pub fn call<T>(&mut self, name: &str) -> crate::Result<T>
where
T: Clone + Sync + Send + 'static,
{
self.engine
.call_fn(&mut self.scope, &mut self.ast, name, ())
.call_fn(&mut self.scope, &self.ast, name, ())
.map_err(|e| {
crate::Error::Script(format!("Failed to call function {}", name), e.to_string())
})
Expand Down
31 changes: 23 additions & 8 deletions gng-build-agent/src/engine/rhai_modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

//! Filesystem related rhai module:

use rhai::plugin::*;
use rhai::EvalAltResult;
// #![allow(clippy::wildcard_imports)] // rhai depends on all symbols being available
use rhai::plugin::{
export_module, mem, new_vec, CallableFunction, FnAccess, FnNamespace, Module,
NativeCallContext, PluginFunction, TypeId,
};
use rhai::{Dynamic, EvalAltResult, ImmutableString};

use std::convert::TryFrom;
use std::os::unix::fs::PermissionsExt;

// Define a module for filesystem-related tasks.
Expand All @@ -16,20 +21,30 @@ mod fs_module {
mode: rhai::INT,
path: &str,
) -> std::result::Result<rhai::Dynamic, Box<rhai::EvalAltResult>> {
if mode > 0o7777 || mode < 0 {
if !(0..=0o7777).contains(&mode) {
return Err(format!("Invalid mode 0o{:o} for {}.", mode, path).into());
}
let mode = mode as u32;
std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode))
.map_err(|_| format!("Failed to change permissions on {}.", path))?;
let mode = u32::try_from(mode).expect("Was in a safe range just now!");
std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode)).map_err(|e| {
format!(
"Failed to change permissions on {}: {}",
path,
e.to_string()
)
})?;

Ok(rhai::Dynamic::from(true))
}

#[rhai_fn(return_raw)]
pub fn mkdir(directory: &str) -> std::result::Result<rhai::Dynamic, Box<rhai::EvalAltResult>> {
std::fs::create_dir(directory)
.map_err(|_| format!("Failed to create directory {}.", directory))?;
std::fs::create_dir(directory).map_err(|e| {
format!(
"Failed to create directory {}: {}",
directory,
e.to_string()
)
})?;
Ok(rhai::Dynamic::from(true))
}

Expand Down
1 change: 1 addition & 0 deletions gng-build-agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
)]
// Clippy:
#![warn(clippy::all, clippy::nursery, clippy::pedantic)]
#![allow(clippy::non_ascii_literal, clippy::module_name_repetitions)]

// ----------------------------------------------------------------------
// - Error Handling:
Expand Down
22 changes: 5 additions & 17 deletions gng-build-agent/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
)]
// Clippy:
#![warn(clippy::all, clippy::nursery, clippy::pedantic)]
#![allow(clippy::non_ascii_literal, clippy::module_name_repetitions)]

use gng_build_shared::constants::container as cc;
use gng_build_shared::constants::environment as ce;
use gng_build_shared::SourcePacket;

use structopt::StructOpt;

use std::path::Path;

// - Helpers:
// ----------------------------------------------------------------------

Expand All @@ -47,14 +45,14 @@ enum Args {
}

fn get_env(key: &str, default: &str) -> String {
let result = std::env::var(key).unwrap_or(default.to_owned());
let result = std::env::var(key).unwrap_or_else(|_| default.to_owned());
std::env::remove_var(key);
result
}

fn get_message_prefix() -> String {
let message_prefix =
std::env::var(ce::GNG_AGENT_MESSAGE_PREFIX).unwrap_or(String::from("MSG:"));
std::env::var(ce::GNG_AGENT_MESSAGE_PREFIX).unwrap_or_else(|_| String::from("MSG:"));
std::env::remove_var(ce::GNG_AGENT_MESSAGE_PREFIX);

message_prefix
Expand All @@ -75,12 +73,6 @@ fn send_message(message_prefix: &str, message_type: &gng_build_shared::MessageTy

struct Context<'a> {
engine: gng_build_agent::engine::Engine<'a>,
source_packet: SourcePacket,
message_prefix: String,
}

fn query(ctx: &mut Context) -> eyre::Result<()> {
Ok(())
}

fn prepare(ctx: &mut Context) -> eyre::Result<()> {
Expand Down Expand Up @@ -157,14 +149,10 @@ fn main() -> eyre::Result<()> {
&serde_json::to_string(&source_packet)?,
);

let mut ctx = Context {
engine,
source_packet,
message_prefix,
};
let mut ctx = Context { engine };

match args {
Args::QUERY => query(&mut ctx),
Args::QUERY => Ok(()),
Args::PREPARE => prepare(&mut ctx),
Args::BUILD => build(&mut ctx),
Args::CHECK => check(&mut ctx),
Expand Down
9 changes: 5 additions & 4 deletions gng-build-agent/src/source_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ use gng_shared::{Name, Version};
// ----------------------------------------------------------------------

/// Create a `SourcePacket` from an `Engine`
///
/// # Errors
/// Passes along `Error::Script` from the evaluation
pub fn from_engine(
engine: &mut crate::engine::Engine,
) -> crate::Result<gng_build_shared::SourcePacket> {
let source_name = engine.evaluate::<Name>("source_name")?;
let version = engine.evaluate::<Version>("version")?;
let license = engine.evaluate::<String>("license")?;
let url = engine.evaluate::<String>("url").unwrap_or(String::new());
let bug_url = engine
.evaluate::<String>("bug_url")
.unwrap_or(String::new());
let url = engine.evaluate::<String>("url").unwrap_or_default();
let bug_url = engine.evaluate::<String>("bug_url").unwrap_or_default();
let build_dependencies = engine.evaluate::<Vec<Name>>("build_dependencies")?;
let check_dependencies = engine.evaluate::<Vec<Name>>("check_dependencies")?;

Expand Down
Loading

0 comments on commit 6802083

Please sign in to comment.