Skip to content

Instantly share code, notes, and snippets.

@patefacio
patefacio / curl_samples.sh
Last active May 22, 2018 20:10
Perf Issue HTML only
bash-3.2$ time curl -H "Accept: text/html" http://localhost:8002/forecast/214/1116543?use_item_assumptions=true > /tmp/goo.html
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 321k 100 321k 0 0 168k 0 0:00:01 0:00:01 --:--:-- 168k
real 0m1.921s
user 0m0.008s
sys 0m0.006s
@patefacio
patefacio / visit.rs
Last active March 13, 2018 20:39
Controlled access (Law of Demeter) vs ...
pub struct Account {
/// Identifier for the account
pub id: String,
/// Type of account, important for determining treatment of gains.
///
/// Note: Unlike _Growth Item_ categorization, this is not used to make
/// _categorical_ growth assumptions.
///
pub account_type: AccountType,
/// Holdings in the account
@patefacio
patefacio / compiling_working_fn.rs
Last active March 12, 2018 20:14
Understanding borrowing issue in face of closure
/// Resolve assumptions.
///
/// For each `Worth`, `Holding` and `FlowSpec` finds the appropriate
/// `GrowthAssumption` by looking within the item if `use_item_assumptions`
/// or looking in the `growth_mapping` for the categorized growth.
///
/// * `use_item_assumptions` - If true will prefer `GrowthAssumption`s stored in `Dossier`
/// * `growth_mapping` - Maps `growth_item` to growth characteristics
/// * _return_ - One `AssumptionDetails` instance per each `Worth`, `Holding`, `FlowSpec
///
@patefacio
patefacio / initializing_around_non_copy.rs
Last active March 10, 2018 16:37
How to workaround cannot move out of borrowed content
pub fn roll_worksheet(&mut self) -> () {
let tax_bill = self.worksheet.fed_tax_statement.total_tax_bill;
if let Some(ref mut forecast_details) = self.forecast_details {
forecast_details.worksheets.push(self.worksheet.clone());
}
let worksheet = self.worksheet;
let mut liquidation_summary = worksheet.liquidation_summary;
liquidation_summary.clear();
@patefacio
patefacio / match_alias_to_fn.rs
Last active February 14, 2018 16:29
Trouble matching type alias to member function
type TestFunction =
for<'r> fn(&'r ForecastTester<'r>, forecast_test: &ForecastTest) -> TestResult;
type TestFunctionMap = HashMap<String, TestFunction>;
// --- module struct definitions ---
/// Reads forecast from filesystem, forecasts on them, and reports on results
struct ForecastTester<'a> {
/// Tests read from filesystem requiring validation
forecast_tests: &'a ForecastTestMap,
pub fn read_from_yaml_file<T>(
yaml_path: &::std::path::Path
) -> ::std::result::Result<T, ::failure::Error>
where
for<'de> T: Deserialize<'de> + Serialize,
{
//...
}
pub fn read_from_yaml_file<'de, T>(
@patefacio
patefacio / route_file_gen.dart
Created January 12, 2018 15:30
Sample route generator
import '../../../codegen/rocket_gen/rocket_gen.dart';
final rocketRoutes = [
get('dossier')
..doc = 'Get a dossier'
..requiresDataAccess = true
..dynamicSegments = [
dynamicSegment('dossier_id', 'i32')..doc = 'Id of `Dossier` being queried',
],
get('user')
@patefacio
patefacio / error.rs
Last active January 8, 2018 15:59
Error chain question
error_chain!{
errors {
DieselPgError(diesel_pg_action: DieselPgAction) {
description("Failed interaction with postgresql via diesel"),
}
}
}
// --- module enum definitions ---
@patefacio
patefacio / lifetimewoe.rs
Created January 1, 2018 03:30
lifetime question
/// Wraps the `DataAccessLayer` and provides `Deref` for rocket conversion via `FromRequest`
pub struct DataLayer<'a> {
/// Underlying `DataAccessLayer` from plus_persist
pub data_access_layer: DataAccessLayer<'a>,
}
// --- module impl definitions ---
/// Implementation of trait `Deref` for type `DataLayer<'a>`
use conversion::rate_curve_to_time_series;
use grid_return_generator::{GridReturnGenerator, return_generator_type};
use item_wrappers::*;
use log::LogLevel::{Info};
use plus_core::{DistributionBreakdown, DistributionSpec, DistributionTreatment, Dossier, ForecastConfig, ForecastStyle, ForecastSummary, PeriodBalance, TaxablePosition, Worksheet, Year, YearRange};
use plus_persist::pg::LookupPair;
use plus_persist::{ForecastAssumptions};
use rand::{SeedableRng, XorShiftRng};
use std;
use std::cmp::{max};