@@ -218,6 +218,36 @@ impl Display for AliasName {
218218 }
219219}
220220
221+ #[ derive( Clone , Debug ) ]
222+ pub struct ContractName ( String ) ;
223+
224+ impl std:: ops:: Deref for ContractName {
225+ type Target = str ;
226+ fn deref ( & self ) -> & Self :: Target {
227+ & self . 0
228+ }
229+ }
230+
231+ impl std:: str:: FromStr for ContractName {
232+ type Err = Error ;
233+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
234+ validate_name ( s) ?;
235+ Ok ( ContractName ( s. to_string ( ) ) )
236+ }
237+ }
238+
239+ impl Display for ContractName {
240+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
241+ write ! ( f, "{}" , self . 0 )
242+ }
243+ }
244+
245+ impl AsRef < std:: path:: Path > for ContractName {
246+ fn as_ref ( & self ) -> & std:: path:: Path {
247+ std:: path:: Path :: new ( & self . 0 )
248+ }
249+ }
250+
221251#[ cfg( test) ]
222252mod tests {
223253 use super :: * ;
@@ -272,4 +302,29 @@ mod tests {
272302 fn alias_name_rejects_empty ( ) {
273303 assert ! ( "" . parse:: <AliasName >( ) . is_err( ) ) ;
274304 }
305+
306+ #[ test]
307+ fn contract_name_valid ( ) {
308+ assert ! ( "hello-world" . parse:: <ContractName >( ) . is_ok( ) ) ;
309+ assert ! ( "my_contract_123" . parse:: <ContractName >( ) . is_ok( ) ) ;
310+ }
311+
312+ #[ test]
313+ fn contract_name_rejects_path_traversal ( ) {
314+ assert ! ( "../evil" . parse:: <ContractName >( ) . is_err( ) ) ;
315+ assert ! ( "../../etc/passwd" . parse:: <ContractName >( ) . is_err( ) ) ;
316+ assert ! ( "foo/bar" . parse:: <ContractName >( ) . is_err( ) ) ;
317+ assert ! ( "foo\\ bar" . parse:: <ContractName >( ) . is_err( ) ) ;
318+ }
319+
320+ #[ test]
321+ fn contract_name_rejects_too_long ( ) {
322+ assert ! ( "a" . repeat( 251 ) . parse:: <ContractName >( ) . is_err( ) ) ;
323+ assert ! ( "a" . repeat( 250 ) . parse:: <ContractName >( ) . is_ok( ) ) ;
324+ }
325+
326+ #[ test]
327+ fn contract_name_rejects_empty ( ) {
328+ assert ! ( "" . parse:: <ContractName >( ) . is_err( ) ) ;
329+ }
275330}
0 commit comments