#[allow(unused_imports)]
use alloc::collections::BTreeMap;
#[allow(unused_imports)]
use core::marker::PhantomData;
use jacquard_common::{BosStr, CowStr, 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;
use crate::com_atproto::repo::strong_ref::StrongRef;
#[allow(unused_imports)]
use jacquard_lexicon::validation::{ConstraintError, ValidationPath};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
rename_all = "camelCase",
rename = "pub.quizzy.answer",
tag = "$type",
bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Answer<S: BosStr = DefaultStr> {
pub certainty: AnswerCertainty<S>,
pub question: StrongRef<S>,
pub text: S,
pub timestamp: Datetime,
#[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 AnswerCertainty<S: BosStr = DefaultStr> {
DeadCertain,
FairlySure,
EducatedGuess,
WildGuess,
Other(S),
}
impl<S: BosStr> AnswerCertainty<S> {
pub fn as_str(&self) -> &str {
match self {
Self::DeadCertain => "deadCertain",
Self::FairlySure => "fairlySure",
Self::EducatedGuess => "educatedGuess",
Self::WildGuess => "wildGuess",
Self::Other(s) => s.as_ref(),
}
}
pub fn from_value(s: S) -> Self {
match s.as_ref() {
"deadCertain" => Self::DeadCertain,
"fairlySure" => Self::FairlySure,
"educatedGuess" => Self::EducatedGuess,
"wildGuess" => Self::WildGuess,
_ => Self::Other(s),
}
}
}
impl<S: BosStr> core::fmt::Display for AnswerCertainty<S> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl<S: BosStr> AsRef<str> for AnswerCertainty<S> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl<S: BosStr> Serialize for AnswerCertainty<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 AnswerCertainty<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 AnswerCertainty<S> {
fn default() -> Self {
Self::Other(Default::default())
}
}
impl<S: BosStr> jacquard_common::IntoStatic for AnswerCertainty<S>
where
S: BosStr + jacquard_common::IntoStatic,
S::Output: BosStr,
{
type Output = AnswerCertainty<S::Output>;
fn into_static(self) -> Self::Output {
match self {
AnswerCertainty::DeadCertain => AnswerCertainty::DeadCertain,
AnswerCertainty::FairlySure => AnswerCertainty::FairlySure,
AnswerCertainty::EducatedGuess => AnswerCertainty::EducatedGuess,
AnswerCertainty::WildGuess => AnswerCertainty::WildGuess,
AnswerCertainty::Other(v) => AnswerCertainty::Other(v.into_static()),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct AnswerGetRecordOutput<S: BosStr = DefaultStr> {
#[serde(skip_serializing_if = "Option::is_none")]
pub cid: Option<Cid<S>>,
pub uri: AtUri<S>,
pub value: Answer<S>,
}
impl<S: BosStr> Answer<S> {
pub fn uri(uri: S) -> Result<RecordUri<S, AnswerRecord>, UriError> {
RecordUri::try_from_uri(AtUri::new(uri)?)
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct AnswerRecord;
impl XrpcResp for AnswerRecord {
const NSID: &'static str = "pub.quizzy.answer";
const ENCODING: &'static str = "application/json";
type Output<S: BosStr> = AnswerGetRecordOutput<S>;
type Err = RecordError;
}
impl<S: BosStr> From<AnswerGetRecordOutput<S>> for Answer<S> {
fn from(output: AnswerGetRecordOutput<S>) -> Self {
output.value
}
}
impl<S: BosStr> Collection for Answer<S> {
const NSID: &'static str = "pub.quizzy.answer";
type Record = AnswerRecord;
}
impl Collection for AnswerRecord {
const NSID: &'static str = "pub.quizzy.answer";
type Record = AnswerRecord;
}
impl<S: BosStr> LexiconSchema for Answer<S> {
fn nsid() -> &'static str {
"pub.quizzy.answer"
}
fn def_name() -> &'static str {
"main"
}
fn lexicon_doc() -> LexiconDoc<'static> {
lexicon_doc_pub_quizzy_answer()
}
fn validate(&self) -> Result<(), ConstraintError> {
{
let value = &self.text;
#[allow(unused_comparisons)]
if <str>::len(value.as_ref()) > 1000usize {
return Err(ConstraintError::MaxLength {
path: ValidationPath::from_field("text"),
max: 1000usize,
actual: <str>::len(value.as_ref()),
});
}
}
{
let value = &self.text;
{
let count = UnicodeSegmentation::graphemes(value.as_ref(), true).count();
if count > 100usize {
return Err(ConstraintError::MaxGraphemes {
path: ValidationPath::from_field("text"),
max: 100usize,
actual: count,
});
}
}
}
Ok(())
}
}
pub mod answer_state {
pub use crate::builder_types::{IsSet, IsUnset, Set, Unset};
#[allow(unused)]
use ::core::marker::PhantomData;
mod sealed {
pub trait Sealed {}
}
pub trait State: sealed::Sealed {
type Timestamp;
type Question;
type Text;
type Certainty;
}
pub struct Empty(());
impl sealed::Sealed for Empty {}
impl State for Empty {
type Timestamp = Unset;
type Question = Unset;
type Text = Unset;
type Certainty = Unset;
}
pub struct SetTimestamp<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetTimestamp<St> {}
impl<St: State> State for SetTimestamp<St> {
type Timestamp = Set<members::timestamp>;
type Question = St::Question;
type Text = St::Text;
type Certainty = St::Certainty;
}
pub struct SetQuestion<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetQuestion<St> {}
impl<St: State> State for SetQuestion<St> {
type Timestamp = St::Timestamp;
type Question = Set<members::question>;
type Text = St::Text;
type Certainty = St::Certainty;
}
pub struct SetText<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetText<St> {}
impl<St: State> State for SetText<St> {
type Timestamp = St::Timestamp;
type Question = St::Question;
type Text = Set<members::text>;
type Certainty = St::Certainty;
}
pub struct SetCertainty<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetCertainty<St> {}
impl<St: State> State for SetCertainty<St> {
type Timestamp = St::Timestamp;
type Question = St::Question;
type Text = St::Text;
type Certainty = Set<members::certainty>;
}
#[allow(non_camel_case_types)]
pub mod members {
pub struct timestamp(());
pub struct question(());
pub struct text(());
pub struct certainty(());
}
}
pub struct AnswerBuilder<S: BosStr, St: answer_state::State> {
_state: PhantomData<fn() -> St>,
_fields: (
Option<AnswerCertainty<S>>,
Option<StrongRef<S>>,
Option<S>,
Option<Datetime>,
),
_type: PhantomData<fn() -> S>,
}
impl<S: BosStr> Answer<S> {
pub fn new() -> AnswerBuilder<S, answer_state::Empty> {
AnswerBuilder::new()
}
}
impl<S: BosStr> AnswerBuilder<S, answer_state::Empty> {
pub fn new() -> Self {
AnswerBuilder {
_state: PhantomData,
_fields: (None, None, None, None),
_type: PhantomData,
}
}
}
impl<S: BosStr, St> AnswerBuilder<S, St>
where
St: answer_state::State,
St::Certainty: answer_state::IsUnset,
{
pub fn certainty(
mut self,
value: impl Into<AnswerCertainty<S>>,
) -> AnswerBuilder<S, answer_state::SetCertainty<St>> {
self._fields.0 = Option::Some(value.into());
AnswerBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> AnswerBuilder<S, St>
where
St: answer_state::State,
St::Question: answer_state::IsUnset,
{
pub fn question(
mut self,
value: impl Into<StrongRef<S>>,
) -> AnswerBuilder<S, answer_state::SetQuestion<St>> {
self._fields.1 = Option::Some(value.into());
AnswerBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> AnswerBuilder<S, St>
where
St: answer_state::State,
St::Text: answer_state::IsUnset,
{
pub fn text(mut self, value: impl Into<S>) -> AnswerBuilder<S, answer_state::SetText<St>> {
self._fields.2 = Option::Some(value.into());
AnswerBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> AnswerBuilder<S, St>
where
St: answer_state::State,
St::Timestamp: answer_state::IsUnset,
{
pub fn timestamp(
mut self,
value: impl Into<Datetime>,
) -> AnswerBuilder<S, answer_state::SetTimestamp<St>> {
self._fields.3 = Option::Some(value.into());
AnswerBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> AnswerBuilder<S, St>
where
St: answer_state::State,
St::Timestamp: answer_state::IsSet,
St::Question: answer_state::IsSet,
St::Text: answer_state::IsSet,
St::Certainty: answer_state::IsSet,
{
pub fn build(self) -> Answer<S> {
Answer {
certainty: self._fields.0.unwrap(),
question: self._fields.1.unwrap(),
text: self._fields.2.unwrap(),
timestamp: self._fields.3.unwrap(),
extra_data: Default::default(),
}
}
pub fn build_with_data(self, extra_data: BTreeMap<SmolStr, Data<S>>) -> Answer<S> {
Answer {
certainty: self._fields.0.unwrap(),
question: self._fields.1.unwrap(),
text: self._fields.2.unwrap(),
timestamp: self._fields.3.unwrap(),
extra_data: Some(extra_data),
}
}
}
fn lexicon_doc_pub_quizzy_answer() -> LexiconDoc<'static> {
use alloc::collections::BTreeMap;
#[allow(unused_imports)]
use jacquard_common::{CowStr, deps::smol_str::SmolStr, types::blob::MimeType};
use jacquard_lexicon::lexicon::*;
LexiconDoc {
lexicon: Lexicon::Lexicon1,
id: CowStr::new_static("pub.quizzy.answer"),
defs: {
let mut map = BTreeMap::new();
map.insert(
SmolStr::new_static("main"),
LexUserType::Record(LexRecord {
description: Some(CowStr::new_static("A person's answer to a question")),
key: Some(CowStr::new_static("tid")),
record: LexRecordRecord::Object(LexObject {
required: Some(vec![
SmolStr::new_static("question"),
SmolStr::new_static("text"),
SmolStr::new_static("certainty"),
SmolStr::new_static("timestamp"),
]),
properties: {
#[allow(unused_mut)]
let mut map = BTreeMap::new();
map.insert(
SmolStr::new_static("certainty"),
LexObjectProperty::String(LexString {
description: Some(CowStr::new_static(
"How certain the person is about this answer",
)),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("question"),
LexObjectProperty::Ref(LexRef {
r#ref: CowStr::new_static("com.atproto.repo.strongRef"),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("text"),
LexObjectProperty::String(LexString {
description: Some(CowStr::new_static("The answer text")),
max_length: Some(1000usize),
max_graphemes: Some(100usize),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("timestamp"),
LexObjectProperty::String(LexString {
description: Some(CowStr::new_static(
"When this answer was submitted",
)),
format: Some(LexStringFormat::Datetime),
..Default::default()
}),
);
map
},
..Default::default()
}),
..Default::default()
}),
);
map
},
..Default::default()
}
}