@@ -8,7 +8,7 @@ use gleam_core::{
88 build:: { Codegen , Compile , Mode , Options , Package , Target } ,
99 config:: { GleamVersion , PackageConfig , SpdxLicense } ,
1010 docs:: { Dependency , DependencyKind , DocContext } ,
11- error:: { SmallVersion , wrap} ,
11+ error:: { InvalidReadmeReason , SmallVersion , wrap} ,
1212 hex,
1313 manifest:: ManifestPackageSource ,
1414 paths:: { self , ProjectPaths } ,
@@ -31,7 +31,7 @@ pub fn command(paths: &ProjectPaths, replace: bool, i_am_sure: bool) -> Result<(
3131 && check_for_version_zero ( & config) ?
3232 && check_repo_url ( & config, i_am_sure) ?;
3333
34- check_for_default_readme ( & config, paths) ?;
34+ check_for_invalid_readme ( & config, paths) ?;
3535
3636 if !should_publish {
3737 println ! ( "Not publishing." ) ;
@@ -132,33 +132,43 @@ HTML documentation will work:
132132 Ok ( ( ) )
133133}
134134
135- fn check_for_default_readme ( config : & PackageConfig , paths : & ProjectPaths ) -> Result < ( ) , Error > {
136- let default_readme = default_readme ( config. name . as_str ( ) ) ;
135+ fn check_for_invalid_readme ( config : & PackageConfig , paths : & ProjectPaths ) -> Result < ( ) , Error > {
136+ let normalise = |string : String | {
137+ string
138+ . trim ( )
139+ . replace ( "\r \n " , "" )
140+ . replace ( "\n " , "" )
141+ . replace ( "\t " , "" )
142+ . replace ( " " , "" )
143+ } ;
144+
137145 let project_readme = match fs:: read ( paths. readme ( ) ) {
138146 Err ( Error :: FileIo {
139147 err : Some ( message) , ..
140148 } ) if message. contains ( "No such file or directory" ) => {
141- return Err ( Error :: CannotPublishWithNoReadme ) ;
149+ return Err ( Error :: CannotPublishWithInvalidReadme {
150+ reason : InvalidReadmeReason :: Missing ,
151+ } ) ;
142152 }
143153 Err ( error) => return Err ( error) ,
144154 Ok ( project_readme) => project_readme,
145155 } ;
146156
147- // We consider the two READMEs equal modulo whitespace, otherwise it would
148- // be pretty trivial to trick this check by just formatting the default
149- // README differently.
150- let normalise = |string : String | {
151- string
152- . replace ( "\r \n " , "" )
153- . replace ( "\n " , "" )
154- . replace ( "\t " , "" )
155- . replace ( " " , "" )
156- } ;
157- if normalise ( project_readme) == normalise ( default_readme) {
158- Err ( Error :: CannotPublishWithDefaultReadme )
159- } else {
160- Ok ( ( ) )
157+ let normalised_project_readme = normalise ( project_readme) ;
158+ if normalised_project_readme. is_empty ( ) {
159+ return Err ( Error :: CannotPublishWithInvalidReadme {
160+ reason : InvalidReadmeReason :: Empty ,
161+ } ) ;
161162 }
163+
164+ let default_readme = default_readme ( config. name . as_str ( ) ) ;
165+ if normalised_project_readme == normalise ( default_readme) {
166+ return Err ( Error :: CannotPublishWithInvalidReadme {
167+ reason : InvalidReadmeReason :: Default ,
168+ } ) ;
169+ }
170+
171+ Ok ( ( ) )
162172}
163173
164174fn check_for_name_squatting ( package : & Package ) -> Result < ( ) , Error > {
0 commit comments