1 // Written in the D programming language.
2 
3 /**
4 A type for representing the name object of the 9P / Styx protocol. 
5 
6 Copyright: LightHouse Software, 2021
7 License:   $(HTTP https://github.com/aquaratixc/ESL-License, Experimental Software License 1.0).
8 Authors:   Oleg Bakharev,
9 		   Ilya Pertsev
10 */
11 module styx2000.protobj.name;
12 
13 private {
14 	import std..string : format;
15 	
16 	import styx2000.lowlevel.endianness;
17 	import styx2000.lowlevel.vls;
18 	
19 	import styx2000.protobj.styxobject;
20 }
21 
22 /**
23 	A class that provides a type for the name field in some Styx messages. Inherits methods from the Name class and the StyxObject class. 
24 	See_Also:
25 		https://web.archive.org/web/20201029184954/https://powerman.name/Inferno/man/5/0intro.html
26 */
27 class Name : StyxObject
28 {
29 	protected {
30 		string _name;
31 		
32 		ubyte[] _representation;
33 	}
34 	
35 	/**
36 	A constructor that creates an object of the Name class with the given parameter in the form of some string value representing names. 
37 	If called without parameters, then the default parameter is empty string value. 
38     Params:
39     name = String value for name.
40     
41     Typical usage:
42     ----
43     Name name = new Name(`test`);
44     ----
45     */
46 	this(string name = "")
47 	{
48 		_name = name;
49 		_representation = VariableLengthSequence.pack(cast(ubyte[]) name);
50 	}
51 	
52 	/// Get name from Name object
53 	string getName()
54 	{
55 		return _name;
56 	}
57 	
58 	/// Set mode from string object
59 	void setName(string name)
60 	{
61 		_name = name;
62 		_representation = VariableLengthSequence.pack(cast(ubyte[]) name);
63 	}
64 	
65 	/// Pack to bytes array
66 	ubyte[] pack()
67 	{
68 		return _representation;
69 	}
70 	
71 	/// Unpack from bytes array
72 	void unpack(ubyte[] bytes...)
73 	{
74 		_name = cast(string) (VariableLengthSequence.unpack(bytes));
75 		_representation = VariableLengthSequence.pack(cast(ubyte[]) _name);
76 	}
77 	
78 	/// Convenient string representation of an object for printing 
79 	override string toString()
80 	{
81 		return format(
82 			`Name(name="%s")`, 
83 			_name == "" ? `` : _name
84 		);
85 	}
86 	
87 	/// An alias for easier packing into a byte array without having to manually call the pack() method
88 	alias pack this;
89 }