Skip to content

Remove dependency on rustfmt installation for consumers #221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions example-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ serde = "1.0"
serde_json = "1.0.94"

[build-dependencies]
prettyplease = "0.1"
schemars = "0.8"
serde_json = "1.0.94"
syn = "1"
typify = { path = "../typify" }
2 changes: 1 addition & 1 deletion example-build/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() {
let contents = format!(
"{}\n{}",
"use serde::{Deserialize, Serialize};",
type_space.to_string()
prettyplease::unparse(&syn::parse2::<syn::File>(type_space.to_stream()).unwrap())
);

let mut out_file = Path::new(&env::var("OUT_DIR").unwrap()).to_path_buf();
Expand Down
3 changes: 2 additions & 1 deletion typify-impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ log = "0.4"
proc-macro2 = "1.0"
quote = "1.0"
regress = "0.5.0"
rustfmt-wrapper = "0.2"
schemars = "0.8.12"
serde_json = "1.0"
syn = { version = "1.0", features = ["full"] }
Expand All @@ -24,6 +23,8 @@ unicode-ident = "1.0.8"
[dev-dependencies]
expectorate = "1.0"
paste = "1.0"
prettyplease = "0.1"
rustfmt-wrapper = "0.2"
schema = "0.0.1"
schemars = { version = "0.8.12", features = ["uuid1"] }
serde = "1.0"
Expand Down
7 changes: 0 additions & 7 deletions typify-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use log::info;
use output::OutputSpace;
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use rustfmt_wrapper::rustfmt;
use schemars::schema::{Metadata, Schema};
use thiserror::Error;
use type_entry::{
Expand Down Expand Up @@ -801,12 +800,6 @@ impl TypeSpace {
}
}

impl ToString for TypeSpace {
fn to_string(&self) -> String {
rustfmt(self.to_stream().to_string()).unwrap()
}
}

impl ToTokens for TypeSpace {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.extend(self.to_stream())
Expand Down
234 changes: 1 addition & 233 deletions typify-impl/tests/generator.out
Original file line number Diff line number Diff line change
@@ -1,233 +1 @@
mod types {
#[derive(
Clone, Debug, Deserialize, Eq, Hash, JsonSchema, Ord, PartialEq, PartialOrd, Serialize,
)]
pub struct AllTheTraits {
pub ok: String,
}
impl From<&AllTheTraits> for AllTheTraits {
fn from(value: &AllTheTraits) -> Self {
value.clone()
}
}
impl AllTheTraits {
pub fn builder() -> builder::AllTheTraits {
builder::AllTheTraits::default()
}
}
#[derive(Clone, Debug, Deserialize, JsonSchema, Serialize)]
pub struct CompoundType {
pub value1: String,
pub value2: u64,
}
impl From<&CompoundType> for CompoundType {
fn from(value: &CompoundType) -> Self {
value.clone()
}
}
impl CompoundType {
pub fn builder() -> builder::CompoundType {
builder::CompoundType::default()
}
}
#[derive(Clone, Debug, Deserialize, JsonSchema, Serialize)]
pub struct Pair {
#[serde(default = "defaults::pair_a")]
pub a: StringEnum,
#[serde(default = "defaults::pair_b")]
pub b: StringEnum,
}
impl From<&Pair> for Pair {
fn from(value: &Pair) -> Self {
value.clone()
}
}
impl Pair {
pub fn builder() -> builder::Pair {
builder::Pair::default()
}
}
#[derive(
Clone, Copy, Debug, Deserialize, Eq, Hash, JsonSchema, Ord, PartialEq, PartialOrd, Serialize,
)]
pub enum StringEnum {
One,
Two,
BuckleMyShoe,
}
impl From<&StringEnum> for StringEnum {
fn from(value: &StringEnum) -> Self {
value.clone()
}
}
impl ToString for StringEnum {
fn to_string(&self) -> String {
match *self {
Self::One => "One".to_string(),
Self::Two => "Two".to_string(),
Self::BuckleMyShoe => "BuckleMyShoe".to_string(),
}
}
}
impl std::str::FromStr for StringEnum {
type Err = &'static str;
fn from_str(value: &str) -> Result<Self, &'static str> {
match value {
"One" => Ok(Self::One),
"Two" => Ok(Self::Two),
"BuckleMyShoe" => Ok(Self::BuckleMyShoe),
_ => Err("invalid value"),
}
}
}
impl std::convert::TryFrom<&str> for StringEnum {
type Error = &'static str;
fn try_from(value: &str) -> Result<Self, &'static str> {
value.parse()
}
}
impl std::convert::TryFrom<&String> for StringEnum {
type Error = &'static str;
fn try_from(value: &String) -> Result<Self, &'static str> {
value.parse()
}
}
impl std::convert::TryFrom<String> for StringEnum {
type Error = &'static str;
fn try_from(value: String) -> Result<Self, &'static str> {
value.parse()
}
}
mod builder {
pub struct AllTheTraits {
ok: Result<String, String>,
}
impl Default for AllTheTraits {
fn default() -> Self {
Self {
ok: Err("no value supplied for ok".to_string()),
}
}
}
impl AllTheTraits {
pub fn ok<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.ok = value
.try_into()
.map_err(|e| format!("error converting supplied value for ok: {}", e));
self
}
}
impl std::convert::TryFrom<AllTheTraits> for super::AllTheTraits {
type Error = String;
fn try_from(value: AllTheTraits) -> Result<Self, String> {
Ok(Self { ok: value.ok? })
}
}
pub struct CompoundType {
value1: Result<String, String>,
value2: Result<u64, String>,
}
impl Default for CompoundType {
fn default() -> Self {
Self {
value1: Err("no value supplied for value1".to_string()),
value2: Err("no value supplied for value2".to_string()),
}
}
}
impl CompoundType {
pub fn value1<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.value1 = value
.try_into()
.map_err(|e| format!("error converting supplied value for value1: {}", e));
self
}
pub fn value2<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<u64>,
T::Error: std::fmt::Display,
{
self.value2 = value
.try_into()
.map_err(|e| format!("error converting supplied value for value2: {}", e));
self
}
}
impl std::convert::TryFrom<CompoundType> for super::CompoundType {
type Error = String;
fn try_from(value: CompoundType) -> Result<Self, String> {
Ok(Self {
value1: value.value1?,
value2: value.value2?,
})
}
}
pub struct Pair {
a: Result<super::StringEnum, String>,
b: Result<super::StringEnum, String>,
}
impl Default for Pair {
fn default() -> Self {
Self {
a: Ok(super::defaults::pair_a()),
b: Ok(super::defaults::pair_b()),
}
}
}
impl Pair {
pub fn a<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<super::StringEnum>,
T::Error: std::fmt::Display,
{
self.a = value
.try_into()
.map_err(|e| format!("error converting supplied value for a: {}", e));
self
}
pub fn b<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<super::StringEnum>,
T::Error: std::fmt::Display,
{
self.b = value
.try_into()
.map_err(|e| format!("error converting supplied value for b: {}", e));
self
}
}
impl std::convert::TryFrom<Pair> for super::Pair {
type Error = String;
fn try_from(value: Pair) -> Result<Self, String> {
Ok(Self {
a: value.a?,
b: value.b?,
})
}
}
}
mod defaults {
pub(super) fn pair_a() -> super::StringEnum {
super::StringEnum::One
}
pub(super) fn pair_b() -> super::StringEnum {
super::StringEnum::Two
}
}
}
pub fn do_stuff(
body: &types::CompoundType,
string: &str,
opt_int: Option<u32>,
strenum: types::StringEnum,
) -> types::CompoundType {
todo!()
}
mod types { # [derive (Clone , Debug , Deserialize , Eq , Hash , JsonSchema , Ord , PartialEq , PartialOrd , Serialize)] pub struct AllTheTraits { pub ok : String , } impl From < & AllTheTraits > for AllTheTraits { fn from (value : & AllTheTraits) -> Self { value . clone () } } impl AllTheTraits { pub fn builder () -> builder :: AllTheTraits { builder :: AllTheTraits :: default () } } # [derive (Clone , Debug , Deserialize , JsonSchema , Serialize)] pub struct CompoundType { pub value1 : String , pub value2 : u64 , } impl From < & CompoundType > for CompoundType { fn from (value : & CompoundType) -> Self { value . clone () } } impl CompoundType { pub fn builder () -> builder :: CompoundType { builder :: CompoundType :: default () } } # [derive (Clone , Debug , Deserialize , JsonSchema , Serialize)] pub struct Pair { # [serde (default = "defaults::pair_a")] pub a : StringEnum , # [serde (default = "defaults::pair_b")] pub b : StringEnum , } impl From < & Pair > for Pair { fn from (value : & Pair) -> Self { value . clone () } } impl Pair { pub fn builder () -> builder :: Pair { builder :: Pair :: default () } } # [derive (Clone , Copy , Debug , Deserialize , Eq , Hash , JsonSchema , Ord , PartialEq , PartialOrd , Serialize)] pub enum StringEnum { One , Two , BuckleMyShoe , } impl From < & StringEnum > for StringEnum { fn from (value : & StringEnum) -> Self { value . clone () } } impl ToString for StringEnum { fn to_string (& self) -> String { match * self { Self :: One => "One" . to_string () , Self :: Two => "Two" . to_string () , Self :: BuckleMyShoe => "BuckleMyShoe" . to_string () , } } } impl std :: str :: FromStr for StringEnum { type Err = & 'static str ; fn from_str (value : & str) -> Result < Self , & 'static str > { match value { "One" => Ok (Self :: One) , "Two" => Ok (Self :: Two) , "BuckleMyShoe" => Ok (Self :: BuckleMyShoe) , _ => Err ("invalid value") , } } } impl std :: convert :: TryFrom < & str > for StringEnum { type Error = & 'static str ; fn try_from (value : & str) -> Result < Self , & 'static str > { value . parse () } } impl std :: convert :: TryFrom < & String > for StringEnum { type Error = & 'static str ; fn try_from (value : & String) -> Result < Self , & 'static str > { value . parse () } } impl std :: convert :: TryFrom < String > for StringEnum { type Error = & 'static str ; fn try_from (value : String) -> Result < Self , & 'static str > { value . parse () } } mod builder { pub struct AllTheTraits { ok : Result < String , String > , } impl Default for AllTheTraits { fn default () -> Self { Self { ok : Err ("no value supplied for ok" . to_string ()) , } } } impl AllTheTraits { pub fn ok < T > (mut self , value : T) -> Self where T : std :: convert :: TryInto < String > , T :: Error : std :: fmt :: Display , { self . ok = value . try_into () . map_err (| e | format ! ("error converting supplied value for ok: {}" , e)) ; self } } impl std :: convert :: TryFrom < AllTheTraits > for super :: AllTheTraits { type Error = String ; fn try_from (value : AllTheTraits) -> Result < Self , String > { Ok (Self { ok : value . ok ? , }) } } pub struct CompoundType { value1 : Result < String , String > , value2 : Result < u64 , String > , } impl Default for CompoundType { fn default () -> Self { Self { value1 : Err ("no value supplied for value1" . to_string ()) , value2 : Err ("no value supplied for value2" . to_string ()) , } } } impl CompoundType { pub fn value1 < T > (mut self , value : T) -> Self where T : std :: convert :: TryInto < String > , T :: Error : std :: fmt :: Display , { self . value1 = value . try_into () . map_err (| e | format ! ("error converting supplied value for value1: {}" , e)) ; self } pub fn value2 < T > (mut self , value : T) -> Self where T : std :: convert :: TryInto < u64 > , T :: Error : std :: fmt :: Display , { self . value2 = value . try_into () . map_err (| e | format ! ("error converting supplied value for value2: {}" , e)) ; self } } impl std :: convert :: TryFrom < CompoundType > for super :: CompoundType { type Error = String ; fn try_from (value : CompoundType) -> Result < Self , String > { Ok (Self { value1 : value . value1 ? , value2 : value . value2 ? , }) } } pub struct Pair { a : Result < super :: StringEnum , String > , b : Result < super :: StringEnum , String > , } impl Default for Pair { fn default () -> Self { Self { a : Ok (super :: defaults :: pair_a ()) , b : Ok (super :: defaults :: pair_b ()) , } } } impl Pair { pub fn a < T > (mut self , value : T) -> Self where T : std :: convert :: TryInto < super :: StringEnum > , T :: Error : std :: fmt :: Display , { self . a = value . try_into () . map_err (| e | format ! ("error converting supplied value for a: {}" , e)) ; self } pub fn b < T > (mut self , value : T) -> Self where T : std :: convert :: TryInto < super :: StringEnum > , T :: Error : std :: fmt :: Display , { self . b = value . try_into () . map_err (| e | format ! ("error converting supplied value for b: {}" , e)) ; self } } impl std :: convert :: TryFrom < Pair > for super :: Pair { type Error = String ; fn try_from (value : Pair) -> Result < Self , String > { Ok (Self { a : value . a ? , b : value . b ? , }) } } } mod defaults { pub (super) fn pair_a () -> super :: StringEnum { super :: StringEnum :: One } pub (super) fn pair_b () -> super :: StringEnum { super :: StringEnum :: Two } } } pub fn do_stuff (body : & types :: CompoundType , string : & str , opt_int : Option < u32 > , strenum : types :: StringEnum ,) -> types :: CompoundType { todo ! () }
Loading