toasty_core/schema/app/relation/
has_one.rs

1use crate::{
2    schema::app::{BelongsTo, FieldId, FieldTy, Model, ModelId, Schema},
3    stmt,
4};
5
6#[derive(Debug, Clone)]
7pub struct HasOne {
8    /// Associated model
9    pub target: ModelId,
10
11    /// The association's expression type. This is the type the field evaluates
12    /// to from a user's point of view.
13    pub expr_ty: stmt::Type,
14
15    /// The `BelongsTo` association that pairs with this
16    pub pair: FieldId,
17}
18
19impl HasOne {
20    pub fn target<'a>(&self, schema: &'a Schema) -> &'a Model {
21        schema.model(self.target)
22    }
23
24    pub fn pair<'a>(&self, schema: &'a Schema) -> &'a BelongsTo {
25        schema.field(self.pair).ty.expect_belongs_to()
26    }
27}
28
29impl From<HasOne> for FieldTy {
30    fn from(value: HasOne) -> Self {
31        Self::HasOne(value)
32    }
33}