Skip to content

Commit

Permalink
Extract reusable code from doc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
schneems committed Dec 20, 2024
1 parent 4647bc4 commit ccc9700
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 264 deletions.
27 changes: 27 additions & 0 deletions src/fixtures/personV1_V2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# use chrono::{DateTime, Utc};
# use serde::{Deserialize, Serialize};
# use std::convert::Infallible;
#
# #[derive(Deserialize, Serialize, Debug)]
# #[serde(deny_unknown_fields)]
# struct PersonV1 {
# name: String,
# }
#
# #[derive(Deserialize, Serialize, Debug)]
# #[serde(deny_unknown_fields)]
# struct PersonV2 {
# name: String,
# updated_at: DateTime<Utc>,
# }
#
# // First define how to map from one struct to another
# impl From<PersonV1> for PersonV2 {
# fn from(value: PersonV1) -> Self {
# PersonV2 {
# name: value.name.clone(),
# updated_at: Utc::now(),
# }
# }
# }
#
50 changes: 50 additions & 0 deletions src/fixtures/try_personV1_V2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# use serde::{Deserialize, Serialize};
# use std::convert::Infallible;
#
# #[derive(Deserialize, Serialize, Debug)]
# #[serde(deny_unknown_fields)]
# struct PersonV1 {
# name: String,
# title: Option<String>,
# }
#
# #[derive(Deserialize, Serialize, Debug)]
# #[serde(deny_unknown_fields)]
# struct PersonV2 {
# name: String,
# job_title: String,
# }
#
# #[derive(Debug, Eq, PartialEq)]
# struct TitleCannotBeEmpty;
#
# impl From<TitleCannotBeEmpty> for PersonMigrationError {
# fn from(value: TitleCannotBeEmpty) -> Self {
# PersonMigrationError::TitleCannotBeEmpty
# }
# }
#
# #[derive(thiserror::Error, Debug, Eq, PartialEq)]
# enum PersonMigrationError {
# #[error("Title cannot be empty!!!")]
# TitleCannotBeEmpty,
# }
#
# impl TryFrom<PersonV1> for PersonV2 {
# type Error = TitleCannotBeEmpty;
#
# fn try_from(value: PersonV1) -> Result<Self, TitleCannotBeEmpty> {
# if let Some(title) = value.title {
# if title.is_empty() {
# Err(TitleCannotBeEmpty)
# } else {
# Ok(PersonV2 {
# name: value.name,
# job_title: title,
# })
# }
# } else {
# Err(TitleCannotBeEmpty)
# }
# }
# }
Loading

0 comments on commit ccc9700

Please sign in to comment.