2021-02-08 23:58:46 +00:00
|
|
|
use rust_decimal::Decimal;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2021-02-04 01:40:06 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
use thiserror::Error;
|
|
|
|
|
2021-02-08 23:58:46 +00:00
|
|
|
#[derive(
|
|
|
|
Serialize, Deserialize, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default, sqlx::Type,
|
|
|
|
)]
|
|
|
|
#[serde(transparent)]
|
|
|
|
#[sqlx(transparent)]
|
2021-02-04 01:40:06 +00:00
|
|
|
pub struct StockName(String);
|
|
|
|
|
2021-02-08 23:58:46 +00:00
|
|
|
impl From<String> for StockName {
|
|
|
|
fn from(s: String) -> Self {
|
|
|
|
Self(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 01:40:06 +00:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum StockNameParseError {}
|
|
|
|
|
|
|
|
impl FromStr for StockName {
|
|
|
|
type Err = StockNameParseError;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
Ok(Self(s.to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 23:58:46 +00:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
|
|
|
|
#[serde(transparent)]
|
|
|
|
pub struct StockSymbol(String);
|
|
|
|
|
|
|
|
impl From<String> for StockSymbol {
|
|
|
|
fn from(s: String) -> Self {
|
|
|
|
Self(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
|
|
|
|
#[serde(transparent)]
|
|
|
|
pub struct StockDescription(String);
|
|
|
|
|
|
|
|
impl From<String> for StockDescription {
|
|
|
|
fn from(s: String) -> Self {
|
|
|
|
Self(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Copy, Clone, PartialEq, PartialOrd, Debug, Default)]
|
|
|
|
#[serde(transparent)]
|
|
|
|
pub struct StockPrice(Decimal);
|
|
|
|
|
|
|
|
impl From<Decimal> for StockPrice {
|
|
|
|
fn from(p: Decimal) -> Self {
|
|
|
|
Self(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
|
|
|
|
pub struct Stock {
|
|
|
|
name: StockName,
|
|
|
|
symbol: StockSymbol,
|
|
|
|
description: StockDescription,
|
|
|
|
price: StockPrice,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stock {
|
|
|
|
pub fn new(
|
|
|
|
name: impl Into<StockName>,
|
|
|
|
symbol: impl Into<StockSymbol>,
|
|
|
|
description: impl Into<StockDescription>,
|
|
|
|
price: impl Into<StockPrice>,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
name: name.into(),
|
|
|
|
symbol: symbol.into(),
|
|
|
|
description: description.into(),
|
|
|
|
price: price.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|