Skip to content

Commit

Permalink
Add note about special bases to try
Browse files Browse the repository at this point in the history
Fix clippy issues
  • Loading branch information
spejamchr committed Mar 14, 2024
1 parent 8b7b9fa commit 4d98678
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/bases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn rounded_string(num: BigDecimal, hard_limit: Option<NonZeroU64>) -> String
let str = num
.with_precision_round(hl, bigdecimal::RoundingMode::Down)
.to_string();
if str.split("E").count() > 1 {
if str.split('E').count() > 1 {
return str;
} else {
return format!("{}…", str); // ellide
Expand Down
4 changes: 2 additions & 2 deletions src/components/home_inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::components::value_in_base::value_in_base;

#[component]
pub fn HomeInputs(
string_value: Memo<Result<String, String>>,
base10_representation: Memo<Result<String, String>>,
output_representation: Memo<Result<String, String>>,
#[prop(into)] input_string: Signal<String>,
#[prop(into)] set_input_string: WriteSignal<String>,
Expand All @@ -23,7 +23,7 @@ pub fn HomeInputs(
thead().child(
tr().child(th().child("Value in Base-10:"))
.child(th().child(value_in_base(
string_value,
base10_representation,
(|| BigDecimal::from(10)).into(),
))),
),
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use leptos::*;
use leptos_meta::*;
use leptos_router::*;

// Modules
mod bases;
Expand All @@ -11,7 +10,6 @@ mod pages;

// Top-Level pages
use crate::pages::home::Home;
use crate::pages::not_found::NotFound;

/// An app router which renders the homepage and handles 404's
#[component]
Expand Down
21 changes: 17 additions & 4 deletions src/pages/home.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::str::FromStr;

use bigdecimal::BigDecimal;
use leptos::*;
use leptos::{html::*, *};

use crate::{
bases::{val_from_base, val_to_base},
Expand All @@ -11,20 +11,31 @@ use crate::{
/// Default Home Page
#[component]
pub fn Home() -> impl IntoView {
let (input_string, set_input_string) = create_signal("23982982383829823983".to_string());
let (input_string, set_input_string) = create_signal("123.45".to_string());
let (input_base, set_input_base) = create_signal(BigDecimal::from_str("10.3").unwrap());
let (output_base, set_output_base) = create_signal(BigDecimal::from_str("10.3").unwrap());

let result_value = create_memo(move |_| val_from_base(&input_string(), &input_base()));

let string_value =
let base10_representation =
create_memo(move |_| result_value().and_then(|v| val_to_base(&v, &BigDecimal::from(10))));
let output_representation = create_memo(move |_| {
result_value()
.map_err(|_| "".to_string())
.and_then(|v| val_to_base(&v, &output_base()))
});

let also_try = sub()
.child("Also try bases: ")
.child(code().child("pi"))
.child(", ")
.child(code().child("e"))
.child(", ")
.child(code().child("sqrt2"))
.child(", ")
.child(code().child("phi"))
.child(".");

view! {
<ErrorBoundary fallback=|errors| {
view! {
Expand All @@ -50,7 +61,7 @@ pub fn Home() -> impl IntoView {
<h1>"ChangeBase"</h1>

<HomeInputs
string_value=string_value
base10_representation=base10_representation
output_representation=output_representation
input_string=input_string
set_input_string=set_input_string
Expand All @@ -60,6 +71,8 @@ pub fn Home() -> impl IntoView {
set_output_base=set_output_base
/>

{also_try}

<OutputDetails output={output_representation} base={output_base}/>

</div>
Expand Down

0 comments on commit 4d98678

Please sign in to comment.