use std::{fmt::Debug, marker::PhantomData}; pub trait Build { fn build(code: C, msg: String) -> Self; } #[allow(dead_code)] pub struct Builder { pub code: C, pub msg: Option, phantom: PhantomData, } impl Builder where C: Debug, O: Build + Build, { pub fn new(code: C) -> Self { Builder { code, msg: None, phantom: PhantomData, } } pub fn msg(mut self, msg: M) -> Self where M: Into, { self.msg = Some(msg.into()); self } pub fn error(mut self, err: Err) -> Self where Err: std::fmt::Debug, { self.msg = Some(format!("{:?}", err)); self } pub fn build(mut self) -> O { let msg = self.msg.take().unwrap_or("".to_owned()); O::build(self.code, msg) } }