typestate builder

Created Diff never expires
9 removals
Lines
Total
Removed
Words
Total
Removed
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
81 lines
33 additions
Lines
Total
Added
Words
Total
Added
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
98 lines
fn main() {
fn main() {
let test = Test::builder()
let test = Test::builder()
.foo("Hello".to_string())
.foo("Hello".to_string())
.bar("world".to_string())
.bar("world".to_string())
.baz("!".to_string())
.baz("!".to_string())
.build();
.build();
println!("{test:?}")
println!("{test:?}")
}
}


#[derive(Debug, Default)]
#[derive(Debug, Default)]
pub struct Test {
pub struct Test {
foo: Option<String>,
foo: Option<String>,
bar: Option<String>,
bar: Option<String>,
baz: Option<String>,
baz: Option<String>,
quz: Option<String>,
}
}


#[derive(Debug)]
#[derive(Debug)]
pub struct TestBuilder<const FOO: bool, const BAR: bool, const BAZ: bool> {
pub struct TestBuilder<const FOO: bool, const BAR: bool, const BAZ: bool, const QUZ: bool> {
data: Test,
data: Test,
}
}


impl Test {
impl Test {
pub fn builder() -> TestBuilder<false, false, false> {
pub fn builder() -> TestBuilder<false, false, false, false> {
TestBuilder::new()
TestBuilder::new()
}
}
}
}


impl TestBuilder<false, false, false> {
impl TestBuilder<false, false, false, false> {
fn new() -> Self {
fn new() -> Self {
TestBuilder { data: Default::default() }
TestBuilder { data: Default::default() }
}
}
}
}


impl <const BAR: bool, const BAZ: bool> TestBuilder<false, BAR, BAZ> {
impl <const BAR: bool, const BAZ: bool, const QUZ: bool> TestBuilder<false, BAR, BAZ, QUZ> {
pub fn foo(self, value: String) -> TestBuilder<true, BAR, BAZ> {
pub fn foo(self, value: String) -> TestBuilder<true, BAR, BAZ, QUZ> {
let mut data = self.data;
let mut data = self.data;
data.foo = Some(value);
data.foo = Some(value);
TestBuilder {
TestBuilder {
data
data
}
}
}
}
}
}


impl <const FOO: bool, const BAZ: bool> TestBuilder<FOO, false, BAZ> {
impl <const FOO: bool, const BAZ: bool, const QUZ: bool> TestBuilder<FOO, false, BAZ, QUZ> {
pub fn bar(self, value: String) -> TestBuilder<FOO, true, BAZ> {
pub fn bar(self, value: String) -> TestBuilder<FOO, true, BAZ, QUZ> {
let mut data = self.data;
let mut data = self.data;
data.bar = Some(value);
data.bar = Some(value);
TestBuilder {
TestBuilder {
data
data
}
}
}
}
}
}


impl <const FOO: bool, const BAR: bool> TestBuilder<FOO, BAR, false> {
impl <const FOO: bool, const BAR: bool, const QUZ: bool> TestBuilder<FOO, BAR, false, QUZ> {
pub fn baz(self, value: String) -> TestBuilder<FOO, BAR, true> {
pub fn baz(self, value: String) -> TestBuilder<FOO, BAR, true, QUZ> {
let mut data = self.data;
let mut data = self.data;
data.baz = Some(value);
data.baz = Some(value);
TestBuilder {
TestBuilder {
data
data
}
}
}
}
}
}


impl TestBuilder<true, false, true> {
impl <const FOO: bool, const BAR: bool, const BAZ: bool> TestBuilder<FOO, BAR, BAZ, false> {
pub fn baz(self, value: String) -> TestBuilder<FOO, BAR, BAZ, true> {
let mut data = self.data;
data.quz = Some(value);
TestBuilder {
data
}
}
}

impl TestBuilder<true, false, true, false> {
pub fn build(self) -> Test {
pub fn build(self) -> Test {
self.data
self.data
}
}
}
}


impl TestBuilder<true, true, false> {
impl TestBuilder<true, true, false, true> {
pub fn build(self) -> Test {
pub fn build(self) -> Test {
self.data
self.data
}
}
}
}


impl TestBuilder<true, true, true> {
impl TestBuilder<true, true, false, false> {
pub fn build(self) -> Test {
self.data
}
}

impl TestBuilder<true, true, true, false> {
pub fn build(self) -> Test {
pub fn build(self) -> Test {
self.data
self.data
}
}
}
}