#[allow(unused_imports)]
use alloc::collections::BTreeMap;
#[allow(unused_imports)]
use core::marker::PhantomData;
use jacquard_common::{CowStr, BosStr, DefaultStr, FromStaticStr};
#[allow(unused_imports)]
use jacquard_common::deps::codegen::unicode_segmentation::UnicodeSegmentation;
use jacquard_common::deps::smol_str::SmolStr;
use jacquard_common::types::collection::{Collection, RecordError};
use jacquard_common::types::string::{AtUri, Cid, Datetime};
use jacquard_common::types::uri::{RecordUri, UriError};
use jacquard_common::types::value::Data;
use jacquard_common::xrpc::XrpcResp;
use jacquard_derive::{IntoStatic, lexicon};
use jacquard_lexicon::lexicon::LexiconDoc;
use jacquard_lexicon::schema::LexiconSchema;
#[allow(unused_imports)]
use jacquard_lexicon::validation::{ConstraintError, ValidationPath};
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
rename_all = "camelCase",
rename = "app.fitsky.goal",
tag = "$type",
bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Goal<S: BosStr = DefaultStr> {
pub created_at: Datetime,
#[serde(skip_serializing_if = "Option::is_none")]
pub end_date: Option<Datetime>,
pub metric: GoalMetric<S>,
pub period: GoalPeriod<S>,
pub start_date: Datetime,
pub target_value: i64,
#[serde(flatten, default, skip_serializing_if = "Option::is_none")]
pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum GoalMetric<S: BosStr = DefaultStr> {
Distance,
Workouts,
Duration,
Pace,
Other(S),
}
impl<S: BosStr> GoalMetric<S> {
pub fn as_str(&self) -> &str {
match self {
Self::Distance => "distance",
Self::Workouts => "workouts",
Self::Duration => "duration",
Self::Pace => "pace",
Self::Other(s) => s.as_ref(),
}
}
pub fn from_value(s: S) -> Self {
match s.as_ref() {
"distance" => Self::Distance,
"workouts" => Self::Workouts,
"duration" => Self::Duration,
"pace" => Self::Pace,
_ => Self::Other(s),
}
}
}
impl<S: BosStr> core::fmt::Display for GoalMetric<S> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl<S: BosStr> AsRef<str> for GoalMetric<S> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl<S: BosStr> Serialize for GoalMetric<S> {
fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
where
Ser: serde::Serializer,
{
serializer.serialize_str(self.as_str())
}
}
impl<'de, S: Deserialize<'de> + BosStr> Deserialize<'de> for GoalMetric<S> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = S::deserialize(deserializer)?;
Ok(Self::from_value(s))
}
}
impl<S: BosStr + Default> Default for GoalMetric<S> {
fn default() -> Self {
Self::Other(Default::default())
}
}
impl<S: BosStr> jacquard_common::IntoStatic for GoalMetric<S>
where
S: BosStr + jacquard_common::IntoStatic,
S::Output: BosStr,
{
type Output = GoalMetric<S::Output>;
fn into_static(self) -> Self::Output {
match self {
GoalMetric::Distance => GoalMetric::Distance,
GoalMetric::Workouts => GoalMetric::Workouts,
GoalMetric::Duration => GoalMetric::Duration,
GoalMetric::Pace => GoalMetric::Pace,
GoalMetric::Other(v) => GoalMetric::Other(v.into_static()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum GoalPeriod<S: BosStr = DefaultStr> {
Weekly,
Monthly,
Yearly,
Other(S),
}
impl<S: BosStr> GoalPeriod<S> {
pub fn as_str(&self) -> &str {
match self {
Self::Weekly => "weekly",
Self::Monthly => "monthly",
Self::Yearly => "yearly",
Self::Other(s) => s.as_ref(),
}
}
pub fn from_value(s: S) -> Self {
match s.as_ref() {
"weekly" => Self::Weekly,
"monthly" => Self::Monthly,
"yearly" => Self::Yearly,
_ => Self::Other(s),
}
}
}
impl<S: BosStr> core::fmt::Display for GoalPeriod<S> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl<S: BosStr> AsRef<str> for GoalPeriod<S> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl<S: BosStr> Serialize for GoalPeriod<S> {
fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
where
Ser: serde::Serializer,
{
serializer.serialize_str(self.as_str())
}
}
impl<'de, S: Deserialize<'de> + BosStr> Deserialize<'de> for GoalPeriod<S> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = S::deserialize(deserializer)?;
Ok(Self::from_value(s))
}
}
impl<S: BosStr + Default> Default for GoalPeriod<S> {
fn default() -> Self {
Self::Other(Default::default())
}
}
impl<S: BosStr> jacquard_common::IntoStatic for GoalPeriod<S>
where
S: BosStr + jacquard_common::IntoStatic,
S::Output: BosStr,
{
type Output = GoalPeriod<S::Output>;
fn into_static(self) -> Self::Output {
match self {
GoalPeriod::Weekly => GoalPeriod::Weekly,
GoalPeriod::Monthly => GoalPeriod::Monthly,
GoalPeriod::Yearly => GoalPeriod::Yearly,
GoalPeriod::Other(v) => GoalPeriod::Other(v.into_static()),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct GoalGetRecordOutput<S: BosStr = DefaultStr> {
#[serde(skip_serializing_if = "Option::is_none")]
pub cid: Option<Cid<S>>,
pub uri: AtUri<S>,
pub value: Goal<S>,
}
impl<S: BosStr> Goal<S> {
pub fn uri(uri: S) -> Result<RecordUri<S, GoalRecord>, UriError> {
RecordUri::try_from_uri(AtUri::new(uri)?)
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GoalRecord;
impl XrpcResp for GoalRecord {
const NSID: &'static str = "app.fitsky.goal";
const ENCODING: &'static str = "application/json";
type Output<S: BosStr> = GoalGetRecordOutput<S>;
type Err = RecordError;
}
impl<S: BosStr> From<GoalGetRecordOutput<S>> for Goal<S> {
fn from(output: GoalGetRecordOutput<S>) -> Self {
output.value
}
}
impl<S: BosStr> Collection for Goal<S> {
const NSID: &'static str = "app.fitsky.goal";
type Record = GoalRecord;
}
impl Collection for GoalRecord {
const NSID: &'static str = "app.fitsky.goal";
type Record = GoalRecord;
}
impl<S: BosStr> LexiconSchema for Goal<S> {
fn nsid() -> &'static str {
"app.fitsky.goal"
}
fn def_name() -> &'static str {
"main"
}
fn lexicon_doc() -> LexiconDoc<'static> {
lexicon_doc_app_fitsky_goal()
}
fn validate(&self) -> Result<(), ConstraintError> {
{
let value = &self.metric;
#[allow(unused_comparisons)]
if <str>::len(value.as_ref()) > 64usize {
return Err(ConstraintError::MaxLength {
path: ValidationPath::from_field("metric"),
max: 64usize,
actual: <str>::len(value.as_ref()),
});
}
}
{
let value = &self.period;
#[allow(unused_comparisons)]
if <str>::len(value.as_ref()) > 64usize {
return Err(ConstraintError::MaxLength {
path: ValidationPath::from_field("period"),
max: 64usize,
actual: <str>::len(value.as_ref()),
});
}
}
{
let value = &self.target_value;
if *value < 0i64 {
return Err(ConstraintError::Minimum {
path: ValidationPath::from_field("target_value"),
min: 0i64,
actual: *value,
});
}
}
Ok(())
}
}
pub mod goal_state {
pub use crate::builder_types::{Set, Unset, IsSet, IsUnset};
#[allow(unused)]
use ::core::marker::PhantomData;
mod sealed {
pub trait Sealed {}
}
pub trait State: sealed::Sealed {
type TargetValue;
type CreatedAt;
type StartDate;
type Metric;
type Period;
}
pub struct Empty(());
impl sealed::Sealed for Empty {}
impl State for Empty {
type TargetValue = Unset;
type CreatedAt = Unset;
type StartDate = Unset;
type Metric = Unset;
type Period = Unset;
}
pub struct SetTargetValue<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetTargetValue<St> {}
impl<St: State> State for SetTargetValue<St> {
type TargetValue = Set<members::target_value>;
type CreatedAt = St::CreatedAt;
type StartDate = St::StartDate;
type Metric = St::Metric;
type Period = St::Period;
}
pub struct SetCreatedAt<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetCreatedAt<St> {}
impl<St: State> State for SetCreatedAt<St> {
type TargetValue = St::TargetValue;
type CreatedAt = Set<members::created_at>;
type StartDate = St::StartDate;
type Metric = St::Metric;
type Period = St::Period;
}
pub struct SetStartDate<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetStartDate<St> {}
impl<St: State> State for SetStartDate<St> {
type TargetValue = St::TargetValue;
type CreatedAt = St::CreatedAt;
type StartDate = Set<members::start_date>;
type Metric = St::Metric;
type Period = St::Period;
}
pub struct SetMetric<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetMetric<St> {}
impl<St: State> State for SetMetric<St> {
type TargetValue = St::TargetValue;
type CreatedAt = St::CreatedAt;
type StartDate = St::StartDate;
type Metric = Set<members::metric>;
type Period = St::Period;
}
pub struct SetPeriod<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetPeriod<St> {}
impl<St: State> State for SetPeriod<St> {
type TargetValue = St::TargetValue;
type CreatedAt = St::CreatedAt;
type StartDate = St::StartDate;
type Metric = St::Metric;
type Period = Set<members::period>;
}
#[allow(non_camel_case_types)]
pub mod members {
pub struct target_value(());
pub struct created_at(());
pub struct start_date(());
pub struct metric(());
pub struct period(());
}
}
pub struct GoalBuilder<S: BosStr, St: goal_state::State> {
_state: PhantomData<fn() -> St>,
_fields: (
Option<Datetime>,
Option<Datetime>,
Option<GoalMetric<S>>,
Option<GoalPeriod<S>>,
Option<Datetime>,
Option<i64>,
),
_type: PhantomData<fn() -> S>,
}
impl<S: BosStr> Goal<S> {
pub fn new() -> GoalBuilder<S, goal_state::Empty> {
GoalBuilder::new()
}
}
impl<S: BosStr> GoalBuilder<S, goal_state::Empty> {
pub fn new() -> Self {
GoalBuilder {
_state: PhantomData,
_fields: (None, None, None, None, None, None),
_type: PhantomData,
}
}
}
impl<S: BosStr, St> GoalBuilder<S, St>
where
St: goal_state::State,
St::CreatedAt: goal_state::IsUnset,
{
pub fn created_at(
mut self,
value: impl Into<Datetime>,
) -> GoalBuilder<S, goal_state::SetCreatedAt<St>> {
self._fields.0 = Option::Some(value.into());
GoalBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St: goal_state::State> GoalBuilder<S, St> {
pub fn end_date(mut self, value: impl Into<Option<Datetime>>) -> Self {
self._fields.1 = value.into();
self
}
pub fn maybe_end_date(mut self, value: Option<Datetime>) -> Self {
self._fields.1 = value;
self
}
}
impl<S: BosStr, St> GoalBuilder<S, St>
where
St: goal_state::State,
St::Metric: goal_state::IsUnset,
{
pub fn metric(
mut self,
value: impl Into<GoalMetric<S>>,
) -> GoalBuilder<S, goal_state::SetMetric<St>> {
self._fields.2 = Option::Some(value.into());
GoalBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> GoalBuilder<S, St>
where
St: goal_state::State,
St::Period: goal_state::IsUnset,
{
pub fn period(
mut self,
value: impl Into<GoalPeriod<S>>,
) -> GoalBuilder<S, goal_state::SetPeriod<St>> {
self._fields.3 = Option::Some(value.into());
GoalBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> GoalBuilder<S, St>
where
St: goal_state::State,
St::StartDate: goal_state::IsUnset,
{
pub fn start_date(
mut self,
value: impl Into<Datetime>,
) -> GoalBuilder<S, goal_state::SetStartDate<St>> {
self._fields.4 = Option::Some(value.into());
GoalBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> GoalBuilder<S, St>
where
St: goal_state::State,
St::TargetValue: goal_state::IsUnset,
{
pub fn target_value(
mut self,
value: impl Into<i64>,
) -> GoalBuilder<S, goal_state::SetTargetValue<St>> {
self._fields.5 = Option::Some(value.into());
GoalBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> GoalBuilder<S, St>
where
St: goal_state::State,
St::TargetValue: goal_state::IsSet,
St::CreatedAt: goal_state::IsSet,
St::StartDate: goal_state::IsSet,
St::Metric: goal_state::IsSet,
St::Period: goal_state::IsSet,
{
pub fn build(self) -> Goal<S> {
Goal {
created_at: self._fields.0.unwrap(),
end_date: self._fields.1,
metric: self._fields.2.unwrap(),
period: self._fields.3.unwrap(),
start_date: self._fields.4.unwrap(),
target_value: self._fields.5.unwrap(),
extra_data: Default::default(),
}
}
pub fn build_with_data(self, extra_data: BTreeMap<SmolStr, Data<S>>) -> Goal<S> {
Goal {
created_at: self._fields.0.unwrap(),
end_date: self._fields.1,
metric: self._fields.2.unwrap(),
period: self._fields.3.unwrap(),
start_date: self._fields.4.unwrap(),
target_value: self._fields.5.unwrap(),
extra_data: Some(extra_data),
}
}
}
fn lexicon_doc_app_fitsky_goal() -> LexiconDoc<'static> {
#[allow(unused_imports)]
use jacquard_common::{CowStr, deps::smol_str::SmolStr, types::blob::MimeType};
use jacquard_lexicon::lexicon::*;
use alloc::collections::BTreeMap;
LexiconDoc {
lexicon: Lexicon::Lexicon1,
id: CowStr::new_static("app.fitsky.goal"),
defs: {
let mut map = BTreeMap::new();
map.insert(
SmolStr::new_static("main"),
LexUserType::Record(LexRecord {
description: Some(
CowStr::new_static("A fitness goal to track progress against"),
),
key: Some(CowStr::new_static("tid")),
record: LexRecordRecord::Object(LexObject {
required: Some(
vec![
SmolStr::new_static("metric"),
SmolStr::new_static("targetValue"),
SmolStr::new_static("period"),
SmolStr::new_static("startDate"),
SmolStr::new_static("createdAt")
],
),
properties: {
#[allow(unused_mut)]
let mut map = BTreeMap::new();
map.insert(
SmolStr::new_static("createdAt"),
LexObjectProperty::String(LexString {
format: Some(LexStringFormat::Datetime),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("endDate"),
LexObjectProperty::String(LexString {
format: Some(LexStringFormat::Datetime),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("metric"),
LexObjectProperty::String(LexString {
max_length: Some(64usize),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("period"),
LexObjectProperty::String(LexString {
max_length: Some(64usize),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("startDate"),
LexObjectProperty::String(LexString {
format: Some(LexStringFormat::Datetime),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("targetValue"),
LexObjectProperty::Integer(LexInteger {
minimum: Some(0i64),
..Default::default()
}),
);
map
},
..Default::default()
}),
..Default::default()
}),
);
map
},
..Default::default()
}
}