MCPcopy Create free account
hub / github.com/SeaQL/sea-orm / active_enum_string

Function active_enum_string

src/entity/active_enum.rs:216–317  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

214
215 #[test]
216 fn active_enum_string() {
217 #[derive(Debug, PartialEq, Eq, EnumIter)]
218 pub enum Category {
219 Big,
220 Small,
221 }
222
223 #[derive(Debug, DeriveIden)]
224 #[sea_orm(iden = "category")]
225 pub struct CategoryEnum;
226
227 impl ActiveEnum for Category {
228 type Value = String;
229
230 type ValueVec = Vec<String>;
231
232 fn name() -> DynIden {
233 SeaRc::new(CategoryEnum)
234 }
235
236 fn to_value(&self) -> Self::Value {
237 match self {
238 Self::Big => "B",
239 Self::Small => "S",
240 }
241 .to_owned()
242 }
243
244 fn try_from_value(v: &Self::Value) -> Result<Self, DbErr> {
245 match v.as_ref() {
246 "B" => Ok(Self::Big),
247 "S" => Ok(Self::Small),
248 _ => Err(type_err(format!("unexpected value for Category enum: {v}"))),
249 }
250 }
251
252 fn db_type() -> ColumnDef {
253 ColumnType::String(StringLen::N(1)).def()
254 }
255 }
256
257 #[derive(Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum, DeriveDisplay)]
258 #[sea_orm(
259 rs_type = "String",
260 db_type = "String(StringLen::N(1))",
261 enum_name = "category"
262 )]
263 pub enum DeriveCategory {
264 #[sea_orm(string_value = "B")]
265 Big,
266 #[sea_orm(string_value = "S")]
267 Small,
268 }
269
270 assert_eq!(Category::Big.to_value(), "B".to_owned());
271 assert_eq!(Category::Small.to_value(), "S".to_owned());
272 assert_eq!(DeriveCategory::Big.to_value(), "B".to_owned());
273 assert_eq!(DeriveCategory::Small.to_value(), "S".to_owned());

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected