moved mize into it's part folder

This commit is contained in:
Sebastian Moser
2026-02-01 13:11:51 +01:00
parent 2a76a3f14b
commit df51cf3347
41 changed files with 5 additions and 5 deletions

2886
parts/mize/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

97
parts/mize/Cargo.toml Normal file
View File

@@ -0,0 +1,97 @@
[package]
name = "mize"
version = "0.0.1"
edition = "2021"
license = "GPL-3.0"
description = 'A strongly typed "filesystem" for the age of connectivity.'
repository = "https://github.com/c2vi/mize"
[package.metadata.wasm-pack.profile.dev]
wasm-opt = false
[lib]
name = "mize"
path = "src/lib.rs"
crate-type = ["cdylib", "rlib"]
[[bin]]
name = "mize"
path = "src/platform/os/main.rs"
required-features = [ "os-target" ]
[features]
default = [ "os-target" ]
os-target = [ "clap", "home", "nix", "sysinfo", "ciborium/default", "tracing-subscriber", "async", "tracing-subscriber/env-filter", "libloading", "ciborium-io", "tracing-core", "tar", "flate2", "http_req" ]
wasm-target = [ "wasm-bindgen", "console_error_panic_hook", "wasm-bindgen-cli", "web-sys", "web-sys/Worker", "web-sys/Window", "web-sys/Request", "web-sys/RequestInit", "web-sys/RequestMode", "web-sys/Response", "web-sys/WorkerOptions", "web-sys/WorkerType", "serde-wasm-bindgen", "wasm-bindgen-futures", "wee_alloc"]
async = ["tokio/net", "tokio", "tokio/rt-multi-thread", "tokio/io-util"]
[dependencies]
# core dependencies
colored = "2.0.0"
uuid = "1.3.0"
ciborium = { version = "0.2.2", default-features = false }
serde = "1.0.203"
interner = { git = "https://github.com/c2vi/interner" }
tracing = "0.1.40"
serde_json = { version = "1.0.117", optional = false }
toml = { version = "0.8.13", features = ["parse"], optional = false }
dyn-clone = "1.0.17"
sha2 = "0.10.8"
flume = "0.11.1"
# buildin modules
#mize_module_blob = { path = "./modules/modules/Blob" }
# wasm dependencies
wasm-bindgen = { version = "0.2.84", optional = true }
console_error_panic_hook = { version = "0.1.7", optional = true }
wasm-bindgen-cli = { version = "0.2.92", optional = true }
web-sys = { version = "0.3.70", optional = true }
serde-wasm-bindgen = { version = "0.6.5", optional = true }
wasm-bindgen-futures = { version = "0.4.49", optional = true }
wee_alloc = { version = "0.4.5", optional = true }
# os dependencies
clap = { version = "4.4.11", features = ["cargo"], optional = true }
home = { version = "0.5.9", optional = true }
daemonize = { version = "0.5.0", optional = true }
nix = { version = "0.27.1", features = ["signal"], optional = true }
sysinfo = { version = "0.30.12", optional = true }
tracing-subscriber = { version = "0.3.18", optional = true }
tokio = { version = "1.46.1", features = ["sync"], optional = true }
libloading = { version = "0.8.5", optional = true }
ciborium-io = { version = "0.2.2", optional = true }
tracing-core = { version = "0.1.32", optional = true }
tar = { version = "0.4.42", optional = true }
flate2 = { version = "1.0.33", optional = true }
#http_req = { version = "0.12.0", default-features = false, features = [ "rustls" ], optional = true }
http_req = { git = "https://github.com/c2vi/http_req", optional = true, default-features = false, features = [ "native-tls" ] }
# bare dependencies
# unused dependencies
axum = { version = "0.6.1", features = ["ws", "headers"], optional = true }
axum-extra = { version = "0.4.2", features = ["spa"], optional = true }
axum-macros = { version = "0.3.0", optional = true }
#tokio-core = "0.1.18"
#tokio-io = "0.1.13"
#tokio-stream = { version = "0.1.9", optional = true }
#derive_more = "0.99.17"
#futures-util = "0.3.24"
#itertools = "0.10.5"
#lazy_static = "1.4.0"
#rmp-serde = "1.1.1"
#rmpv = { version = "1.0.0", features = ["serde", "with-serde"] }
#toml = "0.5.10"
#tower-http = "0.3.5"
[dev-dependencies]
wasm-bindgen-test = { version = "0.3.34" }
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

674
parts/mize/LICENSE Normal file
View File

@@ -0,0 +1,674 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<https://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<https://www.gnu.org/licenses/why-not-lgpl.html>.

28
parts/mize/build.rs Normal file
View File

@@ -0,0 +1,28 @@
use std::process::Command;
use std::env;
fn main() {
// compile the string plugin
//let _out_dir = env::var_os("OUT_DIR").unwrap();
//let mut string_module = Command::new("bash");
//string_module.arg("-c");
//string_module.arg("echo hiiiiiiiiiiiiiiiii");
//string_module.arg("--help");
//string_module.arg("--out-dir");
//string_module.arg(out_dir);
//string_module.env("PWD", "/hoem/me/work/mize/module/modules/Blob");
//let _res = string_module.spawn();
//let _child = Command::new("cargo")
//.arg("build")
//.arg("--lib")
//.arg("--manifest-path")
//.arg("/home/me/work/mize/modules/modules/Blob/Cargo.toml")
//.spawn()
//.expect("failed to start wasm build");
//to outdir (), using the nightly feature --out-dir to have the result there. By default I get a libhearts.rlib
println!("cargo:rustc-link-arg=-zstack-size=0x1000000");
}

45
parts/mize/deploy Executable file
View File

@@ -0,0 +1,45 @@
set -e
if [[ "$1" == "a" ]]
then
ssh acern mkdir -p /tmp/mize-src
ssh acern mkdir -p ~/work/modules
rsync -rv --exclude="target" --exclude="gitignore" ./* acern:/tmp/mize-src/
rsync -rv --exclude="target" --exclude="gitignore" --exclude="node_modules" ~/work/modules acern:work/
path=$(ssh acern /home/me/out/nix/bin/nix build /tmp/mize-src#dist -L --print-out-paths)
echo doneeeeeeeeeeeeeee building
echo path: $path
#scp -r acern:$path/* ocih:host/webserver/webfiles
tmp=$(mktemp -d)
[[ "$path" != "" ]] && rsync -rv -p --chmod=u+w acern:$path/* $tmp
[[ "$path" != "" ]] && rsync -rv $tmp/* ocih:host/data/my-website --rsync-path="sudo rsync"
elif [[ "$1" == "c" ]] # only copy files to acern
then
ssh acern mkdir -p /tmp/mize-src
ssh acern mkdir -p ~/work/modules
rsync -rv --exclude="target" --exclude="gitignore" ./* acern:/tmp/mize-src/
rsync -rv --exclude="target" --exclude="gitignore" --exclude="node_modules" ~/work/modules acern:work/
else
path=$(nix build .#dist -L -v --print-out-paths $@)
echo doneeeeeeeeeeeeeee building
echo path: $path
#scp -r $path/* ocih:host/webserver/webfiles
[[ "$path" != "" ]] && rsync -rv $path/* ocih:host/data/my-website --rsync-path="sudo rsync"
fi

2
parts/mize/dev Normal file
View File

@@ -0,0 +1,2 @@
nix develop .#mizeFor.x86_64-linux-gnu.main.devShell --override-input nixpkgs github:NixOS/nixpkgs/ea6033ce4ddf2c0ede2ea147a450d4f880796128

View File

@@ -0,0 +1,22 @@
# Use mize in a js project
## On the Web
```bash
npm init wasm-app project_dir
cd project_dir
npm install @c2vi/mize
```
## Build npm package yourself
### with nix
```bash
nix build github:c2vi/mize#npmPackage
```
### without nix
```bash
wasm-pack build -- --features wasm-target
```

154
parts/mize/flake.lock generated Normal file
View File

@@ -0,0 +1,154 @@
{
"nodes": {
"crane": {
"locked": {
"lastModified": 1725125250,
"narHash": "sha256-CB20rDD5eHikF6mMTTJdwPP1qvyoiyyw1RDUzwIaIF8=",
"owner": "ipetkov",
"repo": "crane",
"rev": "96fd12c7100e9e05fa1a0a5bd108525600ce282f",
"type": "github"
},
"original": {
"owner": "ipetkov",
"repo": "crane",
"type": "github"
}
},
"fenix": {
"inputs": {
"nixpkgs": [
"nixpkgs"
],
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1748327829,
"narHash": "sha256-TJwbWCsyT9yJkBRTnqKE/YDTiXuEdYHb37WLK4TcAwE=",
"owner": "nix-community",
"repo": "fenix",
"rev": "26fbcc33a5e94d6d0dc5f467eca8f7a746a54e6c",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "fenix",
"type": "github"
}
},
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1701680307,
"narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "4022d587cbbfd70fe950c1e2083a02621806a725",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"mize_modules": {
"flake": false,
"locked": {
"lastModified": 1725362504,
"narHash": "sha256-U0IQ+3SiyLb24BTDUvlnE30LdjjJWJ/P6dHPqxArJXk=",
"owner": "c2vi",
"repo": "mize-modules",
"rev": "fa3b462cbe3e63fc9ecada6c4445d366e58a6f86",
"type": "github"
},
"original": {
"owner": "c2vi",
"repo": "mize-modules",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1703637592,
"narHash": "sha256-8MXjxU0RfFfzl57Zy3OfXCITS0qWDNLzlBAdwxGZwfY=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "cfc3698c31b1fb9cdcf10f36c9643460264d0ca8",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"crane": "crane",
"fenix": "fenix",
"flake-utils": "flake-utils",
"mize_modules": "mize_modules",
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}
},
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1748295258,
"narHash": "sha256-ejRVtsT7fat4mGgDQpgIGKJP+vbr9r4Mo84jRb2bndk=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "7fa66d67a72bcad236b50907e14f7464c47ecede",
"type": "github"
},
"original": {
"owner": "rust-lang",
"ref": "nightly",
"repo": "rust-analyzer",
"type": "github"
}
},
"rust-overlay": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1725157860,
"narHash": "sha256-DhqyM7XJYKj+WAEaYwMtXaYX66tA+lOd31sd5QkxLDM=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "1fd72e343c6890f695243a37b367a1e3b90a49ee",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

225
parts/mize/flake.nix Normal file
View File

@@ -0,0 +1,225 @@
{
inputs = {
# nixpkgs 35.11 still contains rust 1.73
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
crane = {
url = "github:ipetkov/crane";
};
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
mize_modules = {
url = "github:c2vi/mize-modules?submodules=1";
flake = false;
};
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, flake-utils, nixpkgs, fenix, crane, mize_modules, rust-overlay, ... }@inputs: flake-utils.lib.eachDefaultSystem (system:
############################## LET BINDINGS ##############################
let
pkgs = nixpkgs.legacyPackages.${system};
wasmToolchain = fenix.packages.${system}.combine [
fenix.packages.${system}.targets.wasm32-unknown-unknown.stable.toolchain
fenix.packages.${system}.stable.toolchain
];
wasmCrane = (crane.mkLib pkgs).overrideToolchain wasmToolchain;
osToolchain = fenix.packages.${system}.stable.toolchain;
osCrane = (crane.mkLib pkgs).overrideToolchain osToolchain;
#winToolchain = fenix.packages.${system}.combine [
#fenix.packages.${system}.targets.x86_64-pc-windows-gnu.latest.toolchain
#fenix.packages.${system}.latest.toolchain
#];
winToolchain = with fenix.packages.${system};
combine [
minimal.rustc
minimal.cargo
targets.x86_64-pc-windows-gnu.latest.rust-std
];
winCrane = (crane.mkLib pkgs).overrideToolchain winToolchain;
wasmArtifacts = wasmCrane.buildDepsOnly ({
src = self;
doCheck = false; # tests does not work in wasm
CARGO_BUILD_TARGET = "wasm32-unknown-unknown";
cargoExtraArgs = "--features wasm-target --no-default-features";
RUSTFLAGS="-C linker=wasm-ld";
buildInputs = with pkgs; [ cargo-binutils lld ];
});
defaultMizeConfig = {
config = {
namespace = "mize.buildtime.ns";
module_url = "c2vi.dev";
};
};
#systems = [ "aarch64-linux" "x86_64-linux" "x86_64-pc-windows-gnu" "wasm32-unknown-none-unknown" "x86_64-apple-darwin" "aarch64-apple-darwin" ];
systems = [ "aarch64-unknown-linux-gnu" "x86_64-unknown-linux-gnu" "x86_64-pc-windows-gnu" "wasm32-unknown-none-unknown" ];
mizeLib = import ./lib.nix {
inherit inputs nixpkgs pkgs self osCrane defaultMizeConfig mize_modules;
inherit rust-overlay crane fenix;
buildSystem = system;
stdenv = pkgs.stdenv;
};
in {
############################## PACKAGES ##############################
packages = rec {
inherit osCrane;
#test = mizeLib.buildMizeForSystem "wasm32-unknown-none-unknown";
test = mizeLib.buildMizeForSystem "aarch64-linux";
#test = mizeLib.buildMizeForSystem "x86_64-pc-windows-gnu";
mizeFor = let
mizes = map mizeLib.buildMizeForSystem systems;
in builtins.listToAttrs ( map ( mize: { name = mize.system.name; value = mize; } ) mizes );
dist = mizeLib.dist systems;
random = mizeLib.random systems;
inherit mizeLib;
#pkgsCross = import nixpkgs { buildSystem = system; crossSystem = { config = "x86_64-pc-windows-gnu"; }; overlays = [ rust-overlay.overlays.default ]; };
pkgsTest = pkgs;
#pkgsCross = import nixpkgs { buildSystem = system; crossSystem = { config = "x86_64-w64-windows-mingw"; }; overlays = [ rust-overlay.overlays.default ]; };
pkgsCross = import nixpkgs { buildSystem = system; crossSystem = { config = "x86_64-unknown-linux-gnu"; }; overlays = [ rust-overlay.overlays.default ]; };
craneLib = (crane.mkLib pkgsCross).overrideToolchain (p: p.rust-bin.stable.latest.default);
one = craneLib.buildPackage {
src = "${self}";
#cargoExtraArgs = "--bin mize --features os-target";
cargoExtraArgs = "--bin mize";
doCheck = false; # tests does not work in wasm
CARGO_BUILD_TARGET = "x86_64-pc-windows-gnu";
MIZE_BUILD_CONFIG = pkgs.writeTextFile {
name = "mize-build-config";
text = builtins.toJSON (defaultMizeConfig // {
mize_version = one.version;
});
};
};
default = osCrane.buildPackage {
src = "${self}";
cargoExtraArgs = "--bin mize --features os-target";
};
wasm = wasmCrane.buildPackage {
src = "${self}";
CARGO_BUILD_TARGET = "wasm32-unknown-unknown";
cargoExtraArgs = "--bin mize --features wasm-target";
doCheck = false;
};
inherit wasmArtifacts;
# Thanks to:
# - https://github.com/dl-solarity/hardhat-diamond-tools/blob/e5b8bc9624fbf89eda83a8ce4f6b6672ea83f550/flake.nix#L103
# - https://github.com/ipetkov/crane/issues/362#issuecomment-1683220603
npmPackage = wasmCrane.mkCargoDerivation {
name = "mize-npm-package";
# i can't use CARGO_BUILD_TARGET to set the target
# because then the cargo install run by wasm-pack tries to build the wasm-bindgen-cli for a wasm target...
src = self;
cargoArtifacts = wasmArtifacts;
doCheck = false;
buildPhaseCargoCommand = ''
echo build hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
mkdir -p $out/pkg
export HOME=$(mktemp -d fake-homeXXXX)
wasm-pack build --out-dir $out/pkg --scope=c2vi -- --no-default-features --features wasm-target
'';
buildInputs = with pkgs; [ wasm-bindgen-cli binaryen wasm-pack wasmToolchain ];
};
};
############################## DEV SHELLS ##############################
devShells = {
modules = mizeLib.moduleShells system;
modulesCross = pkgs.lib.genAttrs systems (system: mizeLib.moduleShells system);
one = pkgs.stdenv.mkDerivation {
name = "hiiiiiiii";
nativeBuildInputs = [
(fenix.packages.${system}.combine [
fenix.packages.${system}.targets.wasm32-unknown-unknown.latest.toolchain
fenix.packages.${system}.latest.toolchain
fenix.packages.${system}.latest.rust-src
])
];
};
win = winCrane.buildPackage {
#src = winCrane.cleanCargoSource ./testing;
#pname = "testing";
src = ./testing;
strictDeps = true;
doCheck = false;
CARGO_BUILD_TARGET = "x86_64-pc-windows-gnu";
# fixes issues related to libring
TARGET_CC = "${pkgs.pkgsCross.mingwW64.stdenv.cc}/bin/${pkgs.pkgsCross.mingwW64.stdenv.cc.targetPrefix}cc";
#fixes issues related to openssl
OPENSSL_DIR = "${pkgs.openssl.dev}";
OPENSSL_LIB_DIR = "${pkgs.openssl.out}/lib";
OPENSSL_INCLUDE_DIR = "${pkgs.openssl.dev}/include/";
depsBuildBuild = with pkgs; [
pkgsCross.mingwW64.stdenv.cc
pkgsCross.mingwW64.windows.pthreads
];
shellHook = ''
export MIZE_CONFIG_FILE=${self}/test-config.toml
'';
MIZE_BUILD_CONFIG = pkgs.writeTextFile {
name = "vic-build-config";
text = builtins.toJSON defaultMizeConfig;
};
};
default = (mizeLib.buildMizeForSystem system).modules.mize.devShell;
};
}) // {
############################## SOME GLOBAL OUTPUTS ##############################
inherit inputs self;
rustPkgs = import nixpkgs { overlays = [rust-overlay.overlays.default]; system = "x86_64-linux"; };
};
}

392
parts/mize/lib.nix Normal file
View File

@@ -0,0 +1,392 @@
{ pkgs
, osCrane
, self
, defaultMizeConfig
, mize_modules
, buildSystem
, nixpkgs
, rust-overlay
, crane
, fenix
, stdenv
, lib ? pkgs.lib
, ...
}:
rec {
getCrossSystem = system: let
hostSystem = (if builtins.isString system then (pkgs.lib.systems.parse.mkSystemFromString system) else system)
// rec {
name = if hostSystem.vendor != "unknown"
then "${hostSystem.cpu.name}-${hostSystem.kernel.name}-${hostSystem.abi.name}"
else "${hostSystem.cpu.name}-${hostSystem.vendor.name}-${hostSystem.kernel.name}-${hostSystem.abi.name}";
nameFull = "${hostSystem.cpu.name}-${hostSystem.vendor.name}-${hostSystem.kernel.name}-${hostSystem.abi.name}";
nameRust = nameFull;
config = nameFull;
isCross = system != buildSystem;
isNative = system == buildSystem;
};
in hostSystem;
# get a list of all modules
# aka folders with a mize.nix in them
module_list_drv = path: pkgs.stdenv.mkDerivation {
name = "mize_module_list";
dontUnpack = true;
configurePhase = "";
buildPhase = ''
mkdir -p $out
touch $out/modules_to_build
find ${path} -not -type d -name mize.nix | while read p; do
(echo "$p" >> $out/modules_to_build; echo "found module $p")
done
exit 0
'';
};
findModules = path: let
mize_module_no_externals = builtins.getEnv "MIZE_MODULE_NO_EXTERNALS";
module_list_string = builtins.readFile "${module_list_drv path}/modules_to_build";
module_list = pkgs.lib.lists.remove "" (pkgs.lib.strings.splitString "\n" module_list_string);
getExternals = path: map findModules ((pkgs.callPackage ((import path).externals or (args: [])) {}));
in module_list ++ (if mize_module_no_externals != "" then [] else (map getExternals module_list));
modulesFileList = let
from_mize_modules_repo = findModules mize_modules;
mize_module_no_repo = builtins.getEnv "MIZE_MODULE_NO_REPO";
in pkgs.lib.lists.flatten (from_env_var ++ (if mize_module_no_repo != "" then [] else from_mize_modules_repo) ++ (findModules self));
mize_module_path = builtins.getEnv "MIZE_MODULE_PATH";
dirs_from_path = pkgs.lib.lists.remove "" (pkgs.lib.strings.splitString ":" mize_module_path);
dirs_in_nix_store = map (path: builtins.fetchGit {
url = path;
}) dirs_from_path;
from_env_var = map findModules dirs_in_nix_store;
buildMizeForSystem = system: let
hostSystem = getCrossSystem system;
pkgsCross = import nixpkgs { inherit buildSystem hostSystem; localSystem = buildSystem; overlays = [ rust-overlay.overlays.default ]; };
pkgsNative = import nixpkgs { inherit buildSystem; overlays = [ rust-overlay.overlays.default ]; };
craneLib =
if hostSystem.kernel.name == "windows"
then
(crane.mkLib pkgs).overrideToolchain (fenix.packages.${buildSystem}.combine [
#fenix.packages.${buildSystem}.minimal.rustc
#fenix.packages.${buildSystem}.minimal.cargo
fenix.packages.${buildSystem}.stable.toolchain
fenix.packages.${buildSystem}.targets."${hostSystem.cpu.name}-pc-windows-gnu".stable.toolchain
])
else if hostSystem.cpu.name == "wasm32"
then
(crane.mkLib pkgs).overrideToolchain (fenix.packages.${buildSystem}.combine [
fenix.packages.${buildSystem}.targets.wasm32-unknown-unknown.latest.toolchain
fenix.packages.${buildSystem}.latest.toolchain
])
else if hostSystem.name == "x86_64-linux-gnu"
then
builtins.trace "x86_64-linux-gnu hackfix...."
(crane.mkLib pkgs).overrideToolchain (fenix.packages.${buildSystem}.combine [
fenix.packages.${buildSystem}.stable.toolchain
])
else
(crane.mkLib pkgsCross).overrideToolchain (p: fenix.packages.${buildSystem}.stable.toolchain)
;
toolchain_version_drv = pkgs.stdenv.mkDerivation {
name = "toolchain_version_drv";
dontUnpack = true;
configurePhase = "";
buildPhase = ''
mkdir -p $out
touch $out/rustc-version
echo hiiiiiiiiiiiiiiiiiiiiiiii
#echo 'rustc 1.80.1 (3f5fd8dd4 2024-08-06)' > $out/rustc-version
${craneLib.cargo}/bin/rustc --version > $out/rustc-version
exit 0
'';
};
mkSelString = attrs: builtins.toJSON (attrs // {
inherit toolchain_version;
system = hostSystem.name;
mize_version = "0.0.1";
});
toolchain_version = pkgs.lib.strings.removeSuffix "\n" (builtins.readFile "${toolchain_version_drv}/rustc-version");
mkMizeModule = attrs: (if builtins.hasAttr "drvFunc" attrs then attrs.drvFunc else pkgsCross.stdenv.mkDerivation) ( rec {
name = attrs.modName;
inherit (attrs) modName;
selector_string = mkSelString (attrs.select or {} // {
inherit (attrs) modName;
});
hash = builtins.substring 0 32 (builtins.hashString "sha256" selector_string);
}
// (lib.attrsets.removeAttrs attrs ["drvFunc"] )
);
########## build a Rust Module
mkMizeRustModule = attrs: mkMizeModule (
# general stuff
{
drvFunc = craneLib.buildPackage;
MIZE_BUILD_CONFIG = mizeBuildConfigStr;
mizeInstallPhase = attrs.mizeInstallPhase or ''
mkdir -p $out/lib/
cp $build_dir/target/${hostSystem.nameRust}/$debugOrRelease/libmize_module_${attrs.modName}.so $out/lib/
'';
mizeBuildPhase = attrs.mizeBuildPhase or ''
cargo --color always build --target ${hostSystem.nameRust} ${if builtins.hasAttr "cargoExtraArgs" attrs then attrs.cargoExtraArgs else ""} --manifest-path $build_dir/Cargo.toml --lib
'';
selector_string = mkSelString (attrs.select or {} // {
inherit (attrs) modName;
});
devShell = if builtins.hasAttr "devShell" attrs then attrs.devShell else mkMizeRustShell {};
}
# linux specific stuff
// (lib.attrsets.optionalAttrs (hostSystem.kernel.name == "linux") {
# rename the so library, in case the lib.name in your Cargo.toml is not like mize_module_mylib
postInstall = (attrs.postInstall or "") + ''
if [ ! -f "$out/lib/libmize_module_${attrs.modName}" ]; then
echo RENAMING $out/lib/${attrs.modName}.so to $out/lib/libmize_module_${attrs.modName}.so
mv $out/lib/${attrs.modName}.so to $out/lib/libmize_module_${attrs.modName}.so || true
echo RENAMING $out/lib/${attrs.modName}.a to $out/lib/libmize_module_${attrs.modName}.a
mv $out/lib/${attrs.modName}.a to $out/lib/libmize_module_${attrs.modName}.a && true # ignore if there is no .a file || true
echo "$FILE"
fi
'';
})
# linux cross stuff
// (lib.attrsets.optionalAttrs (hostSystem.kernel.name == "linux" && hostSystem.isCross) {
"CARGO_TARGET_${builtins.replaceStrings ["-"] ["_"] (pkgsCross.lib.strings.toUpper hostSystem.nameFull)}_LINKER" = "${pkgsCross.stdenv.cc.targetPrefix}cc";
HOST_CC = "${pkgsCross.stdenv.cc.nativePrefix}cc";
TARGET_CC = "${pkgsCross.stdenv.cc.targetPrefix}cc";
CARGO_BUILD_TARGET = hostSystem.nameFull;
nativeBuildInputs = attrs.nativeBuildInputs or [] ++ [
pkgsCross.stdenv.cc
];
})
# wasm stuff
// (lib.attrsets.optionalAttrs (hostSystem.cpu.name == "wasm32") {
CARGO_BUILD_TARGET = "wasm32-unknown-unknown";
# checks fail on wasm
doCheck = false;
})
# add windows stuff
// (lib.attrsets.optionalAttrs (hostSystem.kernel.name == "windows") {
strictDeps = true;
doCheck = false;
CARGO_BUILD_TARGET = "${hostSystem.cpu.name}-pc-windows-gnu";
# fixes issues related to libring
TARGET_CC = "${pkgs.pkgsCross.mingwW64.stdenv.cc}/bin/${pkgs.pkgsCross.mingwW64.stdenv.cc.targetPrefix}cc";
#fixes issues related to openssl
OPENSSL_DIR = "${pkgs.openssl.dev}";
OPENSSL_LIB_DIR = "${pkgs.openssl.out}/lib";
OPENSSL_INCLUDE_DIR = "${pkgs.openssl.dev}/include/";
depsBuildBuild = [
pkgs.pkgsCross.mingwW64.stdenv.cc
pkgs.pkgsCross.mingwW64.windows.pthreads
];
})
# add the attrs passed to mkMizeRustModule
// attrs
);
buildModule = path: extraArgs: pkgs.callPackage ((import path).module or (attrs: null)) ({
inherit buildMizeForSystem mizeBuildConfig mizeBuildConfigStr;
inherit mkSelString craneLib toolchain_version;
inherit mkMizeModule mkMizeRustModule buildModule findModules hostSystem pkgsCross pkgsNative mkMizeRustShell;
inherit fenix;
mize_version = "0.0.1";
} // extraArgs );
buildLib = path: extraArgs: pkgs.callPackage ((import path).lib or (args: {})) {
inherit mkSelString craneLib toolchain_version;
inherit mkMizeModule mkMizeRustModule buildModule findModules hostSystem pkgsCross pkgsNative mkMizeRustShell;
mize_version = "0.0.1";
};
mizeBuildConfig = {
namespace = "mize.buildtime.ns";
module_url = "c2vi.dev";
selector = builtins.fromJSON (mkSelString {});
};
mizeBuildConfigStr = let
settingsFormat = pkgs.formats.toml { };
in settingsFormat.generate "mize-build-config.toml" {
config = mizeBuildConfig;
};
################# dev Shels ###################
mkMizeRustShell = attrs: mkMizeModuleShell (attrs // {
#_shell_type = "rust";
nativeBuildInputs = attrs.nativeBuildInputs or [] ++ [
pkgs.pkg-config
(fenix.packages."x86_64-linux".combine [
fenix.packages."x86_64-linux".stable.toolchain
fenix.packages."x86_64-linux".targets.wasm32-unknown-unknown.stable.toolchain
])
# the shell script, to mk the dist folder, for a standard rust module
(pkgs.writeShellApplication {
name = "mkdist";
# should we use the folder, that cargo locate-project gives us??? or just cp ./target/debug/
text = ''
mkdir -p ./dist
mkdir -p ./dist/lib
cp ./target/debug/libmize_module*.so ./dist/lib
cp ./target/debug/libmize_module*.a ./dist/lib || true
'';
})
];
buildInputs = [
pkgsCross.openssl
];
});
mkMizeModuleShell = attrs: pkgs.mkShell (attrs // {
MIZE_BUILD_CONFIG = mizeBuildConfigStr;
IS_MIZE_DEV_SHELL = "true";
});
################# output attrset ###################
in rec {
main = modules.mize;
system = hostSystem;
modules = builtins.listToAttrs ( map ( mod: { name = mod.modName; value = mod; } ) modulesList );
# for debugging
modulesLibList = map (mod: buildLib mod {}) modulesFileList;
modulesLib = pkgs.lib.lists.foldr (a: b: b // a) {} modulesLibList;
modulesList = pkgs.lib.lists.remove null (map (mod: (buildModule mod modulesLib)) modulesFileList);
inherit toolchain_version_drv pkgsCross craneLib;
inherit mize_module_path dirs_from_path dirs_in_nix_store from_env_var;
};
mkInstallPhase = mize: ''
# install mize
mkdir -p $out/mize/${mize.system.name}
cp -r ${mize.main}/* $out/mize/${mize.system.name}/
# intall the modules
${mkModulesInstallPhase mize.modulesList}
'';
mkModulesInstallPhase = modules: pkgs.lib.concatStringsSep "\n" (map mkModuleInstallPhase modules);
mkModuleInstallPhase = module: let
hash = module.hash;
in ''
echo got module: ${module.name}
echo out: $out
echo hash: ${hash}
cp --no-preserve=mode,ownership -r ${module}/* $out/mize/dist/${hash}-${module.modName}
${pkgs.gnutar}/bin/tar -czf $out/mize/dist/${hash}-${module.modName}.tar.gz -C ${module} .
echo '${module.selector_string}' > $out/mize/dist/${hash}-${module.modName}/selector
'';
dist = systems: stdenv.mkDerivation {
name = "mize-dist";
dontUnpack = true;
# so that those binaries run on average linux-gnu systems
dontPatchShebangs = true;
buildPhase = ''
'';
installPhase = ''
mkdir -p $out
mkdir -p $out/mize
mkdir -p $out/mize/dist
'' ;# + pkgs.lib.concatStringsSep "\n" (map mkInstallPhase (map buildMizeForSystem systems));
nativeBuildInputs = [
];
};
moduleShells = system: let
mizeFor = buildMizeForSystem system;
in builtins.listToAttrs ( map ( mod: { name = mod.modName; value = (mod.devShell); } ) mizeFor.modulesList );
random = systems: rec {
distInstallPhase = map mkInstallPhase (map buildMizeForSystem systems);
mizeList = map buildMizeForSystem systems;
mize-x86_64-linux = buildMizeForSystem "x86_64-linux";
mize-x86_64-linux-installPhase = mkInstallPhase mize-x86_64-linux;
modulesListDrv = module_list_drv mize_modules;
inherit modulesFileList;
};
}

18
parts/mize/mize Executable file
View File

@@ -0,0 +1,18 @@
#!/bin/sh
#set -e
# this is a Victorinix Wrapper
source $($VIC_BINARY print-wrapper-path)
BINARY=/home/me/work/mize/target/debug/mize
function vic_build_func() {
nix develop ~/work/victorinix --impure -c cargo build --manifest-path ~/work/victorinix/Cargo.toml
}
vic_wrapper_run "$@"

135
parts/mize/mize.nix Normal file
View File

@@ -0,0 +1,135 @@
{
module = { mkMizeRustModule, mkMizeRustShell, hostSystem, pkgsCross, pkgs, lib, craneLib, ... }:
mkMizeRustModule (
# general stuff
{
modName = "mize";
src = ./.;
cargoExtraArgs = "--no-default-features --lib";
}
# linux stuff
// (lib.attrsets.optionalAttrs (hostSystem.kernel.name == "linux") {
strictDeps = true;
cargoExtraArgs = "--bin mize --features os-target";
mizeInstallPhase = ''
mkdir -p $out
cp $build_dir/target/${hostSystem.nameRust}/$debugOrRelease/mize $out/
'';
})
# x86_64-linux stuff
// (lib.attrsets.optionalAttrs (hostSystem.name == "x86_64-linux-gnu") rec {
nativeBuildInputs = [
pkgsCross.buildPackages.pkg-config
];
buildInputs = [
pkgs.openssl
];
devShell = mkMizeRustShell {
inherit buildInputs nativeBuildInputs;
};
# patch the interpreter to run on most linux-gnu distros
# postBuild = "patchelf --set-interpreter /lib64/ld-linux-x86-64.so.2 /build/source/target/release/mize";
})
# aarch64-linux stuff
// (lib.attrsets.optionalAttrs (hostSystem.name == "aarch64-linux-gnu") {
# patch the interpreter to run on most linux-gnu distros
postBuild = "patchelf --set-interpreter /lib64/ld-linux-aarch64.so.2 /build/source/target/${hostSystem.nameFull}/release/mize";
})
#x86_64-windows stuff
// (lib.attrsets.optionalAttrs (hostSystem.name == "x86_64-windows-gnu") {
strictDeps = true;
doCheck = false;
cargoExtraArgs = "--bin mize --features os-target";
CARGO_BUILD_TARGET = "${hostSystem.cpu.name}-pc-windows-gnu";
# fixes issues related to libring
TARGET_CC = "${pkgs.pkgsCross.mingwW64.stdenv.cc}/bin/${pkgs.pkgsCross.mingwW64.stdenv.cc.targetPrefix}cc";
#fixes issues related to openssl
OPENSSL_DIR = "${pkgs.openssl.dev}";
OPENSSL_LIB_DIR = "${pkgs.openssl.out}/lib";
OPENSSL_INCLUDE_DIR = "${pkgs.openssl.dev}/include/";
depsBuildBuild = [
pkgs.pkgsCross.mingwW64.stdenv.cc
pkgs.pkgsCross.mingwW64.windows.pthreads
];
})
#browser stuff
// (lib.attrsets.optionalAttrs (hostSystem.name == "wasm32-none-unknown") rec {
cargoArtifacts = craneLib.buildDepsOnly ({
src = ./.;
doCheck = false; # tests does not work in wasm
CARGO_BUILD_TARGET = "wasm32-unknown-unknown";
cargoExtraArgs = "--features wasm-target --no-default-features";
RUSTFLAGS="-C linker=wasm-ld";
buildInputs = with pkgs; [ cargo-binutils lld ];
});
mizeBuildPhase = ''
cd $build_dir
RUST_LOG=off wasm-pack build --target no-modules --dev --out-dir $out -- --features wasm-target --no-default-features
cat $build_dir/src/platform/wasm/init.js >> $out/mize.js
'';
mizeInstallPhase = "";
doInstallCargoArtifacts = false;
doCheck = false;
buildPhaseCargoCommand = ''
mkdir -p $out/pkg
HOME=$(mktemp -d fake-homeXXXX) wasm-pack build --target no-modules --out-dir $out/pkg --scope=c2vi -- --features wasm-target --no-default-features
'';
postInstall = ''
rm -rf $out/target.tar.zst
cat $src/src/platform/wasm/init.js >> $out/pkg/mize.js
'';
nativeBuildInputs = with pkgs; [ wasm-bindgen-cli binaryen wasm-pack ];
devShell = mkMizeRustShell {
inherit nativeBuildInputs;
};
})
);
}

View File

@@ -0,0 +1,322 @@
use core::result::Result;
use core::result::Result::Ok;
use std::collections::HashMap;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::format;
use std::string::FromUtf8Error;
use std::io;
use colored::Colorize;
use tracing::{trace, debug, info, warn, error};
use crate::proto::MizeMessage;
#[macro_export]
macro_rules! mize_err {
($($arg:tt)*) => { MizeError::new().msg(format!( $($arg)*)) };
}
pub type MizeResult<T> = Result<T, MizeError>;
pub trait MizeResultTrait<T> {
fn critical(self) -> T;
fn as_std(self) -> Result<T, MizeErrorWhichImplementsError>;
}
pub trait IntoMizeResult<T, S> {
fn mize_result(self) -> MizeResult<T>;
fn mize_result_msg(self, msg: S) -> MizeResult<T> where S: std::fmt::Display ;
}
#[derive(Debug, Clone)]
pub struct MizeError {
pub category: Vec<String>,
pub code: u32,
pub messages: Vec<String>,
pub caused_by_msg: Option<CausedByMessage>,
pub code_location: Option<MizeCodeLocation>,
pub backtrace: String,
}
#[derive(Debug, Clone)]
pub struct CausedByMessage {
// peer: &Connection, // TODO: add later
msg: Box<MizeMessage>,
}
#[derive(Debug, Clone)]
pub struct MizeCodeLocation {
file: String,
line: u32,
column: u32,
}
impl Display for MizeCodeLocation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "file: {} line: {} column: {}", self.file, self.line, self.column)
}
}
impl From<&std::panic::Location <'_>> for MizeCodeLocation {
fn from(panic_location: &std::panic::Location) -> MizeCodeLocation {
let file = panic_location.file().to_string();
let line = panic_location.line();
let column = panic_location.column();
MizeCodeLocation {file, line, column}
}
}
impl MizeError {
#[track_caller]
pub fn code(code: u32) -> MizeError {
let caller_location = std::panic::Location::caller();
get_error_by_code(code, caller_location)
}
pub fn set_code(mut self, code: u32) -> MizeError {
self.code = code;
return self;
}
#[track_caller]
pub fn new() -> MizeError {
let caller_location = std::panic::Location::caller();
return MizeError {
category: Vec::new(),
code: 0,
messages: Vec::new(),
caused_by_msg: None,
code_location: Some(caller_location.into()),
backtrace: format!("{}", std::backtrace::Backtrace::capture()),
}
}
pub fn msg<E>(mut self, msg: E) -> MizeError
where E: std::fmt::Display
{
self.messages.push(format!("{}", msg));
return self;
}
pub fn category<T>(mut self, category: T) -> MizeError where T: Into<String> {
self.category.push(category.into());
return self;
}
fn send(self) -> MizeError{
self
}
fn add_to_err_item(self) -> MizeError {
self
}
pub fn log(self) -> MizeError {
error!("MizeError envountered!");
if let Some(ref location) = self.code_location {
error!("[ {} ] {}", "LOCATION".yellow(), location);
};
error!("[ {} ] {}", "BACKTRACE".yellow(), self.backtrace);
for msg in &self.messages {
error!("[ {} ] {}", "MSG".yellow(), msg);
}
self
}
pub fn handle(self) -> MizeError {
return self.send().add_to_err_item().log();
}
pub fn critical(self){
let mut msg_iter = self.messages.iter();
match msg_iter.next() {
None => {
error!("{} MizeError with code: {}", "CRITICAL".red(), self.code);
},
Some(msg) => {
error!("{} MizeError with code: {} - {}", "CRITICAL".red(), self.code, msg);
},
}
for msg in msg_iter {
error!("{} {}", "MSG".red(), msg)
}
if let Some(location) = self.code_location {
error!("{} {}", "LOCATION".red(), location)
}
if let Some(caused_by_msg) = self.caused_by_msg {
error!("{} {:?}", "CAUSED_BY_MESSAGE".red(), caused_by_msg)
}
panic!();
}
pub fn location(mut self, location: MizeCodeLocation) -> MizeError {
self.code_location = Some(location);
self
}
}
/*
pub trait MizeResultExtension<T> {
fn extra_msg<E>(self, msg: E) -> Result<T, MizeError>
where E: std::fmt::Display
;
fn handle(self) -> Result<T, MizeError>;
fn is_critical(self) -> T;
}
impl<T> MizeResultExtension<T> for Result<T, MizeError> {
fn msg<E>(mut self, msg: E) -> Result<T, MizeError>
where E: std::fmt::Display
{
match self {
Err(ref mut e) => {
e.msg.push(format!("{}", msg));
return self;
},
Ok(something) => Ok(something),
}
}
fn handle(self) -> Result<T, MizeError> {
match self {
Err(e) => {
Err(e.send().add_to_err_item().write_to_stderr())
},
Ok(something) => Ok(something)
}
}
fn is_critical(self) -> T {
self.expect("Aborting because of a critical Error")
}
}
*/
/*
impl From<FromUtf8Error> for MizeError {
#[track_caller]
fn from(utf_err: FromUtf8Error) -> MizeError{
let caller_location = std::panic::Location::caller();
let mut err = MizeError::code(109).msg(format!("From std::string::FromUtf8Error: {}", utf_err));
err.code_location = Some(caller_location.into());
return err;
}
}
*/
impl<T: Display> From<T> for MizeError {
#[track_caller]
fn from(value: T) -> Self {
let caller_location = std::panic::Location::caller();
MizeError::new()
.msg(format!("From {}: {}", std::any::type_name_of_val(&value), value))
.location(caller_location.into())
}
}
impl <T> MizeResultTrait<T> for MizeResult<T> {
fn critical(self) -> T {
match self {
Ok(val) => val,
Err(err) => {
err.critical();
unreachable!()
},
}
}
fn as_std(self) -> Result<T, MizeErrorWhichImplementsError> {
match self {
Ok(val) => Ok(val),
Err(err) => {
Err(MizeErrorWhichImplementsError { inner: err })
},
}
}
}
impl<T, E, S> IntoMizeResult<T, S> for Result<T, E> where E: std::fmt::Display {
#[track_caller]
fn mize_result(self) -> MizeResult<T> {
let caller_location = std::panic::Location::caller();
match self {
Ok(val) => MizeResult::Ok(val),
Err(err) => MizeResult::Err(MizeError::new()
.category("misc")
.msg(format!("From {}: {}", std::any::type_name_of_val(&err), err))),
}
}
#[track_caller]
fn mize_result_msg(self, msg: S) -> MizeResult<T> where S: std::fmt::Display {
let caller_location = std::panic::Location::caller();
match self {
Ok(val) => MizeResult::Ok(val),
Err(err) => MizeResult::Err(MizeError::new()
.category("misc")
.msg(format!("{}", msg))
.msg(format!("From {}: {}", std::any::type_name_of_val(&err), err))),
}
}
}
fn get_error_by_code(code: u32, caller_location: &std::panic::Location) -> MizeError {
let err = MizeError {
category: Vec::new(),
code: 0,
messages: Vec::new(),
caused_by_msg: None,
code_location: Some(caller_location.into()),
backtrace: format!("{}", std::backtrace::Backtrace::capture()),
};
match code {
109 => err.set_code(109).category("decoding").msg("conversion from utf8 failed"),
108 => err.set_code(108).category("decoding").msg("serde deserialisation failed"),
_ => err,
}
}
#[derive(Debug, Clone)]
pub struct MizeErrorWhichImplementsError {
inner: MizeError,
}
impl Display for MizeErrorWhichImplementsError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "MizeError envountered!");
if let Some(ref location) = self.inner.code_location {
write!(f, "[ {} ] {}", "LOCATION".yellow(), location);
};
write!(f, "[ {} ] {}", "BACKTRACE".yellow(), self.inner.backtrace);
for msg in &self.inner.messages {
write!(f,"[ {} ] {}", "MSG".yellow(), msg);
}
Ok(())
}
}
impl From<MizeError> for MizeErrorWhichImplementsError {
fn from(value: MizeError) -> Self {
MizeErrorWhichImplementsError { inner: value }
}
}
impl std::error::Error for MizeErrorWhichImplementsError {}

180
parts/mize/src/core/id.rs Normal file
View File

@@ -0,0 +1,180 @@
use core::fmt;
use std::{collections::binary_heap::Iter, fmt::write, hash::Hash};
use interner::{shared::{SharedPool, SharedString, SharedVecString}, Pooled};
use std::collections::hash_map::RandomState;
use std::path::Path;
use crate::{instance::{store::Store, Instance}, mize_err};
use crate::error::{MizeResult, MizeError, MizeResultTrait};
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct MizeId {
pub path: SharedVecString,
pub namespace: Namespace,
}
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct Namespace ( pub SharedString );
impl Namespace {
pub fn as_string(&self) -> SharedString {
self.0.clone()
}
pub fn as_real_string(&self) -> String {
let a = self.as_string();
let b = a.to_string();
return b;
}
}
pub trait IntoMizeId {
fn to_mize_id(self, instance: &Instance) -> MizeResult<MizeId>;
}
impl MizeId {
pub fn store_part(&self) -> &str {
self.path.iter().nth(0)
.expect("an empty MizeId found, that should absolutely not be possible!!!!").as_str()
}
pub fn nth_part(&self, n: usize) -> MizeResult<&str> {
match self.path.iter().nth(n) {
Some(val) => Ok(val.as_str()),
None => Err(mize_err!("id does not have {} parts", n)),
}
}
pub fn path(&self) -> SharedVecString {
self.path.clone()
}
pub fn after_store_part(&self) -> Vec<String> {
let tmp = self.path();
let mut path_iter = tmp.into_iter();
path_iter.next();
let new_path: Vec<String> = path_iter.map(|v| v.to_owned()).collect();
return new_path;
}
pub fn namespace(&self) -> Namespace {
self.namespace.clone()
}
pub fn namespace_str(&self) -> &str {
&self.namespace.0
}
}
impl fmt::Display for MizeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.path.join("/"))
}
}
impl IntoMizeId for &str {
fn to_mize_id(self, instance: &Instance) -> MizeResult<MizeId> {
instance.id_from_string(self.to_owned())
}
}
impl IntoMizeId for String {
fn to_mize_id(self, instance: &Instance) -> MizeResult<MizeId> {
instance.id_from_string(self)
}
}
impl IntoMizeId for &String {
fn to_mize_id(self, instance: &Instance) -> MizeResult<MizeId> {
instance.id_from_string(self.to_owned())
}
}
impl IntoMizeId for Vec<String> {
fn to_mize_id(self, instance: &Instance) -> MizeResult<MizeId> {
instance.id_from_vec_string(self)
}
}
impl IntoMizeId for Vec<&String> {
fn to_mize_id(self, instance: &Instance) -> MizeResult<MizeId> {
let owned = self.into_iter().map(|s| s.to_owned()).collect::<Vec<String>>();
instance.id_from_vec_string(owned)
}
}
impl IntoMizeId for &[&str] {
fn to_mize_id(self, instance: &Instance) -> MizeResult<MizeId> {
let mut vec: Vec<String> = Vec::new();
for i in self {
vec.push((*i).to_owned())
}
instance.id_from_vec_string(vec)
}
}
impl IntoMizeId for Vec<&str> {
fn to_mize_id(self, instance: &Instance) -> MizeResult<MizeId> {
let mut vec: Vec<String> = Vec::new();
for i in self {
vec.push((*i).to_owned())
}
instance.id_from_vec_string(vec)
}
}
impl IntoMizeId for MizeId {
fn to_mize_id(self, instance: &Instance) -> MizeResult<MizeId> {
Ok(self)
}
}
// would only work with a global id_pool
// for now use Instance::new_id()
//impl<T: Into<String>> From<T> for MizeId {
//fn from(value: T) -> Self {
//let string = value.into();
//let path: Vec<String> = string.split("/").map(|v| v.to_owned()).collect();
//return MizeId { MIZE_ID_POOL.get(path) }
//}
//}
#[cfg(test)]
mod test {
use crate::instance::Instance;
use super::*;
#[test]
fn test_id() -> MizeResult<()> {
let instance = Instance::empty()?;
let id_one = instance.new_id("0/config/hi")?;
assert_eq!(instance.get_namespace()?, id_one.namespace());
assert_eq!("0", id_one.store_part());
assert_eq!(vec!["config".to_owned(), "hi".to_owned()], id_one.after_store_part());
let id_two = instance.new_id("test.ns:hello/config/hi")?;
assert_eq!(instance.namespace_from_string("test.ns".to_owned())?, id_two.namespace());
assert_eq!("hello", id_two.store_part());
assert_eq!(vec!["config".to_owned(), "hi".to_owned()], id_one.after_store_part());
let id_three = instance.new_id("0/")?;
assert_eq!(instance.get_namespace()?, id_three.namespace());
assert_eq!("0", id_three.store_part());
assert_eq!(Vec::<String>::new(), id_three.after_store_part());
let id_four = instance.new_id("test.ns:0/")?;
assert_eq!(instance.namespace_from_string("test.ns".to_owned())?, id_four.namespace());
assert_eq!("0", id_four.store_part());
assert_eq!(Vec::<String>::new(), id_four.after_store_part());
let id_fife = instance.new_id("0")?;
assert_eq!(instance.get_namespace()?, id_fife.namespace());
assert_eq!("0", id_fife.store_part());
assert_eq!(Vec::<String>::new(), id_fife.after_store_part());
//TODO: add the parsing form mize:// urls....
Ok(())
}
}

View File

@@ -0,0 +1,59 @@
use flume::{Sender, Receiver};
use tracing::{trace, debug, info, warn, error};
use crate::id::Namespace;
use crate::mize_err;
use crate::proto::MizeMessage;
use crate::error::{MizeError, MizeResult, IntoMizeResult};
use super::Instance;
use crate::item::{get_raw_from_cbor, Item, ItemData};
// only created with Instance::new_connection()
#[derive(Clone, Debug)]
pub struct Connection {
pub tx: Sender<MizeMessage>,
pub id: u64,
pub ns: Option<Namespace>,
}
pub trait ConnListener : Send + Sync {
fn listen(self, instance: Instance) -> MizeResult<()>;
}
impl Connection {
pub fn send(&self, msg: MizeMessage) -> MizeResult<()> {
Ok(self.tx.send(msg)?)
}
}
pub fn value_raw_con_by_id(item: &mut Item) -> MizeResult<ItemData> {
if item.id().nth_part(3)? == "peer" {
return value_raw_from_peer(item);
} else {
return Err(mize_err!("id with path: '{}' is not supported", item.id()));
}
}
pub fn value_raw_from_peer(item: &mut Item) -> MizeResult<ItemData> {
let id = item.id();
let conn_id_str = id.nth_part(2)?;
let conn_id : u64 = conn_id_str.parse()?;
let tmp_path = id.path();
let mut new_id_str = tmp_path.into_iter();
let skipped = new_id_str.skip(4);
let new_id = item.instance.new_id(skipped.collect::<Vec<&String>>())?;
let connection = item.instance.get_connection(conn_id)?;
let msg = MizeMessage::new_get(new_id.clone(), connection.id);
connection.tx.send(msg)?;
let data = item.instance.give_msg_wait(new_id)?;
return Ok(data);
}

View File

@@ -0,0 +1,617 @@
use core::fmt;
use std::collections::HashMap;
use std::fs::create_dir;
use std::{thread, vec};
use flume::{bounded, Receiver, Sender, unbounded};
use std::sync::{Arc, Mutex};
use tracing::{debug, error, info, trace, warn, Instrument};
use uuid::Uuid;
use std::fs::File;
use colored::Colorize;
use std::path::{Path, PathBuf};
use interner::shared::{VecStringPool, StringPool};
use crate::error::{MizeError, MizeResult, IntoMizeResult, MizeResultTrait};
use crate::instance::store::Store;
use crate::instance::updater::{ updater_thread, updater_thread_async };
use crate::id::{IntoMizeId, MizeId, Namespace};
use crate::instance::subscription::Subscription;
use crate::item::{Item, ItemData};
use crate::memstore::MemStore;
use crate::instance::updater::Operation;
use crate::{mize_err, Module};
use crate::proto::MizeMessage;
use self::connection::{ConnListener, Connection};
use self::updater::handle_operation;
#[cfg(feature = "async")]
use tokio::runtime::Runtime;
#[cfg(feature = "async")]
use tokio::runtime::Handle;
use core::future::Future;
use std::thread::JoinHandle;
pub mod connection;
pub mod store;
pub mod subscription;
pub mod updater;
pub mod msg_thread;
pub mod module;
// console_log macro
// that can be copied into other files for debugging purposes
#[cfg(feature = "wasm-target")]
use wasm_bindgen::prelude::*;
#[cfg(feature = "wasm-target")]
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[cfg(feature = "wasm-target")]
macro_rules! console_log {
// Note that this is using the `log` function imported above during
// `bare_bones`
($($t:tt)*) => (unsafe { log(&format_args!($($t)*).to_string())})
}
#[cfg(test)]
mod tests;
static MSG_CHANNEL_SIZE: usize = 200;
static BUILD_TIME_CONFIG: &str = include_str!(std::env!("MIZE_BUILD_CONFIG"));
/// The Instance type is the heart of the mize system
#[derive(Clone)]
pub struct Instance {
// a bit a lot of Mutexes isn-it???
pub store: Arc<Mutex<Box<dyn Store>>>,
connections: Arc<Mutex<Vec<Connection>>>,
next_con_id: Arc<Mutex<u64>>,
subs: Arc<Mutex<HashMap<MizeId, Vec<Subscription>>>>,
pub modules: Arc<Mutex<HashMap<String, Box<dyn Module + Sync + Send >>>>,
pub id_pool: Arc<Mutex<VecStringPool>>,
pub namespace_pool: Arc<Mutex<StringPool>>,
// the namespace the instance operates in
pub namespace: Arc<Mutex<Namespace>>,
// the namespace of the instance itself
// TODO: set to a random uuid
pub self_namespace: Arc<Mutex<Namespace>>,
pub op_tx: Sender<Operation>,
threads: Arc<Mutex<Vec<(u32, String)>>>,
next_thread_id: Arc<Mutex<u32>>,
give_msg_wait: Arc<Mutex<HashMap<MizeId, Vec<Sender<ItemData>>>>>,
create_msg_wait: Arc<Mutex<Option<Sender<MizeId>>>>,
#[cfg(feature = "async")]
pub runtime: Arc<Mutex<Runtime>>,
}
pub struct InstanceRef {
inner: Arc<Mutex<Instance>>,
}
pub struct InstanceAsync {
inner: Arc<Mutex<Instance>>,
}
impl Instance {
pub fn empty() -> MizeResult<Instance> {
let id_pool = Arc::new(Mutex::new(VecStringPool::default()));
let namespace_pool_raw = StringPool::default();
let connections = Arc::new(Mutex::new(Vec::new()));
let subs = Arc::new(Mutex::new(HashMap::new()));
let (op_tx, op_rx) = unbounded();
let give_msg_wait = Arc::new(Mutex::new(HashMap::new()));
let create_msg_wait = Arc::new(Mutex::new(None));
let namespace = Arc::new(Mutex::new(Namespace ( namespace_pool_raw.get("mize.default.namespace") )));
let self_namespace = Arc::new(Mutex::new(Namespace ( namespace_pool_raw.get("mize.default.namespace") )));
let mut instance = Instance {
store: Arc::new(Mutex::new(Box::new(MemStore::new()))),
connections, subs, id_pool,
namespace, self_namespace, op_tx,
namespace_pool: Arc::new(Mutex::new(namespace_pool_raw)),
modules: Arc::new(Mutex::new(HashMap::new())),
threads: Arc::new(Mutex::new(Vec::new())),
next_thread_id: Arc::new(Mutex::new(0)),
next_con_id: Arc::new(Mutex::new(1)),
give_msg_wait, create_msg_wait,
#[cfg(feature = "async")]
runtime: Arc::new(Mutex::new(Runtime::new().mize_result_msg("Could not create async runtime")?)),
};
#[cfg(feature = "os-target")]
{
let instance_clone = instance.clone();
let op_rx_clone = op_rx.clone();
let closure = move || updater_thread(op_rx_clone, &instance_clone);
instance.spawn("updater_thread", closure)?;
let instance_clone_two = instance.clone();
let closure_two = move || updater_thread(op_rx, &instance_clone_two);
instance.spawn("updater_thread", closure_two)?;
}
// set up async update "threads" when using wasm
#[cfg(feature = "wasm-target")]
{
let instance_clone = instance.clone();
let op_rx_clone = op_rx.clone();
wasm_bindgen_futures::spawn_local(updater_thread_async(op_rx_clone, instance_clone));
let instance_clone_two = instance.clone();
wasm_bindgen_futures::spawn_local(updater_thread_async(op_rx, instance_clone_two));
}
// will move the msg stuff into it's own thread
// like this it can deadlock.... if a msg waits on an operation to complete
//let msg_instance_clone = instance.clone();
//let msg_closure = move || msg_thread(op_rx, instance_clone);
//instance.spawn("msg_thread", closure)?;
#[cfg(feature = "wasm-target")]
console_log!("before loading build time config");
// load the config from build time
let config = ItemData::from_toml(BUILD_TIME_CONFIG)?;
instance.set_blocking("0", config);
#[cfg(feature = "wasm-target")]
console_log!("after loading build time config");
return Ok(instance);
}
pub fn new() -> MizeResult<Instance> {
trace!("[ {} ] Instance::new()", "CALL".yellow());
let mut instance = Instance::empty()?;
instance.init();
debug!("instance inited with config: {}", instance.get("0/config")?.as_data_full()?);
return Ok(instance);
}
pub fn init(&mut self) -> MizeResult<()> {
// platform specific init code
crate::platform::any::instance_init(self)?;
// end of platform specific init code
// load the modules, ad specified in the load_modules config
match self.get("0/config/load_modules")?.value_string() {
Ok(modules_to_load) => {
for module in modules_to_load.split(" ") {
#[cfg(feature = "wasm-target")]
console_log!("loading module in Instance::init() ... {}", module);
self.load_module(module)?;
}
},
Err(err) => {
// no load_modules option is set
}
}
debug!("INSTANCE INIT DONE");
Ok(())
}
pub fn with_config(config: ItemData) -> MizeResult<Instance> {
trace!("[ {} ] Instance::with_config()", "CALL".yellow());
trace!("config: {}", config);
let mut instance = Instance::empty()?;
instance.set_blocking("0", config);
instance.init()?;
Ok(instance)
}
pub fn migrate_to_store(&self, new_store: Box<dyn Store>) -> MizeResult<()> {
info!("MIGRATING");
let mut old_store = self.store.lock()?;
let id = self.id_from_string("0".to_owned())?;
let inst_data = old_store.get_value_data_full(id.clone())?;
new_store.set(id, inst_data.to_owned())?;
for id in old_store.id_iter()? {
let data = old_store.get_value_data_full(self.id_from_string(id?)?)?;
let id_of_new_store = new_store.new_id()?;
new_store.set(self.id_from_string(id_of_new_store)?, data.to_owned())?;
};
*old_store = new_store;
Ok(())
}
pub fn new_item(&self) -> MizeResult<Item> {
if self.get_namespace()? != self.get_self_namespace()? {
// need to send create msg and wait for it
let mut connection = self.get_connection_by_ns(self.get_namespace()?)?;
let msg = MizeMessage::new_create(connection.id);
connection.send(msg)?;
let (tx, rx) = bounded::<MizeId>(1);
let mut msg_wait_inner = self.create_msg_wait.lock()?;
*msg_wait_inner = Some(tx);
drop(msg_wait_inner);
let id = rx.recv()?;
println!("new_item namespace: {:?}", id.namespace());
return Ok(Item::new(id, self));
}
let store_inner = self.store.lock()?;
let id = self.id_from_string(store_inner.new_id()?)?;
return Ok(Item::new(id, self));
}
pub fn get<I: IntoMizeId>(&self, id: I) -> MizeResult<Item> {
let id = id.to_mize_id(self)?;
return Ok(Item::new(id, &self));
}
pub fn set<I: IntoMizeId, V: Into<ItemData>>(&self, id: I, value: V) -> MizeResult<()> {
let id = id.to_mize_id(self)?;
self.op_tx.send(Operation::Set(id, value.into(), None));
Ok(())
}
pub fn set_blocking<I: IntoMizeId, V: Into<ItemData>>(&self, id: I, value: V) -> MizeResult<()> {
handle_operation(&mut Operation::Set(id.to_mize_id(self)?, value.into(), None), self)?;
Ok(())
}
pub fn sub<I: IntoMizeId>(&self, id: I, sub: Subscription) -> MizeResult<()> {
let mut subs_inner = self.subs.lock()?;
let id = id.to_mize_id(self)?;
match subs_inner.get_mut(&id) {
Some(vec) => {
vec.push(sub);
},
None => {
subs_inner.insert(id.clone(), vec![sub]);
}
}
// if we are not the owner of this item, send a sub msg to them
if !self.we_are_namespace()? {
let con = self.get_connection_by_ns(id.namespace())?;
let msg = MizeMessage::new_sub(id, con.id);
con.send(msg)?;
}
Ok(())
}
pub fn new_id<T: IntoMizeId>(&self, value: T) -> MizeResult<MizeId> {
value.to_mize_id(self)
}
pub fn id_from_string(&self, string: String) -> MizeResult<MizeId> {
let vec_string: Vec<String> = string.split("/").map(|v| v.to_owned()).filter(|el| el.as_str() != "").collect();
self.id_from_vec_string(vec_string)
}
pub fn id_from_vec_string(&self, mut vec_string: Vec<String>) -> MizeResult<MizeId> {
let id_pool_inner = self.id_pool.lock()?;
let namespace_inner = self.namespace.lock()?;
let first_el = vec_string.first_mut().ok_or(mize_err!("MizeId was empty"))?;
let id = if first_el.contains(":") { // first el is a namespace + store_part
let new_first_el = first_el.clone();
let vec: Vec<&str> = new_first_el.split(":").collect();
let ns_part = vec.iter().nth(0).ok_or(mize_err!("should really not happen"))?;
let store_part = vec.iter().nth(1).ok_or(mize_err!("mizeid was like 'namespace:/hi', why are you doing that"))?;
*first_el = store_part.to_owned().to_owned();
MizeId { path: id_pool_inner.get(vec_string), namespace: self.namespace_from_string(ns_part.to_owned().to_owned())? }
} else {
MizeId { path: id_pool_inner.get(vec_string), namespace: namespace_inner.clone() }
};
trace!("new MizeId made: {:?}", id);
Ok(id)
}
pub fn load_module(&mut self, name: &str) -> MizeResult<()> {
crate::platform::any::load_module(self, name, None)
}
pub fn fetch_module(&mut self, name: &str) -> MizeResult<String> {
// platform specific way to fetch a module
crate::platform::any::fetch_module(self, name)
}
pub fn load_module_at(&mut self, name: &str, path: String) -> MizeResult<()> {
// platform specific init code
crate::platform::any::load_module(self, name, Some(path))
}
pub fn get_module(&mut self, name: &str) -> MizeResult<Box<dyn Module + Send + Sync>> {
let inner = self.modules.lock()?;
let module = inner.get(name).ok_or(mize_err!("Couldn't get_module('{name}')"))?.clone_module();
Ok(module)
}
pub fn namespace_from_string(&self, ns_str: String) -> MizeResult<Namespace> {
let namespace_pool_inner = self.namespace_pool.lock()?;
let namespace = Namespace ( namespace_pool_inner.get(ns_str) );
Ok(namespace)
}
pub fn set_namespace(&self, ns: Namespace) -> MizeResult<()> {
let mut namespace_inner = self.namespace.lock()?;
*namespace_inner = ns;
Ok(())
}
pub fn get_namespace(&self) -> MizeResult<Namespace> {
let mut namespace_inner = self.namespace.lock()?;
return Ok(namespace_inner.clone());
}
pub fn get_self_namespace(&self) -> MizeResult<Namespace> {
let mut self_namespace_inner = self.self_namespace.lock()?;
return Ok(self_namespace_inner.clone());
}
pub fn we_are_namespace(&self) -> MizeResult<bool> {
Ok(self.get_namespace()? == self.get_self_namespace()?)
}
pub fn add_listener<T: ConnListener +'static>(&mut self, listener: T) -> MizeResult<()> {
let mut instance_clone = self.clone();
self.spawn("some_listener", move || listener.listen(instance_clone));
Ok(())
}
pub fn new_connection(&self, tx: Sender<MizeMessage>) -> MizeResult<u64> {
let mut conn_inner = self.connections.lock()?;
let mut next_con_id = self.next_con_id.lock()?;
let old_next_con_id = *next_con_id;
let connection = Connection { id: next_con_id.to_owned(), tx, ns: None};
conn_inner.push(connection);
*next_con_id += 1;
Ok(old_next_con_id)
}
pub fn new_connection_join_namespace(&self, tx: Sender<MizeMessage>) -> MizeResult<u64> {
let conn_id = self.new_connection(tx)?;
let ns_of_peer_str = self.get(format!("inst/con_by_id/{}/peer/0/config/namespace", conn_id))?.value_string()?;
let ns_of_peer = self.namespace_from_string(ns_of_peer_str)?;
self.connection_set_namespace(conn_id, ns_of_peer.clone());
self.set_namespace(ns_of_peer);
Ok(conn_id)
}
pub fn connection_set_namespace(&self, conn_id: u64, namespace: Namespace) -> MizeResult<()> {
let mut connection = self.get_connection(conn_id)?;
connection.ns = Some(namespace);
self.set_connection(conn_id, connection);
Ok(())
}
pub fn got_msg(&self, msg: MizeMessage) -> MizeResult<()> {
Ok(self.op_tx.send(Operation::Msg(msg))?)
}
pub fn report_err(&self, err: MizeError) {
err.log();
}
fn set_connection(&self, conn_id: u64, new_connection: Connection) -> MizeResult<()> {
let mut conn_inner = self.connections.lock()?;
for connection in conn_inner.iter_mut() {
if connection.id == conn_id {
*connection = new_connection;
return Ok(());
}
}
return Err(mize_err!("Connection with id {} not known to instance", conn_id));
Ok(())
}
pub fn get_connection(&self, conn_id: u64) -> MizeResult<Connection> {
let mut conn_inner = self.connections.lock()?;
for connection in conn_inner.iter() {
if connection.id == conn_id {
return Ok(connection.clone());
}
}
return Err(mize_err!("Connection with id {} not known to instance", conn_id));
}
pub fn get_connection_by_ns(&self, ns: Namespace) -> MizeResult<Connection> {
let mut conn_inner = self.connections.lock()?;
for connection in conn_inner.iter() {
if connection.ns == Some(ns.clone()) {
return Ok(connection.clone());
}
}
return Err(mize_err!("Connection with namespace {} not known to instance", ns.as_string()));
}
pub fn spawn(&mut self, name: &str, func: impl FnOnce() -> MizeResult<()> + Send + 'static) -> MizeResult<()> {
let mut threads_inner = self.threads.lock()?;
let mut next_thread_id = self.next_thread_id.lock()?;
threads_inner.push((*next_thread_id, name.to_owned()));
let my_thread_id_no_mutex_guard = *next_thread_id;
let thread_mutex = self.threads.clone();
let name_to_move = name.to_owned();
let to_spawn = move || -> MizeResult<()> {
debug!("spawning thread: {}", name_to_move);
let my_thread_id = my_thread_id_no_mutex_guard;
func()?;
let mut threads_inner = thread_mutex.lock()?;
*threads_inner = threads_inner.clone().into_iter().filter(|el| match el {
(my_thread_id, _) => false,
(_, _) => true,
}).collect();
debug!("thread '{}' stopped", name_to_move);
Ok(())
};
*next_thread_id += 1;
#[cfg(feature = "os-target")]
thread::spawn(move || to_spawn());
#[cfg(feature = "wasm-target")]
{
//console_log!("in instance::spawn with wasm target")
}
//NOT WELL SUPPORTED
//crate::platform::wasm::wasm_spawn(to_spawn)?;
Ok(())
}
pub fn give_msg_wait(&self, id: MizeId) -> MizeResult<ItemData> {
let mut give_msg_wait_inner = self.give_msg_wait.lock()?;
let (tx, rx) = bounded::<ItemData>(1);
let vec = match give_msg_wait_inner.get_mut(&id) {
Some(vec) => vec,
None => {
give_msg_wait_inner.insert(id.clone(), Vec::new());
give_msg_wait_inner.get_mut(&id).unwrap()
}
};
vec.push(tx);
// so that another thread can also give_msg_wait(), while we wait in the recv() of rx
drop(vec);
drop(give_msg_wait_inner);
let data = rx.recv()?;
return Ok(data);
}
#[cfg(feature = "async")]
pub fn spawn_async<F: Future<Output = impl Send + 'static> + Send + 'static>(&mut self, name: &str, func: F) -> MizeResult<()> {
let mut threads_inner = self.threads.lock()?;
let mut next_thread_id = self.next_thread_id.lock()?;
threads_inner.push((*next_thread_id, name.to_owned()));
let runtime_inner = self.runtime.lock().unwrap();
runtime_inner.spawn(func);
*next_thread_id += 1;
Ok(())
}
#[cfg(feature = "async")]
pub fn async_get_handle(&self) -> Handle {
let runtime_inner = self.runtime.lock().unwrap();
let handle = runtime_inner.handle().to_owned();
handle
}
#[cfg(feature = "async")]
pub fn spawn_async_blocking<F: Future<Output = impl Send + Sync + 'static> + Send + Sync + 'static>(&mut self, name: &str, func: F) -> F::Output {
let mut threads_inner = self.threads.lock().expect("mutex lock failed in spawn_async_blocking");
let mut next_thread_id = self.next_thread_id.lock().expect("mutex lock failed in spawn_async_blocking");
threads_inner.push((*next_thread_id, name.to_owned()));
let runtime_inner = self.runtime.lock().unwrap();
let handle = runtime_inner.handle().to_owned();
*next_thread_id += 1;
drop(runtime_inner);
drop(threads_inner);
drop(next_thread_id);
let result = handle.block_on(func);
return result;
}
pub fn wait(&self) {
info!("Instance main thread waiting");
loop {
thread::sleep_ms(10000000)
}
}
pub fn report_error(err: MizeError) {
err.log();
}
}
impl fmt::Debug for Instance {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Mize Instance with subs: {:?}", self.subs,)
}
}

View File

@@ -0,0 +1,69 @@
// https://users.rust-lang.org/t/casting-between-trait-object-types/97220/2
use std::ffi::OsString;
use crate::instance::Instance;
use crate::error::MizeResult;
pub trait Module: {
fn init(&mut self, instance: &Instance) -> MizeResult<()>;
fn exit(&mut self, instance: &Instance) -> MizeResult<()>;
fn clone_module(&self) -> Box<dyn Module + Send + Sync>;
// extra traits, that can be implemented
// return None if not implemented
fn run_cli(&mut self, instance: &Instance, cmd_line: Vec<OsString>) -> Option<MizeResult<()>> {
None
}
}
// console_log macro
// that can be copied into other files for debugging purposes
#[cfg(feature = "wasm-target")]
use wasm_bindgen::prelude::*;
#[cfg(feature = "wasm-target")]
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[cfg(feature = "wasm-target")]
macro_rules! console_log {
// Note that this is using the `log` function imported above during
// `bare_bones`
($($t:tt)*) => (unsafe { log(&format_args!($($t)*).to_string())})
}
pub struct EmptyModule {
}
impl Module for EmptyModule {
fn init(&mut self, instance: &Instance) -> MizeResult<()> {
println!("empty module fn init");
#[cfg(feature = "wasm-target")]
console_log!("empty module fn init");
Ok(())
}
fn exit(&mut self, instance: &Instance) -> MizeResult<()> {
println!("empty module fn exit");
Ok(())
}
fn clone_module(&self) -> Box<dyn Module + Send + Sync> {
Box::new(EmptyModule { })
}
}

View File

@@ -0,0 +1,2 @@

View File

@@ -0,0 +1,73 @@
use std::fmt::Debug;
use std::iter::Map;
use std::option::Iter;
use dyn_clone::DynClone;
use crate::error::MizeResult;
use crate::item::{Item, ItemData};
use crate::id::MizeId;
use crate::memstore::MemStore;
use crate::instance::Instance;
use crate::item::get_raw_from_cbor;
dyn_clone::clone_trait_object!(Store);
pub trait Store: DynClone + Send + Sync + Debug {
// should have the ability to be multi threaded, if the underlying implementation supports
// multithreaded IO operations
// for now all refs to a store hold a Mutex but this mutex is part of where the MizeStore trait
// is implemented, so that there can be a multithreaded implementation
//fn get(self, id: MizeId) -> MizeResult<Item<Self>> where Self: Sized;
fn set(&self, id: MizeId, data: ItemData) -> MizeResult<()>;
// in the future should implement transactions, ....
// funcs to do with links, backlinks
fn get_links(&self, item: Item) -> MizeResult<Vec<MizeId>>;
fn get_backlinks(&self, item: Item) -> MizeResult<Vec<MizeId>>;
fn new_id(&self) -> MizeResult<String>;
fn get_value_raw(&self, id: MizeId) -> MizeResult<Vec<u8>>;
fn get_value_data_full(&self, id: MizeId) -> MizeResult<ItemData>;
fn id_iter(&self) -> MizeResult<IdIter>;
fn next_id(&self, prev_id: &str) -> MizeResult<Option<String>>;
fn first_id(&self) -> MizeResult<String>;
}
pub struct IdIter {
cur_id: String,
store: Box<dyn Store>,
}
impl IdIter {
pub fn new(store: Box<dyn Store>) -> MizeResult<IdIter> {
Ok(IdIter { cur_id: store.first_id()?, store })
}
}
impl Iterator for IdIter {
type Item = MizeResult<String>;
fn next(&mut self) -> Option<Self::Item> {
match self.store.next_id(&self.cur_id) {
Err(err) => {
Some(Err(err))
},
Ok(Some(new_id)) => {
self.cur_id = new_id.clone();
Some(Ok(new_id))
},
Ok(None) => {
None
},
}
}
}

View File

@@ -0,0 +1,66 @@
use core::fmt;
use std::rc::Rc;
use flume::Sender;
use std::sync::Arc;
use tracing::trace;
use crate::id::MizeId;
use crate::instance::connection::Connection;
use crate::item::Item;
use crate::error::MizeResult;
use crate::proto::MizeMessage;
use super::Instance;
#[derive(Clone, Debug)]
pub struct Update {
pub instance: Arc<Instance>,
pub id: MizeId,
}
impl Update {
pub fn new_item(&self) -> MizeResult<Item> {
self.instance.get(self.id.clone())
}
}
pub enum Subscription {
Connection(Connection),
Closure(Box<dyn Fn(Update) -> MizeResult<()> + Send>),
Channel(Sender<Update>),
}
impl Subscription {
pub fn from_conn(conn: Connection) -> Subscription {
Subscription::Connection(conn)
}
pub fn from_closure(closure: Box<dyn Fn(Update) -> MizeResult<()> + Send>) -> Subscription {
Subscription::Closure(closure)
}
pub fn from_sender(tx: Sender<Update>) -> Subscription {
Subscription::Channel(tx)
}
pub fn handle(&mut self, update: Update) -> MizeResult<()> {
trace!("handleing update");
match &self {
Subscription::Connection(conn) => {
let msg = MizeMessage::new_update(update.id.clone(), update.new_item()?.as_data_full()?, conn.id);
conn.send(msg)?;
}
Subscription::Closure(closure) => {
closure(update)?
}
Subscription::Channel(tx) => {
tx.send(update);
}
}
Ok(())
}
}
impl fmt::Debug for Subscription {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "...Subscription...")
}
}

View File

@@ -0,0 +1,230 @@
use tracing_subscriber::registry::Data;
use crate::item::IntoItemData;
use super::*;
#[test]
fn test_sort_keys() -> MizeResult<()> {
let mut data = ItemData::from_toml(r#"
[hi]
test = 4
[config]
zzz = "hiiii"
hi = "hello from config/hi"
[config.test]
inner = "inner"
"#)?;
let data_sorted = ItemData::from_toml(r#"
[config]
hi = "hello from config/hi"
test = { inner = "inner" }
zzz = "hiiii"
[hi]
test = 4
"#)?;
data.sort_keys()?;
assert_eq!(data.to_json()?, data_sorted.to_json()?);
Ok(())
}
#[test]
fn test_get_module_hash() -> MizeResult<()> {
let mut instance = Instance::empty()?;
use crate::platform::os::get_module_hash;
let selector_toml = r#"
mize_version = "0.0.0"
system = "x86_64-unknown-linux-gnu"
toolchain_version = "rustc 1.78.0 (9b00956e5 2024-04-29)"
"#;
instance.set_blocking("0/config/selector", ItemData::from_toml(&selector_toml)?)?;
let (hash, selector) = get_module_hash(&mut instance, "String", ItemData::new())?;
test_println!("hash: {}", hash);
test_println!("selector: {}", selector);
assert_eq!(hash, "e96edf60216688314efc263c1aa8d6ce".to_owned());
Ok(())
}
#[test]
fn test_set_store_path() -> MizeResult<()> {
let instance = Instance::empty()?;
let store_path = "/home/me/.mize".to_owned();
let config = instance.get("0/config")?.as_data_full()?;
test_println!("data at 0/config: {}", config);
instance.set_blocking("0/config/store_path", store_path.clone().into_item_data())?;
let store_path_new = instance.get("0/config/store_path")?.value_string()?;
assert_eq!(store_path_new, store_path);
Ok(())
}
#[test]
fn test_set_sub_path() -> MizeResult<()> {
let instance = Instance::empty()?;
let data = ItemData::from_toml(r#"
[config]
hi = "hello from config/hi"
[config.test]
inner = "inner"
"#)?;
let data_two = ItemData::from_toml(r#"
[config]
hi = "hello from config/hi"
[config.test]
inner = "new inner"
"#)?;
let item = instance.new_item()?;
instance.set_blocking(item.id(), data.clone())?;
assert_eq!(item.as_data_full()?, data);
// try to set a sub path of item
instance.set_blocking(vec![item.id().store_part(), "config", "test", "inner"], "new inner".into_item_data())?;
// the contents of item should then be like data_two
println!("item with id '{}': {}", item.id(), instance.get(vec![item.id().store_part()])?.as_data_full()?);
assert_eq!(instance.get(vec![item.id().store_part(), "config", "test", "inner"])?.as_data_full()?, ItemData::from_string("new inner"));
assert_eq!(item.as_data_full()?, data_two);
Ok(())
}
#[test]
fn test_itemdata_set_path() -> MizeResult<()> {
let mut data = ItemData::from_toml(r#"
[config]
hi = "hello from config/hi"
[config.test]
inner = "inner"
"#)?;
let data_two = ItemData::from_toml(r#"
[config]
hi = "hello from config/hi"
[config.test]
inner = "new inner"
"#)?;
data.set_path(vec!["config", "test", "inner"], ItemData::from_string("new inner"))?;
assert_eq!(data, data_two);
Ok(())
}
#[test]
fn test_itemdata_merge() -> MizeResult<()> {
let mut data = ItemData::from_toml(r#"
[config]
hi = "hello from config/hi"
[config.test]
inner = "inner"
"#)?;
let data_two = ItemData::from_toml(r#"
[config]
hi = "hello from config/hi"
[config.test]
inner = "new inner"
"#)?;
assert_eq!(data.get_path(vec!["config", "test", "inner"])?, ItemData::from_string("inner"));
data.merge(data_two.clone());
assert_eq!(data, data_two);
assert_eq!(data.get_path(vec!["config", "test", "inner"])?, ItemData::from_string("new inner"));
Ok(())
}
#[test]
fn test_item_merge() -> MizeResult<()> {
let instance = Instance::empty()?;
let mut item = instance.new_item()?;
// here the item should contain "empty" data
assert_eq!(item.as_data_full()?, ItemData::new());
let data = ItemData::from_toml(r#"
[config]
hi = "hello from config/hi"
[config.test]
inner = "inner"
"#)?;
let data_two = ItemData::from_toml(r#"
[config]
hi = "hello from config/hi"
[config.test]
inner = "new inner"
"#)?;
item.merge(data.clone())?;
// now it should contain data
assert_eq!(item.as_data_full()?, data);
let mut sub_item = instance.get(vec![item.id().store_part(), "config", "test", "inner"])?;
// the sub item should have the string "inner"
assert_eq!(sub_item.as_data_full()?, ItemData::from_string("inner"));
sub_item.merge(ItemData::from_string("new inner"));
assert_eq!(sub_item.as_data_full()?, ItemData::from_string("new inner"));
// also the item should be updated
assert_eq!(item.as_data_full()?, data_two);
Ok(())
}
/*
#[test]
#[should_panic(expected = "correct panic")]
fn test_cant_set_non_existent_item() -> () {
let instance = Instance::empty().expect("wrong panic");
instance.set_blocking("5", "hello world".into_item_data()).expect("correct panic");
}
*/

View File

@@ -0,0 +1,234 @@
use flume::Receiver;
use tracing::{error, trace, warn};
use std::borrow::BorrowMut;
use std::sync::Arc;
use crate::mize_err;
use crate::{instance::Instance, item::ItemData};
use crate::id::MizeId;
use crate:: error::{MizeResult, MizeError, MizeResultTrait};
use crate::proto::{MessageCmd, MizeMessage};
use super::connection::{self, Connection};
use super::subscription::{Subscription, Update};
#[derive(Debug)]
pub enum Operation {
Set(MizeId, ItemData, Option<Connection>), // bool: is_from_update_msg
Msg(MizeMessage),
}
// console_log macro
// that can be copied into other files for debugging purposes
#[cfg(feature = "wasm-target")]
use wasm_bindgen::prelude::*;
#[cfg(feature = "wasm-target")]
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[cfg(feature = "wasm-target")]
macro_rules! console_log {
// Note that this is using the `log` function imported above during
// `bare_bones`
($($t:tt)*) => (unsafe { log(&format_args!($($t)*).to_string())})
}
#[cfg(not(feature = "wasm-target"))]
macro_rules! console_log {
// Note that this is using the `log` function imported above during
// `bare_bones`
($($t:tt)*) => ()
}
//end of console_log macro
pub async fn updater_thread_async(operation_rx : Receiver<Operation>, instance: Instance) -> () {
let mut count = 0;
console_log!("inside an updater thread");
loop {
let mut operation = match operation_rx.recv_async().await {
Ok(val) => val,
Err(e) => {
instance.report_err(e.into());
continue;
}
};
let op_str = match operation {
Operation::Set(_, _, _) => "SET",
Operation::Msg(_) => "MSG",
};
trace!("OPERATION {} - {}", count, op_str);
console_log!("OPERATION {} - {}", count, op_str);
let result = handle_operation(&mut operation, &instance);
trace!("OPERATION {} DONE", count);
count += 1;
if let Err(err) = result {
error!("OPERATION {} FAILED: {:?}", count, operation);
err.log();
}
}
}
pub fn updater_thread(operation_rx : Receiver<Operation>, instance: &Instance) -> MizeResult<()> {
let mut count = 0;
loop {
let mut operation = operation_rx.recv()?;
let op_str = match operation {
Operation::Set(_, _, _) => "SET",
Operation::Msg(_) => "MSG",
};
trace!("OPERATION {} - {}", count, op_str);
let result = handle_operation(&mut operation, instance);
trace!("OPERATION {} DONE", count);
count += 1;
if let Err(err) = result {
error!("OPERATION FAILED: {:?}", operation);
err.log();
}
}
Ok(())
}
pub fn handle_operation(operation: &mut Operation, instance: &Instance) -> MizeResult<()> {
match operation {
Operation::Set(id, value, maybe_conn) => {
let item_data: ItemData = value.to_owned();
let mut item = instance.get(id.clone())?;
item.merge(item_data)?;
//check subs and handle them
let mut subs_inner = instance.subs.lock()?;
if let Some(vec) = subs_inner.get_mut(&id) {
let update = Update {
instance: Arc::new(instance.to_owned()),
id: id.clone(),
};
for sub in vec.iter_mut() {
// don't handle sub of type connection, in case the update comes from this
// connection
if let Some(conn) = maybe_conn {
if let Subscription::Connection(conn2) = sub {
if conn.id == conn2.id {
continue;
}
}
}
sub.handle(update.clone());
}
}
},
Operation::Msg(msg) => {
handle_msg(msg, instance)?
},
}
Ok(())
}
fn handle_msg(msg: &mut MizeMessage, instance: &Instance) -> MizeResult<()> {
match msg.cmd()? {
MessageCmd::Get => {
let id = msg.id(instance)?;
let mut connection = instance.get_connection(msg.conn_id)?.clone();
let item = instance.get(id.clone())?;
let msg = MizeMessage::new_give(id, item.as_data_full()?, msg.conn_id);
connection.send(msg)?;
},
MessageCmd::GetSub => {
let id = msg.id(instance)?;
let mut connection = instance.get_connection(msg.conn_id)?.clone();
let item = instance.get(id.clone())?;
let msg = MizeMessage::new_give(id.clone(), item.as_data_full()?, msg.conn_id);
connection.send(msg)?;
let sub = Subscription::from_conn(connection.clone());
instance.sub(id, sub)?;
},
MessageCmd::Sub => {
let id = msg.id(instance)?;
let mut connection = instance.get_connection(msg.conn_id)?.clone();
let sub = Subscription::from_conn(connection.clone());
instance.sub(id, sub)?;
},
MessageCmd::Update => {
let data = msg.data()?;
let id = msg.id(instance)?;
let connection = instance.get_connection(msg.conn_id)?;
instance.op_tx.send(Operation::Set(id.clone(), data, Some(connection)));
},
// this should check, if the update is valid
// but for now, we do just always accept it
MessageCmd::UpdateRequest => {
let data = msg.data()?;
let id = msg.id(instance)?;
let connection = instance.get_connection(msg.conn_id)?;
instance.op_tx.send(Operation::Set(id.clone(), data, Some(connection)));
},
MessageCmd::Give => {
let id = msg.id(instance)?;
let data = msg.data()?;
let give_msg_wait_inner = instance.give_msg_wait.lock()?;
if let Some(vec) = give_msg_wait_inner.get(&id) {
for tx in vec {
tx.send(data.clone());
}
} else {
warn!("got give msg for id '{}', that has no one waiting for it", id);
}
},
MessageCmd::Create => {
println!("instance.store: {:?}", instance.clone().store);
let item = instance.new_item()?;
let reply_msg = MizeMessage::new_create_reply(item.id(), msg.conn_id);
let mut connection = instance.get_connection(msg.conn_id)?;
connection.send(reply_msg)?;
}
MessageCmd::CreateReply => {
let create_msg_wait_inner = instance.create_msg_wait.lock()?;
if let Some(sender) = create_msg_wait_inner.as_ref() {
sender.send(msg.id(&mut instance.clone())?)?;
}
return Ok(());
}
_ => {
return Err(mize_err!("got a message, that is not handeled"));
},
}
Ok(())
}

666
parts/mize/src/core/item.rs Normal file
View File

@@ -0,0 +1,666 @@
use tracing::{debug, error, info, instrument, trace, warn, Instrument};
use serde::Deserialize;
use serde::Serialize;
use core::fmt;
use std::fmt::Debug;
use std::{i128, string};
use std::{collections::HashMap, path::PathBuf, io::Cursor, fmt::Display};
use std::fs::File;
use std::rc::Rc;
use colored::Colorize;
use std::io;
use tracing::{span, Level};
use crate::error::{MizeError, MizeResult, IntoMizeResult};
use crate::instance::{connection, Instance};
use crate::instance::store::Store;
use crate::id::MizeId;
use crate::mize_err;
use crate::proto::MizeMessage;
use ciborium::Value as CborValue;
use crate::instance::connection::value_raw_con_by_id;
// a item always has to do with a Instance, which takes care of how it is updated
#[derive(Debug, Clone)]
pub struct Item<'a> {
id: MizeId,
pub instance: &'a Instance
}
// without an Instance it is not an item, but only the "data of an item"
// and this type for now is just an alias to CborValue
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ItemData ( pub CborValue );
impl Item<'_> {
pub fn id(&self) -> MizeId {
self.id.clone()
}
pub fn new(id: MizeId, instance: &Instance) -> Item {
Item { id, instance }
}
pub fn value_raw(&self) -> MizeResult<Vec<u8>> {
let data = self.as_data_full()?;
let raw = get_raw_from_cbor(data.cbor(), vec![])?;
return Ok(raw.to_owned());
}
pub fn value_string(&self) -> MizeResult<String> {
let raw = self.value_raw()?;
String::from_utf8(raw.clone())
.mize_result_msg(format!("failed to convert raw value from item '{}' into a utf-8 string. raw value was: '{:02X?}'", self.id(), raw))
}
pub fn as_data_full(&self) -> MizeResult<ItemData> {
let id = self.id();
// the instance we are
if id.store_part() == "self" {
debug!("getting item '{}' from self", self.id());
match id.nth_part(1)? {
"con_by_id" => { return value_raw_con_by_id(&mut self.clone()); },
"namespace" => {
let namespace_inner = self.instance.namespace.lock()?;
return Ok(ItemData::from_string(namespace_inner.as_real_string()));
},
"self_namespace" => {
let namespace_inner = self.instance.self_namespace.lock()?;
return Ok(ItemData::from_string(namespace_inner.as_real_string()));
},
"config" => {
let rest_path = id.path.into_iter().skip(2).map(|v| v.to_owned()).collect::<Vec<String>>().join("/");
let id_for_store = self.instance.new_id("0/config".to_owned() + "/" + rest_path.as_str())?;
let store = self.instance.store.lock()?;
let data = store.get_value_data_full(id_for_store)?;
return Ok(data);
}
_ => {},
}
return Err(mize_err!("a /self path, but the next element in the path '{:?}' is not valid", id.nth_part(1)));
}
let self_namespace_inner = self.instance.self_namespace.lock()?;
if self.id().namespace() == self_namespace_inner.to_owned() {
debug!("getting item '{}' from store", self.id());
if id.store_part() == "inst" {
match id.nth_part(1)? {
"con_by_id" => { return value_raw_con_by_id(&mut self.clone()); },
"namespace" => {
let namespace_inner = self.instance.namespace.lock()?;
return Ok(ItemData::from_string(namespace_inner.as_real_string()));
},
"self_namespace" => {
let namespace_inner = self.instance.self_namespace.lock()?;
return Ok(ItemData::from_string(namespace_inner.as_real_string()));
},
_ => {},
}
}
let store_inner = self.instance.store.lock()?;
return store_inner.get_value_data_full(self.id());
} else {
let mut connection = self.instance.get_connection_by_ns(self.id().namespace())?;
debug!("getting item '{}' from connection '{}' from store", self.id(), connection.id);
let msg = MizeMessage::new_get(self.id(), connection.id);
connection.send(msg);
let data = self.instance.give_msg_wait(self.id())?;
return Ok(data);
}
}
#[instrument(name = "fn.ItemData::merge")]
pub fn merge<V: Into<ItemData> + Debug >(&mut self, mut value: V) -> MizeResult<()> {
let mut data = self.as_data_full()?;
//let mut data = data_full.get_path(id_path_without_store_part)?;
trace!("item::merge data: {:?}", data);
trace!("item::merge id: {:?}", self.id());
let new_data = value.into();
data.merge(new_data);
trace!("item::merge new_data: {:?}", data);
// handle the case of /self/*
if self.id().store_part() == "self" {
let rest_path = self.id().path.into_iter().skip(1).map(|v| v.to_owned()).collect::<Vec<String>>().join("/");
let id_for_store = self.instance.new_id("0".to_owned() + "/" + rest_path.as_str())?;
let store_inner = self.instance.store.lock()?;
store_inner.set(id_for_store, data)?;
} else if self.instance.we_are_namespace()? {
let store_inner = self.instance.store.lock()?;
store_inner.set(self.id(), data)?;
} else {
let namespace = self.id().namespace();
let mut connection = self.instance.get_connection_by_ns(namespace)?;
let msg = MizeMessage::new_update_request(self.id(), data, connection.id);
connection.send(msg)?;
}
Ok(())
}
}
impl<'a> Display for Item<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
//let item_data = self.as_data_full().map_err(|_| std::fmt::Error)?;
write!(f, "Item with id: {}", self.id());
return Ok(());
}
}
impl ItemData {
pub fn new() -> ItemData {
ItemData(CborValue::Null)
}
pub fn empty() -> ItemData {
ItemData(CborValue::Null)
}
pub fn from_toml(toml_string: &str) -> MizeResult<ItemData> {
let toml_deserializer = toml::Deserializer::new(&toml_string);
let value = CborValue::deserialize(toml_deserializer)
.mize_result_msg(format!("Could not deserialize the toml string: {}", &toml_string))?;
return Ok(value.into_item_data());
}
pub fn value_string(self) -> MizeResult<String> {
match self.cbor() {
CborValue::Text(string) => Ok(string.to_owned()),
_ => Err(mize_err!("this ItemData has no string value")),
}
}
pub fn from_string<S: Into<String>>(into_string: S) -> ItemData {
let string = into_string.into();
let data = CborValue::Text(string);
return ItemData (data);
}
pub fn to_json(self) -> MizeResult<String> {
let mut result: Vec<u8> = Vec::new();
self.0.serialize(&mut serde_json::Serializer::new(&mut result))?;
Ok(String::from_utf8(result)?)
}
pub fn from_json(json_str: String) -> MizeResult<ItemData> {
let mut json_deserializer = serde_json::Deserializer::from_str(json_str.as_str());
let value = CborValue::deserialize(&mut json_deserializer)
.mize_result_msg(format!("could not deserialize the json string: {}", json_str))?;
return Ok(value.into_item_data());
}
pub fn merge(&mut self, other: ItemData) {
item_data_merge(&mut self.0, &other.0);
}
pub fn cbor(&self) -> &CborValue {
&self.0
}
pub fn from_cbor(cbor: CborValue) -> ItemData {
ItemData (cbor)
}
pub fn sort_keys(&mut self) -> MizeResult<()> {
item_data_sort_keys(&mut self.0)
}
pub fn null() -> CborValue {
CborValue::Null
}
pub fn parse<T: Into<String>>(value: T) -> ItemData {
let value_str = value.into();
if value_str == "false" {
return CborValue::Bool(false).into_item_data();
}
if value_str == "false" {
return CborValue::Bool(true).into_item_data();
}
if let Ok(int) = value_str.parse::<i128>() {
return int.into_item_data()
}
return CborValue::Text(value_str).into_item_data();
}
pub fn set_path<P: IntoPath, D: IntoItemData>(&mut self, path: P, value: D) -> MizeResult<()> {
//trace!("[ {} ] ItemData::set_path()", "CALL".yellow());
let path = path.into_path();
let value = value.into_item_data();
item_data_set_path(&mut self.0, path, &value.0)
}
pub fn get_path<P: IntoPath>(&self, path: P) -> MizeResult<ItemData> {
let path = path.into_path();
Ok(item_data_get_path(&self.0, path)?.to_owned().into_item_data())
}
}
impl Default for ItemData {
fn default() -> Self {
ItemData::new()
}
}
pub fn data_from_string(data_string: String) -> MizeResult<ItemData> {
let mut data = ItemData::new();
// in case it's just a string
if !data_string.contains("=") {
let cbor = CborValue::Text(data_string);
return Ok(ItemData::from_cbor(cbor));
}
for option in data_string.split(":") {
//trace!("data_from_string: option: {}", option);
if option == "".to_owned() {
continue;
}
let path = option.split("=").nth(0)
.ok_or(MizeError::new().msg(format!("Failed to parse Option: option '{}' has an empty path (thing before =)", option)))?;
//trace!("data_from_string: path: {}", path);
let value = option.split("=").nth(1)
.ok_or(MizeError::new().msg(format!("Failed to parse Option: option '{}' has an empty value (thing after =)", option)))?;
//trace!("data_from_string: value: {}", value);
let mut path_vec = Vec::new();
path_vec.extend(path.split("."));
//trace!("data_from_string: path_vec: {:?}", path_vec);
data.set_path(path_vec, ItemData::parse(value))?;
}
trace!("data_from_string: data: {}", data);
return Ok(data);
}
pub fn item_data_get_path(data: &CborValue, path: Vec<String>) -> MizeResult<&CborValue> {
let mut path_iter = path.clone().into_iter();
//println!("data: {:?}", data);
//println!("path: {:?}", path);
let path_el = match path_iter.nth(0) {
Some(val) => val,
None => return Ok(data), // our base case
};
let mut sub_data = &CborValue::Null;
match data {
CborValue::Map(ref map) => {
for (key, val) in map {
//println!("key: {:?}", key);
//println!("path_el: {}", path_el);
if let CborValue::Text(key_str) = key {
if key_str == &path_el {
sub_data = val;
}
}
}
},
val => {
return Err(MizeError::new()
.msg(format!("Failed to get path '{:?}' from ItemData, the data at '{}' is not a map", path, path_el))
.msg(format!("{:?} is: {:?}", path_el, val)));
},
};
item_data_get_path(sub_data, path_iter.collect())
}
pub fn item_data_sort_keys(data: &mut CborValue) -> MizeResult<()> {
// for a list, sort each item of the list
if data.is_array() {
for el in data.as_array_mut().unwrap().iter_mut() {
item_data_sort_keys(el)?
}
}
// sort the keys of a map
if data.is_map() {
let map: &mut Vec<(CborValue, CborValue)> = data.as_map_mut().unwrap();
//map.sort_by_key(|el| el.0);
map.sort_by(|a,b| {
match a.0.partial_cmp(&b.0) {
Some(val) => val,
None => {
// just say the two values are equal, if they can't be ordered
std::cmp::Ordering::Equal
}
}
});
// also sort every sub value
for (key, val) in map {
item_data_sort_keys(val)?
}
}
Ok(())
}
pub fn item_data_set_path(data: &mut CborValue, path: Vec<String>, data_to_set: &CborValue) -> MizeResult<()> {
//trace!("[ {} ] item_data_set_path()", "CALL".yellow());
//trace!("[ {} ] data: {}", "ARG".yellow(), data.clone().into_item_data());
//trace!("[ {} ] path: {:?}", "ARG".yellow(), path);
//trace!("[ {} ] data_to_set: {}", "ARG".yellow(), data_to_set.clone().into_item_data());
test_println!("item_data_set_path ###########################");
test_println!("data: {}", ItemData::from_cbor(data.clone()));
test_println!("path: {:?}", path);
test_println!("data_to_set: {}", ItemData::from_cbor(data_to_set.clone()));
test_println!("###########################");
let mut path_iter = path.clone().into_iter();
let path_el = match path_iter.nth(0) {
Some(val) => val,
None => {
//trace!("item_data_set_path: base_case");
// our base case
*data = data_to_set.to_owned();
return Ok(());
},
};
match data {
CborValue::Map(ref mut map) => {
for (key, val) in &mut * map {
if let CborValue::Text(key_str) = key {
if key_str == &path_el {
//trace!("item_data_set_path: setting key {key_str} in map with recursive call");
return item_data_set_path(val, path_iter.collect(), data_to_set);
}
}
}
// if we get here, that means, the key (which is the variable path_el) does not exist
// in the map
// so add it... but also with a recursive call which is needed, in case path_iter
// longer than 1
//trace!("item_data_set_path: adding key {path_el} to map");
let mut inner_value = CborValue::Null;
item_data_set_path(&mut inner_value, path_iter.collect(), data_to_set);
map.push((CborValue::Text(path_el.clone()), inner_value));
return Ok(());
},
CborValue::Null => {
//trace!("item_data_set_path: CborValue::Null case");
let mut inner_value = CborValue::Null;
item_data_set_path(&mut inner_value, path_iter.collect(), data_to_set);
let map_vec = vec![(CborValue::Text(path_el), inner_value)];
*data = CborValue::Map(map_vec);
return Ok(());
},
val => {
return Err(MizeError::new()
.msg(format!("Failed to get path '{:?}' from ItemData, the data at '{}' is not a map", path, path_el))
.msg(format!("{:?} is: {:?}", path_el, val)));
},
};
return Err(MizeError::new().msg("unreachable"));
}
pub fn item_data_merge(merge_into: &mut CborValue, other: &CborValue){
// needs to be recursive
match (merge_into, other) {
// if both are maps
// merge the keys/vals in the maps and call recursively on them
(CborValue::Map(ref mut merge_into_map), CborValue::Map(ref other_map)) => {
// go through other_map and merge those keys/values to merge_into
for (other_key, other_val) in other_map {
// so find that same vale in merge_into, if exists recursively merge, else just add to
// vec
let mut found_value = CborValue::Null;
let mut value_found = false;
let mut new_value_for_merge_into: Vec<(CborValue, CborValue)> = Vec::new(); // here we collect the
// new contents of the
// map, to assign later
// to merge_into_map
for (merge_into_key, merge_into_val) in merge_into_map.clone() {
if merge_into_key == *other_key {
value_found = true;
found_value = merge_into_val.to_owned();
// if we found it, also don't add it to new_value_for_merge_into
} else {
//if not found push to new_value_for_merge_into
new_value_for_merge_into.push((merge_into_key.to_owned(), merge_into_val.to_owned()));
}
}
if value_found {
// we found it so merge and asign
item_data_merge(&mut found_value, other_val);
// to asign push to new_value_for_merge_into, because the old was removed already
new_value_for_merge_into.push((other_key.to_owned(), found_value));
} else {
// if not found, just add the other_key/val to new_value_for_merge_into
new_value_for_merge_into.push((other_key.to_owned(), other_val.to_owned()));
}
// finally, we need to asign the new_value_for_merge_into into merge_into
*merge_into_map = new_value_for_merge_into;
}
}
// if other is Null, don't asign
// this is gonna have repercussions.... because it's not clean behaviour
(merge_into, CborValue::Null) => {
}
// in any other case we just want to set merge_into to other
// also this is the base case
(merge_into, other) => {
*merge_into = other.to_owned();
}
}
}
#[instrument(name = "fn.get_raw_from_cbor")]
pub fn get_raw_from_cbor<'a>(value: &'a CborValue, path: Vec<&String>) -> MizeResult<&'a [u8]> {
trace!("[ {} ] value: {:?}", "ARG".yellow(), value);
trace!("[ {} ] path: {:?}", "ARG".yellow(), path);
let mut path_iter = path.clone().into_iter();
let path_el = match path_iter.nth(0) {
Some(val) => val,
None => {
// our base case
let ret_val = match value {
CborValue::Bytes(vec) => {
trace!("[ {} ] ret value: {:?}", "RET".yellow(), &vec[..]);
return Ok(&vec[..]);
},
CborValue::Text(string) => {
trace!("[ {} ] ret value: {:?}", "RET".yellow(), string.as_bytes());
return Ok(string.as_bytes());
},
other => {
return Err(mize_err!("path is empty and the value: '{:?}' is neither Bytes nor Text", other));
},
};
},
};
match value {
CborValue::Bytes(vec) => {
trace!("[ {} ] ret value: {:?}", "RET".yellow(), &vec[..]);
return Ok(&vec[..]);
},
CborValue::Text(string) => {
trace!("[ {} ] ret value: {:?}", "RET".yellow(), string.as_bytes());
return Ok(string.as_bytes());
},
CborValue::Map(map) => {
let mut inner_val: &CborValue = &CborValue::Null;
let mut inner_val_found = false;
// find the inner_val at path
for (key, val) in map.into_iter() {
if let CborValue::Text(key_text) = key {
if key_text == path_el {
inner_val = val;
inner_val_found = true;
}
}
}
// if there is no value at that path
if inner_val_found == false {
return Err(MizeError::new()
// jesus christ, this is a pfusch...
.msg(format!("get_raw_from_cbor: Path '{}' not Found", path.clone().into_iter().map(|v| v.to_owned()).collect::<Vec<String>>().join("/"))))
}
//path_iter.next();
let inner_path = path_iter.collect();
let ret_value = get_raw_from_cbor(inner_val, inner_path);
trace!("[ {} ] ret value: {:?}", "RET".yellow(), ret_value);
return ret_value;
},
_ => Err(MizeError::new()
.msg(format!("get_raw_from_cbor: value is not a map, text or bytes ... value: {:?}", value))),
}
}
trait IntoPath {
fn into_path(self) -> Vec<String>;
}
pub trait IntoItemData {
fn into_item_data(self) -> ItemData;
}
impl IntoPath for &str {
fn into_path(self) -> Vec<String> {
self.to_owned().split("/").map(|v| v.to_owned()).collect()
}
}
impl IntoPath for &[&str] {
fn into_path(self) -> Vec<String> {
self.into_iter().map(|v| (*v).to_owned()).collect()
}
}
impl IntoPath for Vec<String> {
fn into_path(self) -> Vec<String> {
self
}
}
impl IntoPath for Vec<&str> {
fn into_path(self) -> Vec<String> {
self.into_iter().map(|v| v.to_owned()).collect()
}
}
impl IntoItemData for &str {
fn into_item_data(self) -> ItemData {
ItemData::parse(self)
}
}
impl IntoItemData for String {
fn into_item_data(self) -> ItemData {
ItemData(self.into())
}
}
impl IntoItemData for &String {
fn into_item_data(self) -> ItemData {
ItemData(self.to_owned().into())
}
}
impl IntoItemData for CborValue {
fn into_item_data(self) -> ItemData {
ItemData ( self )
}
}
impl IntoItemData for ItemData {
fn into_item_data(self) -> ItemData {
self
}
}
impl IntoItemData for i128 {
fn into_item_data(self) -> ItemData {
ItemData(self.into())
}
}
impl fmt::Display for ItemData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ItemData: ");
let display_writer = DisplayWriter (f);
self.0.serialize(&mut serde_json::Serializer::pretty(display_writer))
.map_err(|serde_err| std::fmt::Error)
}
}
// thanks to: https://stackoverflow.com/a/61768916
struct DisplayWriter<'a, 'b>(&'a mut fmt::Formatter<'b>);
impl<'a, 'b> io::Write for DisplayWriter<'a, 'b> {
fn write(&mut self, bytes: &[u8]) -> std::result::Result<usize, std::io::Error> {
self.0.write_str(&String::from_utf8_lossy(bytes))
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))?;
Ok(bytes.len())
}
fn flush(&mut self) -> std::result::Result<(), std::io::Error> { todo!() }
}
impl PartialEq for ItemData {
fn eq(&self, other: &Self) -> bool {
let mut buf: Vec<u8> = Vec::new();
let mut buf_other: Vec<u8> = Vec::new();
ciborium::into_writer(&self.0, &mut buf);
ciborium::into_writer(&other.0, &mut buf_other);
buf == buf_other
}
}
impl Eq for ItemData {}

View File

@@ -0,0 +1,3 @@
#![ allow( warnings ) ]

View File

@@ -0,0 +1,148 @@
use std::collections::binary_heap::Iter;
use std::collections::HashMap;
use std::iter::Map;
use std::sync::{Arc, Mutex};
use ciborium::Value as CborValue;
use tracing::{instrument, trace};
use colored::Colorize;
use crate::id::MizeId;
use crate::instance::store::{Store, IdIter};
use crate::error::{MizeError, MizeResult, IntoMizeResult};
use crate::item::{Item, ItemData};
use crate::instance::Instance;
use crate::item::get_raw_from_cbor;
use crate::mize_err;
#[derive(Clone, Debug)]
pub struct MemStore {
inner: Arc<Mutex<MemStoreInner>>,
}
pub type MemStoreId = u64;
#[derive(Debug)]
struct MemStoreInner {
map: HashMap<u64, ItemData>,
next_id: u64,
}
impl Store for MemStore {
fn set(&self, id: MizeId, data: ItemData) -> MizeResult<()> {
let mut inner = self.inner.lock()?;
if let Some(old_data) = inner.map.get(&id_to_u64(id.clone())?) {
//if there is already data there, set at the correct path
let mut old_data = old_data.to_owned();
let path = id.after_store_part();
old_data.set_path(path, data)?;
inner.map.insert(id_to_u64(id)?, old_data);
} else {
// if no data exists for that store_part, just insert
inner.map.insert(id_to_u64(id)?, data);
}
return Ok(())
}
fn get_links(&self, item: Item) -> MizeResult<Vec<MizeId>> {
let inner = self.inner.lock()?;
Ok(Vec::new())
}
fn get_backlinks(&self, item: Item) -> MizeResult<Vec<MizeId>> {
let inner = self.inner.lock()?;
Ok(Vec::new())
}
fn new_id(&self) -> MizeResult<String> {
let mut inner = self.inner.lock()?;
let id_string = format!("{}", inner.next_id);
inner.next_id += 1;
return Ok(id_string);
}
#[instrument(name="fn.MemStore::get_value_raw" skip(self))]
fn get_value_raw(&self, id: MizeId) -> MizeResult<Vec<u8>> {
trace!("[ {} ] id: {:?}", "ARG".yellow(), id);
let inner = self.inner.lock()?;
let cbor_val = inner.map.get(&id_to_u64(id.clone())?)
.ok_or(MizeError::new()
.msg(format!("Item with store_part: {}, id: {} does not exist in MemStore", id_to_u64(id.clone())?, id)))?;
let path = id.path();
let mut id_iter = path.iter();
id_iter.next();
let id_without_first = id_iter.collect();
Ok(get_raw_from_cbor(&cbor_val.0, id_without_first)?.to_owned())
}
fn get_value_data_full(&self, id: MizeId) -> MizeResult<ItemData> {
let inner = self.inner.lock()?;
let data = match inner.map.get(&id_to_u64(id.clone())?) {
Some(val) => val.to_owned(),
None => ItemData::new(),
};
let new_path = id.after_store_part();
let ret_data = data.get_path(new_path)?;
return Ok(ret_data);
}
fn id_iter(&self) -> MizeResult<IdIter> {
IdIter::new(Box::new(self.to_owned()))
}
fn next_id(&self, prev_id_str: &str) -> MizeResult<Option<String>> {
let inner = self.inner.lock()?;
let mut id = str_to_u64(prev_id_str)?;
loop {
id += 1;
if id > inner.next_id {
return Ok(None);
}
if inner.map.contains_key(&id) {
println!("returning: {}", id);
return Ok(Some(format!("{}", id)));
} else {
// try id +1
continue;
}
}
}
fn first_id(&self) -> MizeResult<String> {
Ok("0".to_owned())
}
}
impl MemStore {
pub fn new() -> MemStore {
let inner = MemStoreInner { map: HashMap::new(), next_id: 0 };
return MemStore { inner: Arc::new(Mutex::new(inner)) };
}
}
fn id_to_u64(id: MizeId) -> MizeResult<u64> {
str_to_u64(id.store_part())
}
fn str_to_u64(id_str: &str) -> MizeResult<u64> {
id_str.parse()
.mize_result_msg(format!("Could not parse the store_part of mizeid {} into a u64 for the MemStore", id_str))
}

View File

@@ -0,0 +1,346 @@
use core::fmt;
use std::io;
use serde::Serialize;
use ciborium::{value::Integer, Value as CborValue};
use crate::{error::{IntoMizeResult, MizeError, MizeResult}, id::MizeId, instance::{self, connection::Connection, Instance}, item::{IntoItemData, ItemData}};
#[derive(Clone)]
pub struct MizeMessage {
value: CborValue,
pub conn_id: u64,
}
// msg items
static MSG_CMD: u16 = 1;
static MSG_ID: u16 = 2;
static MSG_DATA: u16 = 3;
// cmds
static CMD_GET: u16 = 1;
static CMD_UPDATE: u16 = 2;
static CMD_GIVE: u16 = 3;
static CMD_CREATE: u16 = 4;
static CMD_CREATE_REPLY: u16 = 5;
static CMD_UPDATE_REQUEST: u16 = 6;
static CMD_GET_SUB: u16 = 7;
static CMD_SUB: u16 = 8;
#[derive(Debug)]
pub enum MessageCmd {
Get,
Update,
Give,
Create,
CreateReply,
UpdateRequest,
GetSub,
Sub,
}
impl MizeMessage {
pub fn new(value: CborValue, conn_id: u64) -> MizeMessage {
MizeMessage { value, conn_id }
}
pub fn new_get(id: MizeId, conn_id: u64) -> MizeMessage {
let id_path = id.path().into_iter().map(|string| CborValue::Text(string.to_owned())).collect();
let cmd = (CborValue::Integer(MSG_CMD.into()), CborValue::Integer(CMD_GET.into()));
let id = (CborValue::Integer(MSG_ID.into()), CborValue::Array(id_path));
let value = CborValue::Map(vec![cmd, id]);
MizeMessage::new(value, conn_id)
}
pub fn new_get_sub(id: MizeId, conn_id: u64) -> MizeMessage {
let id_path = id.path().into_iter().map(|string| CborValue::Text(string.to_owned())).collect();
let cmd = (CborValue::Integer(MSG_CMD.into()), CborValue::Integer(CMD_GET_SUB.into()));
let id = (CborValue::Integer(MSG_ID.into()), CborValue::Array(id_path));
let value = CborValue::Map(vec![cmd, id]);
MizeMessage::new(value, conn_id)
}
pub fn new_sub(id: MizeId, conn_id: u64) -> MizeMessage {
let id_path = id.path().into_iter().map(|string| CborValue::Text(string.to_owned())).collect();
let cmd = (CborValue::Integer(MSG_CMD.into()), CborValue::Integer(CMD_SUB.into()));
let id = (CborValue::Integer(MSG_ID.into()), CborValue::Array(id_path));
let value = CborValue::Map(vec![cmd, id]);
MizeMessage::new(value, conn_id)
}
pub fn new_create(conn_id: u64) -> MizeMessage {
let cmd = (CborValue::Integer(MSG_CMD.into()), CborValue::Integer(CMD_CREATE.into()));
let value = CborValue::Map(vec![cmd]);
MizeMessage::new(value, conn_id)
}
pub fn new_create_reply(id: MizeId, conn_id: u64) -> MizeMessage {
let id_path = id.path().into_iter().map(|string| CborValue::Text(string.to_owned())).collect();
let cmd = (CborValue::Integer(MSG_CMD.into()), CborValue::Integer(CMD_CREATE_REPLY.into()));
let id = (CborValue::Integer(MSG_ID.into()), CborValue::Array(id_path));
let value = CborValue::Map(vec![cmd, id]);
MizeMessage::new(value, conn_id)
}
pub fn new_give(id: MizeId, data: ItemData, conn_id: u64) -> MizeMessage {
let id_path = id.path().into_iter().map(|string| CborValue::Text(string.to_owned())).collect();
let cmd = (CborValue::Integer(MSG_CMD.into()), CborValue::Integer(CMD_GIVE.into()));
let id = (CborValue::Integer(MSG_ID.into()), CborValue::Array(id_path));
let data = (CborValue::Integer(MSG_DATA.into()), data.cbor().to_owned());
let value = CborValue::Map(vec![cmd, id, data]);
MizeMessage::new(value, conn_id)
}
pub fn new_update_request(id: MizeId, data: ItemData, conn_id: u64) -> MizeMessage {
let id_path = id.path().into_iter().map(|string| CborValue::Text(string.to_owned())).collect();
let cmd = (CborValue::Integer(MSG_CMD.into()), CborValue::Integer(CMD_UPDATE_REQUEST.into()));
let id = (CborValue::Integer(MSG_ID.into()), CborValue::Array(id_path));
let data = (CborValue::Integer(MSG_DATA.into()), data.cbor().to_owned());
let value = CborValue::Map(vec![cmd, id, data]);
MizeMessage::new(value, conn_id)
}
pub fn new_update(id: MizeId, data: ItemData, conn_id: u64) -> MizeMessage {
let id_path = id.path().into_iter().map(|string| CborValue::Text(string.to_owned())).collect();
let cmd = (CborValue::Integer(MSG_CMD.into()), CborValue::Integer(CMD_UPDATE.into()));
let id = (CborValue::Integer(MSG_ID.into()), CborValue::Array(id_path));
let data = (CborValue::Integer(MSG_DATA.into()), data.cbor().to_owned());
let value = CborValue::Map(vec![cmd, id, data]);
MizeMessage::new(value, conn_id)
}
pub fn value(self) -> CborValue {
self.value
}
pub fn cmd(&self) -> MizeResult<MessageCmd> {
// return err, if msg is not a map
let msg_as_map = match &self.value {
CborValue::Map(val) => val,
_ => {
return Err(MizeError::new().msg("Message was not a map"));
},
};
//check if in this map there is a c, otherwise return err
let val_of_c = {
let mut tmp_val = 0;
for (key, val) in msg_as_map {
if let CborValue::Integer(key_int) = key {
// number one indicates the cmd field
let one: Integer = 1.into();
if key_int == &one {
if let CborValue::Integer(val_int) = val {
tmp_val = val_int.to_owned().into();
}
}
}
}
if tmp_val == 0 {
return Err(MizeError::new().msg("error getting the cmd value form a msg"));
}
tmp_val
};
// match on value of c
let cmd = match val_of_c {
1 => MessageCmd::Get,
2 => MessageCmd::Update,
3 => MessageCmd::Give,
4 => MessageCmd::Create,
5 => MessageCmd::CreateReply,
6 => MessageCmd::UpdateRequest,
7 => MessageCmd::GetSub,
8 => MessageCmd::Sub,
_ => {
return Err(MizeError::new().msg("error cmd of msg was not a valid command"));
},
};
return Ok(cmd);
}
pub fn id_str(&mut self) -> MizeResult<Vec<String>> {
// return err, if msg is not a map
let msg_as_map = match &self.value {
CborValue::Map(val) => val,
_ => {
return Err(MizeError::new().msg("Message was not a map"));
},
};
let vec_value = {
let mut data = &CborValue::Null;
for (key, val) in msg_as_map {
if let CborValue::Integer(key_int) = key {
// number 2 indicates the id field
let two: Integer = 2.into();
if key_int == &two {
data = val;
}
}
}
if data == &CborValue::Null {
return Err(MizeError::new().msg("error getting the data value form a msg"));
}
match data {
CborValue::Array(inner) => inner,
_ => {
return Err(MizeError::new().msg("Id is not a vector"));
},
}
};
let mut vec_string: Vec<String> = Vec::new();
for val in vec_value {
match val {
CborValue::Text(text) => {
vec_string.push(text.to_owned())
},
_ => {
return Err(MizeError::new().msg("one in the vec of the id in a msg is not of type Text"));
},
}
}
Ok(vec_string)
}
pub fn id(&mut self, instance: &Instance) -> MizeResult<MizeId> {
let id_str = self.id_str()?;
return instance.new_id(id_str);
}
pub fn data(&mut self) -> MizeResult<ItemData> {
// return err, if msg is not a map
let msg_as_map = match &self.value {
CborValue::Map(val) => val,
_ => {
return Err(MizeError::new().msg("Message was not a map"));
},
};
let data = {
let mut tmp_val = &CborValue::Null;
let mut found = false;
for (key, val) in msg_as_map {
if let CborValue::Integer(key_int) = key {
// number three indicates the data field
let three: Integer = 3.into();
if key_int == &three {
tmp_val = val;
found = true;
}
}
}
if ! found {
return Err(MizeError::new().msg("error getting the data value form a msg"));
}
tmp_val
};
return Ok(data.to_owned().into_item_data());
}
}
// thanks to: https://stackoverflow.com/a/61768916
struct DisplayWriter<'a, 'b>(&'a mut fmt::Formatter<'b>);
impl<'a, 'b> io::Write for DisplayWriter<'a, 'b> {
fn write(&mut self, bytes: &[u8]) -> std::result::Result<usize, std::io::Error> {
self.0.write_str(&String::from_utf8_lossy(bytes))
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))?;
Ok(bytes.len())
}
fn flush(&mut self) -> std::result::Result<(), std::io::Error> { todo!() }
}
impl fmt::Display for MizeMessage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut err = false;
match self.to_owned().cmd() {
Ok(cmd) => {
writeln!(f, "MizeMessage with cmd: {:?}", cmd);
},
Err(e) => {
writeln!(f, "MizeMessage with unparsable cmd");
err = true;
}
}
match self.to_owned().id_str() {
Ok(id) => {
let id_str = id.join("/");
writeln!(f, "\tid: {:?}", id_str);
},
Err(e) => {
writeln!(f, "\t msg has no id");
//err = true;
}
}
match self.to_owned().data() {
Ok(data) => {
let value = data.cbor();
write!(f, "\tdata: ");
let display_writer = DisplayWriter (f);
if let Err(e) = value.serialize(&mut serde_json::Serializer::pretty(display_writer))
.map_err(|serde_err| std::fmt::Error) {
writeln!(f, "serialize err: {:?}", e);
err = true;
}
},
Err(e) => {
writeln!(f, "\t msg has no data");
//err = true;
},
}
if err {
write!(f, "Full msg: ");
let display_writer = DisplayWriter (f);
self.value.serialize(&mut serde_json::Serializer::pretty(display_writer))
.map_err(|serde_err| std::fmt::Error).mize_result_msg("serialize error")
.inspect_err(|e| { e.clone().log(); });
}
Ok(())
}
}
impl fmt::Debug for MizeMessage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "\nMizeMessage: ");
let display_writer = DisplayWriter (f);
let value = self.to_owned().value();
value.serialize(&mut serde_json::Serializer::pretty(display_writer))
.map_err(|serde_err| std::fmt::Error)
}
}

View File

103
parts/mize/src/lib.rs Normal file
View File

@@ -0,0 +1,103 @@
#![ allow( warnings ) ]
static PROTO_VERSION: u8 = 1;
#[macro_export]
macro_rules! test_println {
($($arg:tt)*) => {
#[cfg(test)]
print!("[[ {} ]] ", <&str as colored::Colorize>::blue("TEST"));
#[cfg(test)]
print!($($arg)*);
#[cfg(test)]
print!("\n");
};
}
#[macro_export]
macro_rules! test_print {
($($arg:tt)*) => {
#[cfg(test)]
print!("[[ {} ]] ", <&str as colored::Colorize>::blue("TEST"));
#[cfg(test)]
print!($($arg)*);
};
}
// the core part has the code that can run on any platform
mod core {
pub mod memstore;
pub mod instance;
pub mod item;
pub mod id;
pub mod proto;
pub mod error;
pub mod types;
}
pub use core::*;
pub use core::instance::module::Module;
pub use core::instance::Instance;
pub use core::error::MizeError;
pub use core::error::MizeResult;
use std::path::PathBuf;
// platform specific stuff
pub mod platform {
#[cfg(feature = "wasm-target")]
pub mod wasm;
#[cfg(feature = "os-target")]
pub mod os;
pub mod any {
//////////// instance_init
#[cfg(feature = "wasm-target")]
pub use super::wasm::wasm_instance_init as instance_init;
#[cfg(feature = "os-target")]
pub use super::os::os_instance_init as instance_init;
#[cfg(not(any(feature = "os-target", feature = "wasm-target")))]
pub use super::super::instance_init;
//////////// load_module
#[cfg(feature = "os-target")]
pub use super::os::load_module;
#[cfg(feature = "wasm-target")]
pub use super::wasm::load_module;
#[cfg(not(any(feature = "os-target", feature = "wasm-target")))]
pub use super::super::load_module;
//////////// fetch_module
#[cfg(feature = "os-target")]
pub use super::os::fetch_module;
#[cfg(feature = "wasm-target")]
pub use super::super::fetch_module;
#[cfg(not(any(feature = "os-target", feature = "wasm-target")))]
pub use super::super::fetch_module;
}
}
pub fn instance_init(instance: &mut core::instance::Instance) {}
pub fn load_module(instance: &mut core::instance::Instance, name: &str, path: Option<String>) -> MizeResult<()> { Ok(()) }
pub fn fetch_module(instance: &mut core::instance::Instance, name: &str) -> MizeResult<String> { Ok("oh noooooooooooooo, something went really really wrong, if this ends up in the executable....".to_owned()) }

View File

@@ -0,0 +1,174 @@
use core::result::Result::Err;
use core::unimplemented;
use std::ffi::OsString;
use std::path::PathBuf;
use std::str::FromStr;
use clap::ArgMatches;
use home::home_dir;
use flume::bounded;
use mize::mize_err;
use std::io::Read;
use tracing::debug;
use mize::error::{IntoMizeResult, MizeError, MizeResult, MizeResultTrait};
use mize::instance::Instance;
use mize::platform::os::config_from_cli_args;
use mize::instance::subscription::Update;
use mize::platform::os::fsstore::FileStore;
use mize::instance::subscription::Subscription;
use mize::item::{IntoItemData, ItemData};
pub fn get(sub_matches: &ArgMatches) -> MizeResult<()> {
let instance = Instance::with_config(config_from_cli_args(sub_matches)?)?;
let id = sub_matches.get_one::<String>("id")
.ok_or(MizeError::new().msg("No id Argument specified"))?;
let item = instance.get(id)?;
let recurse_count = sub_matches.get_count("recurse");
println!("{}", item.as_data_full()?);
return Ok(());
if recurse_count > 0 {
println!("{}", item.id());
println!("{}", item.as_data_full()?);
} else {
println!("{}", item);
};
return Ok(());
}
pub fn call(sub_matches: &ArgMatches) -> MizeResult<()> {
Ok(())
}
pub fn create(sub_matches: &ArgMatches) -> MizeResult<()> {
let mut instance = Instance::with_config(config_from_cli_args(sub_matches)?)?;
let item = instance.new_item()?;
println!("id: {}", item.id());
println!("with namespace: {}", item.id().namespace().as_real_string());
return Ok(());
}
pub fn is_running(sub_matches: &ArgMatches) -> MizeResult<()> {
println!("not implemented");
Err(mize_err!("not implemented"))
}
pub fn mount(sub_matches: &ArgMatches) -> MizeResult<()> {
Ok(())
}
pub fn run(sub_matches: &ArgMatches) -> MizeResult<()> {
let instance = Instance::with_config(config_from_cli_args(sub_matches)?)?;
instance.wait();
return Ok(());
}
pub fn set(sub_matches: &ArgMatches) -> MizeResult<()> {
let instance = Instance::with_config(config_from_cli_args(sub_matches)?)?;
let id = sub_matches.get_one::<String>("id")
.ok_or(MizeError::new().msg("No id Argument specified"))?;
let value = sub_matches.get_one::<String>("value")
.ok_or(MizeError::new().msg("No value Argument specified"))?;
instance.set_blocking(id, value.into_item_data())?;
Ok(())
}
pub fn show(sub_matches: &ArgMatches) -> MizeResult<()> {
let instance = Instance::with_config(config_from_cli_args(sub_matches)?)?;
let id = sub_matches.get_one::<String>("id")
.ok_or(MizeError::new().msg("No id Argument specified"))?;
let item = instance.get(id)?;
let (tx, rx) = bounded::<Update>(4);
let sub = Subscription::from_sender(tx);
instance.sub(id, sub)?;
println!("item: {}", item.as_data_full()?);
for update in rx {
println!("####### GOT UPDATE #######");
println!("item: {}", update.new_item()?.as_data_full()?);
}
Ok(())
}
pub fn stop(sub_matches: &ArgMatches) -> MizeResult<()> {
Ok(())
}
pub fn gui(sub_matches: &ArgMatches) -> MizeResult<()> {
let mut instance = Instance::with_config(config_from_cli_args(sub_matches)?)?;
instance.load_module("mme")?;
instance.wait();
Ok(())
}
pub fn format_cbor(sub_matches: &ArgMatches) -> MizeResult<()> {
let mut input: Vec<u8> = Vec::new();
let stdin = std::io::stdin();
let mut handle = stdin.lock();
//handle.read_to_end(&mut input);
let cbor_data = ciborium::from_reader(handle)?;
let item_data = ItemData::from_cbor(cbor_data);
println!("{}", item_data);
Ok(())
}
pub fn run_from_module(cmd: &str, sub_matches: &ArgMatches) -> MizeResult<()> {
let default = OsString::default();
let mut sub_cmd_line: Vec<OsString> = sub_matches.get_many::<OsString>("").unwrap().map(|e| e.to_owned()).collect();
sub_cmd_line.insert(0, OsString::from_str(cmd)?);
let mut instance = Instance::with_config(config_from_cli_args(sub_matches)?)?;
debug!("loading module '{}'", cmd);
instance.load_module(cmd)?;
instance.get_module(cmd)?.run_cli(&instance, sub_cmd_line)
.ok_or(mize_err!("The Module '{cmd}' does not implement the run_cli() functionality"))??;
Ok(())
}

View File

@@ -0,0 +1,180 @@
use std::fs;
use std::path::{Path, PathBuf};
use std::fs::OpenOptions;
use sysinfo::{System, Pid, ProcessRefreshKind, RefreshKind};
use std::io::Write;
use ciborium::Value as CborValue;
use tracing::debug;
use crate::instance::store::IdIter;
use crate::item::IntoItemData;
use crate::memstore::MemStore;
use crate::{core::instance::store::Store, mize_err};
use crate::error::{IntoMizeResult, MizeResult, MizeError};
use crate::core::item::{ItemData, Item};
use crate::core::id::MizeId;
use crate::item::get_raw_from_cbor;
use crate::instance::{self, Instance};
use tokio::net::UnixStream;
#[derive(Clone, Debug)]
pub struct FileStore {
path: PathBuf,
}
impl FileStore {
pub fn new(path_str: &str) -> MizeResult<FileStore> {
let path = Path::new(path_str);
// create that path if it does not exist
fs::create_dir_all(Path::new(&path).join("store"))?;
// check for valid pid file
/*
if let Some(pid) = valid_pid_file(path)? {
// store already opened
return Err(mize_err!("MizeStore at path {} is already opened by process with pid {}", path.display(), pid));
} else {
// write our own pid file
fs::remove_file(path.join("pid"));
let pid = std::process::id();
let mut file = OpenOptions::new().write(true).create(true).open(path.join("pid"))?;
write!(file, "{}", pid)?;
}
*/
// init the store
if !path.join("next_id").exists() {
fs::write(path.join("next_id"), "1");
}
Ok(FileStore { path: Path::new(&path).to_owned() })
}
pub fn store_is_opened(store_path: String, instance: &mut Instance) -> MizeResult<bool> {
// this was the old method....
//let valid_pid_file = valid_pid_file(Path::new(&store_path))?.is_some();
let can_connect_to_sock = match instance.spawn_async_blocking("con_test_thread", try_connect_to_sock(store_path.clone())) {
Ok(_) => true,
Err(_) => false,
};
debug!("MiZe Store at {} opened: {}", store_path, can_connect_to_sock);
Ok(can_connect_to_sock)
}
}
async fn try_connect_to_sock(store_path: String) -> MizeResult<()> {
match UnixStream::connect(PathBuf::from(store_path).join("sock")).await {
Err(_) => Err(mize_err!("")),
Ok(_) => Ok(()),
}
}
impl Store for FileStore {
fn new_id(&self) -> MizeResult<String> {
let mut next_id: u64 = String::from_utf8(fs::read(self.path.join("next_id"))
.mize_result_msg(format!("could not read next_id at '{}'", self.path.display()))?)?
.parse()
.mize_result_msg(format!("could not parse next_id at '{}' to u64", self.path.display()))?;
let id_string = format!("{}", next_id);
next_id += 1;
fs::write(self.path.join("next_id"), format!("{}", next_id))?;
return Ok(id_string);
}
fn set(&self, id: MizeId, data: ItemData) -> MizeResult<()> {
let path = self.path.join("store").join(id.namespace_str()).join(id.store_part());
fs::create_dir_all(self.path.join("store").join(id.namespace_str()))?;
let file = OpenOptions::new().write(true).create(true).open(path)?;
ciborium::into_writer(data.cbor(), file)?;
Ok(())
}
fn get_links(&self, item: Item) -> MizeResult<Vec<MizeId>> {
Ok(Vec::new())
}
fn get_backlinks(&self, item: Item) -> MizeResult<Vec<MizeId>> {
Ok(Vec::new())
}
fn get_value_raw(&self, id: MizeId) -> MizeResult<Vec<u8>> {
let path = self.path.join("store").join(id.namespace_str()).join(id.store_part());
let file = OpenOptions::new().read(true).create(true).open(path)?;
let cbor_val: CborValue = ciborium::from_reader(file)
.mize_result_msg(format!("could not read file '{}' from FileStore", self.path.join("store").join(id.store_part()).display()))?;
let path = id.path();
let mut id_iter = path.iter();
id_iter.next();
let id_without_first = id_iter.collect();
Ok(get_raw_from_cbor(&cbor_val, id_without_first)?.to_owned())
}
fn get_value_data_full(&self, id: MizeId) -> MizeResult<ItemData> {
let file_path = self.path.join("store").join(id.namespace_str()).join(id.store_part());
if !file_path.exists() {
}
let file = OpenOptions::new().read(true).open(file_path.clone())
.map_err(|e| mize_err!("fsstoree: Unable to open '{}', err: {}", file_path.display(), e))?;
let cbor_value: CborValue = ciborium::from_reader(file)
.mize_result_msg(format!("could not read file '{}' from FileStore", self.path.join("store").join(id.store_part()).display()))?;
let data: ItemData = cbor_value.into_item_data();
let tmp = id.path();
let mut path_iter = tmp.into_iter();
path_iter.next();
let new_path: Vec<String> = path_iter.map(|v| v.to_owned()).collect();
let ret_data = data.get_path(new_path)?;
return Ok(ret_data);
}
fn id_iter(&self) -> MizeResult<IdIter> {
todo!()
}
fn next_id(&self, prev_id: &str) -> MizeResult<Option<String>> {
todo!()
}
fn first_id(&self) -> MizeResult<String> {
Ok("0".to_owned())
}
}
fn valid_pid_file(path: &Path) -> MizeResult<Option<u32>> {
let pid_file_path = path.join("pid");
// if the pid file does not exist, there is no pid...
if !pid_file_path.exists() {
return Ok(None);
}
// read pid file
let pid: u32 = String::from_utf8(fs::read(&pid_file_path)
.mize_result_msg(format!("Could not read contents of pid file at '{}'", pid_file_path.display()))?)?
.parse()
.mize_result_msg(format!("Could not parse contents of pid file at '{}' to u32", pid_file_path.display()))?;
if Path::new("/proc").join(format!("{}", pid)).exists() {
return Ok(Some(pid));
} else {
return Ok(None);
}
}

View File

@@ -0,0 +1,500 @@
use std::collections::HashMap;
use std::path::PathBuf;
use clap::{ArgAction, ArgMatches};
use clap::{Arg, crate_version, Command};
use tokio::sync::mpsc;
use std::sync::Mutex;
use std::sync::Arc;
use colored::Colorize;
use std::env;
use tracing::{span_enabled, Instrument, Level};
use tracing::level_filters::LevelFilter;
use tracing_subscriber::filter::EnvFilter;
use tracing_subscriber::fmt::FormatEvent;
use tracing_subscriber::registry::LookupSpan;
use tracing_subscriber::fmt::FormatFields;
use tracing_subscriber::fmt::FmtContext;
use tracing_subscriber::fmt::format;
use tracing::Event;
use core::fmt;
use tracing_subscriber::fmt::FormattedFields;
use tracing::{trace, debug, info, warn, error};
use std::sync::atomic::AtomicUsize;
use tracing::{field::Visit, Id, Subscriber};
use tracing_core::Field;
use std::fmt::Write;
use crate::MizeError;
use crate::MizeResult;
use crate::id::MizeId;
use crate::item::ItemData;
static DEFAULT_LOG_LEVEL: LevelFilter = LevelFilter::WARN;
pub fn init_logger(cli_matches: &ArgMatches) {
// to save log messages, before the logger is setup
let mut log_messages: Vec<(Level, String)> = Vec::new();
log_messages.push((Level::INFO, "Starting Mize".to_owned()));
// builder with DEFAULT_LOG_LEVEL
//let mut builder = tracing_subscriber::fmt::Subscriber::builder();
// set loglevel
let mut log_level = DEFAULT_LOG_LEVEL;
log_messages.push((Level::TRACE, format!("set log level from DEFAULT_LOG_LEVEL to {}", DEFAULT_LOG_LEVEL)));
// check env RUST_LOG
let mut val_rust_log = level_from_env("RUST_LOG", log_level);
if let Some(level_rust_log) = val_rust_log.0 {
log_level = level_rust_log;
log_messages.push((Level::TRACE, format!("set log level from the Variable RUST_LOG to {}", level_rust_log)));
}
log_messages.append(&mut val_rust_log.1);
// check env MIZE_LOG
let mut val_mize_log = level_from_env("MIZE_LOG", log_level);
if let Some(level_mize_log) = val_mize_log.0 {
log_level = level_mize_log;
log_messages.push((Level::TRACE, format!("set log level from the Variable MIZE_LOG to {}", level_mize_log)));
}
log_messages.append(&mut val_mize_log.1);
// check -vvvvv flags
let v_flag_count = cli_matches.get_count("verbose");
let default_log_level_as_num = match DEFAULT_LOG_LEVEL {
LevelFilter::OFF => 0,
LevelFilter::ERROR => 1,
LevelFilter::WARN => 2,
LevelFilter::INFO => 3,
LevelFilter::DEBUG => 4,
LevelFilter::TRACE => 5,
};
let new_log_level_num = default_log_level_as_num + v_flag_count;
log_level = match new_log_level_num {
0 => LevelFilter::OFF,
1 => LevelFilter::ERROR,
2 => LevelFilter::WARN,
3 => LevelFilter::INFO,
4 => LevelFilter::DEBUG,
5 => LevelFilter::TRACE,
_ => {
log_messages.push((Level::TRACE, format!("the number of verbose (-v) flags passed ({}) exeeds the maximumg log_level", v_flag_count)));
LevelFilter::TRACE
},
};
log_messages.push((Level::TRACE, format!("set log level from the verbose flags to {}", log_level)));
// check --log-level arg
if let Some(log_level_arg_string) = cli_matches.get_one::<String>("log-level") {
let log_level_from_arg = match log_level_arg_string.to_owned().to_lowercase().as_str() {
"none" => LevelFilter::OFF,
"trace" => LevelFilter::TRACE,
"debug" => LevelFilter::DEBUG,
"info" => LevelFilter::INFO,
"warn" => LevelFilter::WARN,
"error" => LevelFilter::ERROR,
_ => {
log_messages.push((Level::WARN, format!(
"The argument to --log-level \"{}\" is not a valid log-level. loglevel stays at: {}"
, log_level_arg_string
, log_level
)));
log_level
}
};
log_level = log_level_from_arg;
log_messages.push((Level::TRACE, format!("set log level from the --log-level arg to {}", log_level_from_arg)));
}
// check --silent flag
if cli_matches.get_flag("silent") {
if log_level != DEFAULT_LOG_LEVEL {
log_messages.push((Level::WARN, format!(
"The loglevel was set by some method (it's not the DEFAULT_LOG_LEVEL) and is now overwritten by the --siltne to be OFF"
)))
}
log_level = LevelFilter::OFF;
}
// actually set the log_level on the builder
log_messages.push((Level::INFO, format!("loglevel is now {}", log_level)));
// filter loging
//let mut my_env_filter = tracing_subscriber::filter::EnvFilter::new("")
//.add_directive("[get_raw_from_cbor]=debug".parse().expect("error parsing log directive"))
//.add_directive("[MemStore::get_value_raw]=trace".parse().expect("error parsing log directive"))
//.add_directive("trace".parse().unwrap())
//.add_directive("[get_raw_from_cbor]=debug".parse().expect("error parsing log directive"))
//;
MinimalTracer::register();
//builder
//.with_env_filter(my_env_filter)
//.without_time()
//.event_format(MyFormatter)
//.with_max_level(log_level)
//.init();
// write out all log messages from log_messages
for (level, msg) in log_messages {
match level {
Level::ERROR => error!("{}", msg),
Level::WARN => warn!("{}", msg),
Level::INFO => info!("{}", msg),
Level::DEBUG => debug!("{}", msg),
Level::TRACE => trace!("{}", msg),
}
}
}
fn level_from_env(var_name: &str, log_level: LevelFilter) -> (Option<LevelFilter>, Vec<(Level, String)>) {
let mut log_messages: Vec<(Level, String)> = Vec::new();
let var_value = env::var(var_name);
match var_value {
Ok(val) => {
// set loglevel acording to RUST_LOG
let level = match val.to_lowercase().as_str() {
"none" => LevelFilter::OFF,
"trace" => LevelFilter::TRACE,
"debug" => LevelFilter::DEBUG,
"info" => LevelFilter::INFO,
"warn" => LevelFilter::WARN,
"error" => LevelFilter::ERROR,
_ => {
log_messages.push((Level::WARN, format!(
"The value of Environment Variable {}: \"{}\" is not a valid log-level. loglevel stays at: {}"
, var_name
, val
, log_level
)));
log_level
}
};
return (Some(level), log_messages)
},
Err(env::VarError::NotPresent) => {},
Err(env::VarError::NotUnicode(os_string)) => {
let os_string_as_hex : String = os_string.clone().into_encoded_bytes().iter()
.map(|b| format!("{:02x}", b).to_string())
.collect::<Vec<String>>()
.join("");
log_messages.push((Level::DEBUG, format!(
"value of Environment Variable {} was not a unicode valid string. Therefore ignoring it.
\n value with replacement char U+FFFD: {}
\n the WTF-8 raw bytes as hex: {}"
, var_name
, os_string.to_string_lossy(), os_string_as_hex
)));
},
};
return (None, log_messages);
}
struct MyFormatter;
impl<S, N> FormatEvent<S, N> for MyFormatter
where
S: tracing::Subscriber + for<'a> LookupSpan<'a>,
N: for<'a> FormatFields<'a> + 'static,
{
fn format_event(
&self,
ctx: &FmtContext<'_, S, N>,
mut writer: format::Writer<'_>,
event: &Event<'_>,
) -> fmt::Result {
// Format values from the event's's metadata:
let metadata = event.metadata();
// set format
/*
builder.format(|buf, record| {
match record.level() {
}
});
*/
let level_str = match metadata.level() {
&Level::TRACE => format!("[ {} ]", "TRACE".yellow()),
&Level::DEBUG => format!("[ {} ]", "DEBUG".green()),
&Level::INFO => format!("[ {} ]", "INFO".blue()),
&Level::WARN => format!("[ {} ]", "WARN".truecolor(245, 164, 66)),
&Level::ERROR => format!("[ {} ]", "ERROR".red()),
};
// with target
// write!(&mut writer, "{} {}: ", level_str, metadata.target())?;
write!(&mut writer, "{} ", level_str)?;
// Format all the spans in the event's span context.
if let Some(scope) = ctx.event_scope() {
for span in scope.from_root() {
write!(writer, "{}", span.name())?;
// `FormattedFields` is a formatted representation of the span's
// fields, which is stored in its extensions by the `fmt` layer's
// `new_span` method. The fields will have been formatted
// by the same field formatter that's provided to the event
// formatter in the `FmtContext`.
let ext = span.extensions();
let fields = &ext
.get::<FormattedFields<N>>()
.expect("will never be `None`");
// Skip formatting the fields if the span had no fields.
if !fields.is_empty() {
//write!(writer, "{{{}}}", fields)?;
}
write!(writer, ": ")?;
}
}
// Write fields on the event
ctx.field_format().format_fields(writer.by_ref(), event)?;
writeln!(writer)
}
}
// thanks: https://stackoverflow.com/a/75546824
pub struct StringVisitor<'a> {
string: &'a mut String,
}
impl<'a> StringVisitor<'a> {
pub(crate) fn new(string: &'a mut String) -> Self {
StringVisitor { string }
}
}
impl<'a> Visit for StringVisitor<'a> {
fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) {
if field.name() == "message" {
write!(self.string, "{value:?} ").unwrap();
} else {
write!(self.string, "{} = {:?}; ", field.name(), value).unwrap();
}
}
}
struct LogFilter {
target: Option<String>,
level: Option<Level>,
}
pub struct MinimalTracer {
enabled: bool,
filters: Vec<LogFilter>,
level: Arc<Mutex<Level>>,
spans: Arc<Mutex<Vec<Id>>>,
span_levels: Arc<Mutex<HashMap<Id, Level>>>,
span_names: Arc<Mutex<HashMap<Id, String>>>,
span_names_to_enable: Arc<Mutex<Vec<String>>>,
level_no_span: Level,
}
fn string_to_level(string: &str) -> Option<Level> {
match string.to_lowercase().as_str() {
"info" => Some(Level::INFO),
"debug" => Some(Level::DEBUG),
"warn" | "warning" => Some(Level::WARN),
"trace" => Some(Level::TRACE),
"error" => Some(Level::ERROR),
_ => None,
}
}
impl MinimalTracer {
pub fn register() -> Result<(), tracing::subscriber::SetGlobalDefaultError> {
let mut enabled = true;
let mut span_names_to_enable = Vec::new();
let mut my_level = Level::ERROR;
let mut filters: Vec<LogFilter> = Vec::with_capacity(10);
if let Ok(env_value) = env::var("RUST_LOG") {
for filter in env_value.split(',') {
let mut target = Some(filter);
let mut level = None;
if let Some(equals_index) = target.unwrap().find('=') {
let (first, second) = filter.split_at(equals_index);
target = Some(first);
level = string_to_level(&second[1..])
} else {
my_level = string_to_level(target.unwrap()).unwrap();
}
let target_level = string_to_level(target.unwrap());
//if let Some(target_level) = target_level {
//level = Some(target_level);
//target = None;
//}
// in case target is a span name, add id to span_names_to_enable
// for now every target is also a span_name.....
// i would think everything, that contains a "." would be a span, and not a module
// targeet
if let Some(name) = target {
span_names_to_enable.push(name.to_owned());
}
filters.push(LogFilter {
target: target.map(|v| v.to_string()),
level,
});
}
} else {
enabled = false;
}
tracing::subscriber::set_global_default(MinimalTracer {
enabled, filters,
spans: Arc::new(Mutex::new(Vec::new())),
level: Arc::new(Mutex::new(my_level)),
span_names: Arc::new(Mutex::new(HashMap::new())),
span_names_to_enable: Arc::new(Mutex::new(span_names_to_enable)),
span_levels: Arc::new(Mutex::new(HashMap::new())),
level_no_span: my_level,
})
}
}
static AUTO_ID: AtomicUsize = AtomicUsize::new(1);
impl Subscriber for MinimalTracer {
fn enabled(&self, metadata: &tracing::Metadata<'_>) -> bool {
return true;
if metadata.is_span() {
return true;
let mut span_names_to_enable = self.span_names_to_enable.lock().unwrap();
if span_names_to_enable.contains(&metadata.name().to_owned()) {
return true;
}
}
if self.enabled {
if self.filters.is_empty() {
return true;
}
let mut matches: bool;
for filter in &self.filters {
matches = true;
if let Some(level) = filter.level {
if metadata.level() != &level {
matches = false;
}
}
if let Some(target) = &filter.target {
if !metadata.target().starts_with(target) {
matches = false;
}
}
if matches {
return true;
}
}
return false;
}
false
}
fn new_span(&self, _span: &tracing_core::span::Attributes<'_>) -> tracing_core::span::Id {
let mut span_names = self.span_names.lock().unwrap();
let mut span_levels = self.span_levels.lock().unwrap();
let mut span_names_to_enable = self.span_names_to_enable.lock().unwrap();
let id = Id::from_u64(AUTO_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed) as u64);
let span_name = _span.metadata().name().to_string();
span_names.insert(id.clone(), span_name.clone());
if span_names_to_enable.contains(&span_name) {
span_levels.insert(id.clone(), Level::TRACE);
} else {
span_levels.insert(id.clone(), Level::ERROR);
}
id
}
fn record(&self, _span: &tracing_core::span::Id, _values: &tracing_core::span::Record<'_>) {
}
fn record_follows_from(
&self,
_span: &tracing_core::span::Id,
_follows: &tracing_core::span::Id,
) {
}
fn event(&self, event: &tracing::Event<'_>) {
let metadata = event.metadata();
let level = metadata.level();
let my_level = self.level.lock().unwrap();
//println!("uuuuuuuuuuuu event name: {}", metadata.name());
//println!("uuuuuuuuuuuu my_level: {}", my_level);
//println!("uuuuuuuuuuuu event level: {}", level);
if *level > *my_level {
//println!("uuuuuuuuuuuu comparison");
//println!("BLOCKED EVENT")
return;
}
let target = metadata.target();
let mut text = String::new();
let mut visitor = StringVisitor::new(&mut text);
event.record(&mut visitor);
let level_str = match metadata.level() {
&Level::TRACE => format!("[ {} ]", "TRACE".yellow()),
&Level::DEBUG => format!("[ {} ]", "DEBUG".green()),
&Level::INFO => format!("[ {} ]", "INFO".blue()),
&Level::WARN => format!("[ {} ]", "WARN".truecolor(245, 164, 66)),
&Level::ERROR => format!("[ {} ]", "ERROR".red()),
};
//println!("{} {:?}", "META:".red(), metadata.in_current_span());
let mut spans = self.spans.lock().unwrap();
let mut span_names = self.span_names.lock().unwrap();
let span_text = spans.iter()
.map(|id| span_names.get(id).unwrap().to_owned())
.collect::<Vec<String>>()
.join("::");
println!("{level_str} {span_text} {text}");
}
fn enter(&self, _span: &tracing_core::span::Id) {
//println!("eeeeeeeeeeeeeeeee enter");
let mut spans = self.spans.lock().unwrap();
let mut level = self.level.lock().unwrap();
let mut span_levels = self.span_levels.lock().unwrap();
let mut span_names = self.span_names.lock().unwrap();
let name = span_names.get(_span).unwrap().to_owned();
*level = span_levels.get(_span).unwrap().to_owned();
spans.push(_span.clone());
//println!("my_level is now: {}", level);
}
fn exit(&self, _span: &tracing_core::span::Id) {
//println!("eeeeeeeeeeeeeeeee exit");
let mut level = self.level.lock().unwrap();
*level = self.level_no_span;
let mut spans = self.spans.lock().unwrap();
spans.pop();
}
}

View File

@@ -0,0 +1,187 @@
#![ allow( warnings ) ]
use std::ffi::OsString;
use std::path::PathBuf;
use std::process::exit;
use clap::builder::OsStr;
use clap::{ArgAction, ArgMatches};
use clap::{Arg, crate_version, Command};
use mize::error::{MizeError, MizeResult};
use mize::{instance, Instance};
use mize::item::ItemData;
use tokio::sync::{Mutex, mpsc};
use std::sync::Arc;
use std::io::Write;
use colored::Colorize;
use std::env;
use tracing_subscriber::fmt::Subscriber;
use tracing::Level;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::filter::EnvFilter;
use mize::platform::os::logging::init_logger;
use tracing::{trace, debug, info, warn, error};
mod cli;
static APPNAME: &str = "mize";
fn main() {
// welcome to the mize source code
// this is the main entry point for platforms with an os (eg: Linux, MacOS, Windows, BSD, ...)
// Also let's try to make the most best documented code ever. (eventually) so it's fun to work on
let cli_matches = cli_matches();
init_logger(&cli_matches);
// match command
let result = match cli_matches.subcommand() {
// mi daemon
Some(("run", sub_matches)) => cli::run(sub_matches),
Some(("is-running", sub_matches)) => cli::is_running(sub_matches),
// mi serve
Some(("serve", sub_matches)) => cli::stop(sub_matches),
// mi mount
Some(("mount", sub_matches)) => cli::mount(sub_matches),
// mi get
Some(("get", sub_matches)) => cli::get(sub_matches),
// mi set
Some(("set", sub_matches)) => cli::set(sub_matches),
// mi show
Some(("show", sub_matches)) => cli::show(sub_matches),
// mi call
Some(("call", sub_matches)) => cli::call(sub_matches),
// mi create
Some(("create", sub_matches)) => cli::create(sub_matches),
// mi gui
Some(("gui", sub_matches)) => cli::gui(sub_matches),
// mi format-cbor
Some(("format-cbor", sub_matches)) => cli::format_cbor(sub_matches),
// some unknown command passed
Some((cmd, sub_matches)) => cli::run_from_module(cmd, sub_matches),
None => Err(MizeError::new().msg("No subcommand was passed. use --help to list availavle comamnds.")),
};
if let Err(err) = result {
err.log();
exit(1);
}
}
fn cli_matches() -> clap::ArgMatches {
let main = Command::new(APPNAME)
.version(crate_version!())
.author("Sebastian Moser")
.about("The MiZe Command line tool")
.allow_external_subcommands(true)
.arg(Arg::new("verbose")
.long("verbose")
.short('v')
.action(ArgAction::Count)
.global(true)
)
.arg(Arg::new("log-level")
.long("log-level")
.value_name("LOGLEVEL")
.help("set the log-level to one of OFF, ERROR, WARN, INFO, DEBUG, TRACE")
.global(true)
)
.arg(Arg::new("silent")
.long("silent")
.action(ArgAction::SetTrue)
.help("set the log-level to OFF")
.global(true)
)
.arg(Arg::new("folder")
.short('f')
.long("folder")
.help("The folder the Instance stores all it's data and the socket for connections")
.global(true)
)
.arg(Arg::new("config")
.short('c')
.long("config")
.help("overwrite config options")
.global(true)
)
.arg(Arg::new("config-file")
.long("config-file")
.help("specify a config file")
.global(true)
)
.subcommand(
Command::new("run")
.aliases(["r"])
.about("Run a MiZe Instance")
)
.subcommand(Command::new("stop")
.about("Stop a MiZe Instance")
)
.subcommand(
Command::new("mount")
.aliases(["m"])
)
.subcommand(
Command::new("get")
.aliases(["g"])
.arg(Arg::new("id").help("The id or path to get"))
.arg(Arg::new("recurse")
.short('r')
.action(ArgAction::Count)
)
)
.subcommand(
Command::new("set")
.aliases(["s"])
.arg(Arg::new("id").help("The id or path to set"))
.arg(Arg::new("value").help("The value to set the path to"))
)
.subcommand(
Command::new("show")
.aliases(["so"])
.arg(Arg::new("id").help("The id or path to sub to and show"))
)
.subcommand(
Command::new("call")
.aliases(["c"])
)
.subcommand(
Command::new("create")
.aliases(["cr"])
)
.subcommand(
Command::new("is-running")
.aliases(["isr"])
)
.subcommand(
Command::new("gui")
)
.subcommand(
Command::new("format-cbor")
)
.arg_required_else_help(true);
return main.get_matches();
}

View File

@@ -0,0 +1,321 @@
use ciborium::Value as CborValue;
use serde::Deserialize;
use std::env::VarError;
use std::fs;
use tracing::{debug, error, info, trace, warn};
use clap::ArgMatches;
use std::path::{Path, PathBuf};
use std::time::UNIX_EPOCH;
use sha2::{Sha256, Sha512, Digest};
use flate2::bufread::GzDecoder;
use tar::Archive;
use std::fs::File;
use std::io::copy;
use crate::id::MizeId;
use crate::instance::store::Store;
use crate::instance::Instance;
use crate::error::{IntoMizeResult, MizeError, MizeResult};
use crate::item::{data_from_string, IntoItemData, ItemData};
use crate::memstore::MemStore;
use crate::{mize_err, Module};
use crate::instance::module::EmptyModule;
use self::fsstore::FileStore;
pub mod fsstore;
pub mod logging;
#[cfg(target_family = "unix")]
mod unix_socket;
#[cfg(target_family = "unix")]
use crate::platform::os::unix_socket::UnixListener;
//mod web;
pub fn os_instance_init(instance: &mut Instance) -> MizeResult<()> {
// this is the code, that runs to initialize an Instance on a system with an os present.
////// load MIZE_CONFIG_FILE as the instance, which is item 0
match std::env::var("MIZE_CONFIG_FILE") {
Ok(config_file_path) => {
let config = config_from_file(config_file_path)?;
debug!("env var MIZE_CONFIG_FILE present");
instance.set_blocking("0", config)?;
trace!("config after MIZE_CONFIG_FILE env var: {}", instance.get("self/config")?);
}
Err(var_err) => match var_err {
VarError::NotPresent => {
debug!("env var MIZE_CONFIG_FILE NOT present");
},
VarError::NotUnicode(_) => {
warn!("env var MIZE_CONFIG_FILE is not Unicode, so not reading it")
},
}
};
////// load config from MIZE_CONFIG env var
match std::env::var("MIZE_CONFIG") {
Ok(config_string) => {
let config = data_from_string(config_string)?;
debug!("env var MIZE_CONFIG present");
instance.set_blocking("self/config", config)?;
trace!("config after MIZE_CONFIG env var: {}", instance.get("self/config")?);
}
Err(var_err) => match var_err {
VarError::NotPresent => {
debug!("env var MIZE_CONFIG NOT present")
},
VarError::NotUnicode(_) => {
warn!("env var MIZE_CONFIG is not Unicode, so not reading it")
},
}
};
////// if a config.store_path is set, upgrade to the filestore there
let mut test = instance.get("0")?.as_data_full()?;
let mut store_path = match instance.get("self/config/store_path")?.value_string() {
Ok(path) => {
path
},
Err(e) => {
// the default store_path: $HOME/.mize
let home_dir = if let Some(dir) = std::env::home_dir() {
dir
} else {
return Err(mize_err!("could not get home_dir and store_path is not set in the config"));
};
let store_path_buff = home_dir.join(".mize");
let store_path = store_path_buff.display().to_string();
instance.set_blocking("self/config/store_path", store_path.clone().into_item_data())?;
let store_path = instance.get("self/config/store_path")?.value_string()?;
println!("store_path: {}", store_path);
store_path
},
};
if FileStore::store_is_opened(store_path.to_owned(), instance)? {
// if the store is already opened, connect to the instance, that opened it and join
// it's namespace
#[cfg(target_family = "unix")]
{
info!("CONNECTING");
use crate::platform::os::unix_socket;
unix_socket::connect(instance, store_path.into())?;
return Ok(());
}
#[cfg(target_family = "windows")]
{
error!("CONNECTING... would connect, but that is not implemented on windows yet");
}
} else {
// else open it ourselves
let file_store = FileStore::new(store_path.as_str())?;
instance.migrate_to_store(Box::new(file_store))?;
let path = Path::new(&store_path).to_owned();
#[cfg(target_family = "unix")]
{
instance.add_listener(UnixListener::new(path)?)?;
}
#[cfg(target_family = "windows")]
{
warn!("would add a Listener on a local socket, but that is not yet implemented for windows");;
}
}
Ok(())
}
pub fn seconds_since_modification(path: &Path) -> MizeResult<u64> {
let metadata = fs::metadata(path)?;
let modified = metadata.modified()?;
let duration = modified.duration_since(UNIX_EPOCH)?;
Ok(duration.as_secs())
}
pub fn get_module_hash(instance: &mut Instance, name: &str, mut options: ItemData) -> MizeResult<(String, String)> {
let mut selector_data = instance.get("self/config/selector")?.as_data_full()?;
// handle the case, when we want a module for not the system we are running on...
println!("get_module_hash: name: {}", name);
let mut name_parts = name.split(".");
if name_parts.next().ok_or(mize_err!("no 0th element in the modName"))? == "cross" {
let system = name_parts.next().ok_or(mize_err!("no 1th element in the modName"))?;
let mod_name: String = name_parts.collect();
println!("get_module_hash: mod_name: {}", mod_name);
if mod_name == "" {
return Err(mize_err!("no 2nd element in the modName"));
}
selector_data.set_path("system", system)?;
selector_data.set_path("modName", mod_name)?;
} else {
selector_data.set_path("modName", name)?;
}
selector_data.merge(options);
// important to produce the same hash for the sama selector data
selector_data.sort_keys();
let selector_str = selector_data.to_json()?;
let mut hasher = Sha256::new();
hasher.update(selector_str.clone().as_bytes());
let result = hasher.finalize();
let mut hex: String = result.iter()
.map(|b| format!("{:02x}", b).to_string())
.collect::<Vec<String>>()
.join("");
hex.truncate(32);
return Ok((hex, selector_str));
}
fn download_module(instance: &mut Instance, store_path: &str, module_url: &str, module_hash: &str, module_name: &str, module_selector: &str) -> MizeResult<()> {
let tmp_file_path_gz = format!("{}/{}.tar.gz", store_path, module_hash);
let tmp_gz_file = if !PathBuf::from(tmp_file_path_gz.clone()).exists() {
let mut tmp_gz_file = File::create(&tmp_file_path_gz)?;
debug!("downloading module '{}' with hash: {}", module_name, module_hash);
let response = http_req::request::get(format!("http://{}/mize/dist/{}-{}.tar.gz", module_url, module_hash, module_name), &mut tmp_gz_file)?;
let code = response.status_code();
if code != 200.into() {
return Err(mize_err!("failed to get module '{}' with hash '{}' and selector: {}", module_name, module_hash, module_selector).msg(format!("the http request to fetch the module returned a status code of: {}", code)));
}
tmp_gz_file
} else {
std::fs::File::open(tmp_file_path_gz.clone())?
};
let tar = GzDecoder::new(std::io::BufReader::new(tmp_gz_file));
let mut archive = Archive::new(tar);
//archive.set_mask(umask::Mode::parse("rwxrwxrwx")?.into());
let target_dir = format!("{}/modules/{}", store_path, module_hash);
std::fs::create_dir_all(target_dir.as_str())?;
archive.unpack(target_dir.as_str())?;
fs::remove_file(&tmp_file_path_gz)?;
Ok(())
}
pub fn fetch_module(instance: &mut Instance, module_name: &str) -> MizeResult<String> {
let module_name_with_slashes = module_name.replace(".", "/");
if let Ok(module_path) = instance.get(format!("self/config/module_dir/{}", module_name_with_slashes))?.value_string() {
// in case a dir is configured, which holds the modules output
return Ok(module_path);
} else {
// if null, load it from the url
let module_url = instance.get("self/config/module_url")?.value_string()?;
let store_path = instance.get("self/config/store_path")?.value_string()?;
let (module_hash, module_selector) = get_module_hash(instance, module_name, ItemData::new())?;
if !PathBuf::from(format!("{}/modules/{}", store_path, module_hash.as_str())).exists() {
// download module if it does not exist
download_module(instance, store_path.as_str(), module_url.as_str(), module_hash.as_str(), module_name, module_selector.as_str())?;
}
return Ok(format!("{}/modules/{}", store_path, module_hash.as_str()));
}
}
pub fn load_module(instance: &mut Instance, module_name: &str, path: Option<String>) -> MizeResult<()> {
let module_path = if path.is_some() {
format!("{}/lib/libmize_module_{}.so", path.unwrap(), module_name)
} else {
format!("{}/lib/libmize_module_{}.so", fetch_module(instance, module_name)?, module_name)
};
let lib = unsafe { libloading::Library::new(module_path)? };
let func: libloading::Symbol<unsafe extern "C" fn(&mut Box<dyn Module + Send + Sync>, Instance) -> ()> = unsafe { lib.get(format!("get_mize_module_{}", module_name).as_bytes())? };
let mut module: Box<dyn Module + Send + Sync> = Box::new(EmptyModule {});
unsafe { func(&mut module, instance.clone()) };
let mut modules_inner = instance.modules.lock()?;
module.init(&instance)?;
modules_inner.insert(module_name.to_owned(), module);
unsafe {
// dropping the lib, would (i suspect) free all memory, of the library's code, which would
// make the module's vtable point into empty memory
std::mem::forget(lib);
}
Ok(())
}
pub fn config_from_cli_args(matches: &ArgMatches) -> MizeResult<ItemData> {
let mut config = ItemData::new();
if let Some(config_file_path) = matches.get_one::<String>("config-file") {
config.merge(config_from_file(config_file_path.to_string())?);
trace!("config after --config-file arg: {}", config);
}
if let Some(config_string) = matches.get_one::<String>("config") {
config.merge(data_from_string(config_string.to_string())?);
trace!("config after --config arg: {}", config);
}
return Ok(config);
}
pub fn config_from_file(file_path: String) -> MizeResult<ItemData> {
let toml_string = fs::read_to_string(&file_path)
.mize_result_msg(format!("Could not read file {} as mize config", &file_path))?;
let item_data = ItemData::from_toml(toml_string.as_str())?;
return Ok(item_data);
}

View File

@@ -0,0 +1,212 @@
use std::fs;
use std::path::{Path, PathBuf};
use tokio::net::{UnixListener as TokioUnixListener, UnixStream};
use tokio::net::unix::{OwnedReadHalf, OwnedWriteHalf};
use tokio::io::{Interest, AsyncReadExt, AsyncWriteExt};
use flume::{Receiver, Sender, unbounded};
use ciborium::Value as CborValue;
use tracing::{info, warn, debug};
use crate::instance::connection::{ConnListener, Connection};
use crate::instance::{self, Instance};
use crate::error::{MizeError, MizeResult, IntoMizeResult};
use crate::proto::{self, MizeMessage};
pub struct UnixListener {
sock_path: PathBuf,
}
impl UnixListener {
pub fn new(store_path: PathBuf) -> MizeResult<UnixListener> {
Ok(UnixListener { sock_path: store_path.join("sock").to_owned() })
}
}
pub fn connect(instance: &mut Instance, store_path: PathBuf) -> MizeResult<()> {
let conn_id = instance.spawn_async_blocking("unix_connection", connect_async(instance.clone(), store_path) )?;
let ns_of_peer_str = instance.get(format!("inst/con_by_id/{}/peer/0/config/namespace", conn_id))?.value_string()?;
let ns_of_peer = instance.namespace_from_string(ns_of_peer_str)?;
info!("connect_async ... ns_of_peer: {}", ns_of_peer.clone().as_string());
instance.connection_set_namespace(conn_id, ns_of_peer.clone());
instance.set_namespace(ns_of_peer);
Ok(())
}
async fn connect_async(mut instance: Instance, store_path: PathBuf) -> MizeResult<u64> {
let mut stream = UnixStream::connect(store_path.join("sock")).await?;
let (unix_read, unix_write) = stream.into_split();
let (send_tx, send_rx) = unbounded::<MizeMessage>();
let conn_id = instance.new_connection(send_tx)?;
let cloned_instance = instance.clone();
instance.spawn("incomming", move || {
let result = unix_incomming(unix_read, cloned_instance, conn_id);
// if unix incomming fails, close the connection
if let Err(err) = result {
warn!("Connection closing because of EOF");
}
Ok(())
});
let outgoing_cloned_instance = instance.clone();
instance.spawn("outgoing", move || {
let result = unix_outgoing(unix_write, send_rx, outgoing_cloned_instance, conn_id);
// if writing fails, close this connection
if let Err(err) = result {
warn!("Connection closing because of EOF");
}
Ok(())
});
return Ok(conn_id);
}
impl ConnListener for UnixListener {
fn listen(self, mut instance: Instance) -> MizeResult<()> {
instance.spawn_async("unix listen async", unix_listen(self, instance.clone()));
Ok(())
}
}
async fn unix_listen(listener: UnixListener, mut instance: Instance) -> MizeResult<()> {
// remove the file at sock_path if it already exists
fs::remove_file(&listener.sock_path);
let listener = TokioUnixListener::bind(&listener.sock_path)
.mize_result_msg(format!("Could not bind to unix socket at '{}'", listener.sock_path.display()))?;
loop {
let (mut unix_sock, addr) = listener.accept().await
.mize_result_msg("Error while accepting Unix sock connection")?;
info!("new connection");
let (send_tx, send_rx) = unbounded::<MizeMessage>();
let (unix_read, unix_write) = unix_sock.into_split();
let conn_id = instance.new_connection(send_tx)?;
let cloned_instance = instance.clone();
instance.spawn("incomming", move || {
let result = unix_incomming(unix_read, cloned_instance, conn_id);
// if unix incomming fails, close the connection
if let Err(err) = result {
warn!("Connection closing because of EOF");
}
Ok(())
});
let outgoing_cloned_instance = instance.clone();
instance.spawn("outgoing", move || {
let result = unix_outgoing(unix_write, send_rx, outgoing_cloned_instance, conn_id);
// if writing fails, close this connection
if let Err(err) = result {
warn!("Connection closing because of EOF");
}
Ok(())
});
}
}
fn unix_outgoing(mut unix_write: OwnedWriteHalf, send_rx: Receiver<MizeMessage>, mut instance: Instance, conn_id: u64) -> MizeResult<()> {
for msg in send_rx {
debug!("unix outgoing got msg: {}", msg);
let adapter = MyCiboriumWriter { inner: &mut unix_write, instance: &mut instance };
let value = msg.value();
ciborium::into_writer(&value, adapter)?
}
Ok(())
}
fn unix_incomming(mut unix_read: OwnedReadHalf, mut instance: Instance, conn_id: u64) -> MizeResult<()> {
loop {
//println!("unix incoming loop...");
let adapter = MyCiboriumReader { inner: &mut unix_read, instance: &mut instance };
let value: CborValue = ciborium::from_reader(adapter)?;
if let CborValue::Integer(_) = value {
break;
}
let msg = MizeMessage::new(value, conn_id);
debug!("unix incoming got msg: {}", msg);
instance.got_msg(msg);
}
Ok(())
}
struct MyCiboriumWriter<'a> {
inner: &'a mut OwnedWriteHalf,
instance: &'a mut Instance,
}
impl<'a> ciborium_io::Write for MyCiboriumWriter<'a> {
type Error = MizeError;
fn write_all(&mut self, data: &[u8]) -> Result<(), Self::Error> {
//println!("CALL: write_all");
let runtime = self.instance.runtime.lock()?;
//println!("after runtime");
let num_read = runtime.block_on(self.inner.write_all(data))?;
Ok(())
}
fn flush(&mut self) -> Result<(), Self::Error> {
//println!("CALL flush");
let handle = self.instance.async_get_handle();
let result = handle.block_on(self.inner.flush());
//println!("flush result: {:?}", result);
Ok(())
}
}
struct MyCiboriumReader<'a> {
inner: &'a mut OwnedReadHalf,
instance: &'a mut Instance,
}
async fn test(test: &mut OwnedReadHalf, buf: &mut [u8]) {
//println!("hello from async...");
//println!("len: {}", buf.len());
let num_read = test.read_u8().await;
//println!("num_read: {:?}", num_read);
//test.read_exact(buf).await;
//println!("hello from async... twoooooooooooooo");
}
impl<'a> ciborium_io::Read for MyCiboriumReader<'a> {
type Error = MizeError;
fn read_exact(&mut self, data: &mut [u8]) -> Result<(), Self::Error> {
//println!("data before: {:?}", data);
//let runtime = self.instance.runtime.lock()?;
//println!("are we here after runtime lock()");
//runtime.block_on(test(self.inner, data));
//println!("are we hereeeeeeeeeeeeeeeeeeee");
//return Ok(());
//println!("incoming before block_on");
let handle = self.instance.async_get_handle();
let num_read = handle.block_on(self.inner.read_exact(data))?;
//println!("incoming num_read: {:?}", num_read);
//println!("data after: {:?}", data);
// if we read 0 bytes, that means the reader stream closed and we should terminate the
// connection
//if num_read == 0 {
//return Err(MizeError::new().msg("ReceiverStream closed"));
//}
Ok(())
}
}

View File

@@ -0,0 +1,239 @@
static SERVER_PORT: u16 = 3000;
static API_ENDPOINT: &str = "@mize";
pub fn run() -> Result<Web, MizeError> {
/*
* - Run a Webserver
*
*
*
*/
// I will deal with renders later. cli only for now
// let renders =
// for render in &*renders {
// match render.render_type {
// RenderType::JsonUi => {},
// RenderType::WebComponent => {
// let url_path = String::from("/api/render/") + &render.name + "/webroot";
// let file_path = mutexes.mize_folder.clone() + "/mr/" + &render.folder[..] + "/" + &render.webroot.clone()[..];
// let serve_dir = get_service(ServeDir::new(&file_path)).handle_error(handle_error);
// app = app.nest_service(&url_path, serve_dir);
// },
// }
// }
// let serve_client = get_service(ServeDir::new("js-client/src")).handle_error(handle_error);
let mut app = Router::new()
.route(format!("/{}/socket", API_ENDPOINT), get(websocket_handler))
// .route(format!("/{}/render/:id", API_ENDPOINT), get(get_render_main))
// .route(format!("/{}/file", API_ENDPOINT), get(get_file_handler))
// .merge(SpaRouter::new(format!("/{}/client", API_ENDPOINT), "js-client/src").index_file("main.html"))
// .nest_service(format!("/{}/client", API_ENDPOINT), serve_client)
.with_state(main)
.fallback(render_item);
let addr = SocketAddr::from(([0, 0, 0, 0], SERVER_PORT));
// tracing::debug!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.expect(format!("Unable to bind axum::Server to {}", addr));
}
pub struct Web {
}
impl Web {
}
async fn handle_error(_err: std::io::Error) -> impl IntoResponse {
MizeError::new(0).msg("internal server error").handle()
(StatusCode::INTERNAL_SERVER_ERROR, "Something went wrong...")
}
async fn render_item(uri: Uri) -> impl IntoResponse {
Html(fs::read_to_string("js-client/src/main.html").unwrap())
}
//#[axum_macros::debug_handler]
//async fn get_render_main(extract::Path(id): extract::Path<String>, State(mutexes): State<Mutexes>) -> http::Response<String> {
// let renders = &*mutexes.renders.lock().await;
// let render = renders.iter().filter(|&render| render.name == id).nth(0)
// .unwrap_or(renders.iter().filter(|&render| render.name == "mize-mmejs").nth(0).expect("mize-mmejs"));
// let file_path = mutexes.mize_folder.clone() + "/mr/" + &render.folder + "/" + &render.main;
// match render.render_type {
// RenderType::JsonUi => {
// return Response::builder()
// .header("content-type", "application/json")
// .status(StatusCode::OK)
// .body(fs::read_to_string(file_path).unwrap()).unwrap();
// },
// RenderType::WebComponent => {
// return Response::builder()
// .header("content-type", "application/javascript")
// .status(StatusCode::OK)
// .body(fs::read_to_string(file_path).unwrap()).unwrap();
// },
// }
//}
async fn websocket_handler(
ws: WebSocketUpgrade,
State(main): State<&Main>,
headers: HeaderMap,
) -> impl IntoResponse {
ws.on_upgrade(|socket| handle_websocket_connection(socket, main, headers))
}
async fn handle_websocket_connection(
socket: WebSocket,
main: &Main,
headers: HeaderMap,
){
//TODO: auth
//socket_tx.send() ... send out the socket
//socket_rx.read() ... read a message from the socket
let (mut socket_tx, mut socket_rx) = socket.split();
// msg_tx.send() ... send a MizeMessage to a Peer. should be in a Peer struct
// msg_rx.read() ... read MizeMessages that should be sent to the Peer that this ws connection is to
let (msg_tx, msg_rx): (Sender<proto::MizeMessage>, Receiver<proto::MizeMessage>) = mpsc::channel(SOCKET_CHANNEL_SIZE);
// why is this actually
let mut msg_rx = ReceiverStream::new(msg_rx);
// check if mize-module header exists
// let origin = if let Some(mod_header_val) = headers.get("mize-module"){
// let mut mods = mutexes.modules.lock().await;
// let mut client_id = mutexes.next_free_client_id.lock().await;
// let mut module = Module{
// tx: msg_tx.clone(),
// client_id: *client_id,
// name: String::from("tmp"),
// kind: ModuleKind::Extern(),
// registered_types: Vec::new(),
// };
// *client_id += 1;
// if let Ok(name) = mod_header_val.to_str() {
// module.name = name.to_string();
// } else {
// let err = MizeError::new(113)
// .extra_msg("The mize-module Header could not be decoded into a String.");
// msg_tx.send(proto::MizeMessage::Json(err.to_json_message())).await;
// return;
// };
// println!("module {} connected", module.name);
// mods.push(module.clone());
// drop(mods);
// Peer::Module(module)
// } else {
// };
// Forwarding messages
tokio::spawn(async move {
while let Some(msg) = msg_rx.next().await {
match msg {
proto::MizeMessage::Json(json_msg) => {
if let Ok(msg) = serde_json::to_string(&json_msg) {
let result = socket_tx.send(Message::Text(msg.clone())).await;
if let Err(e) = result {
println!("Client no longer Connected: {:?}", e);
remove_peer(myorigin.clone(), mymutexes.clone()).await;
} else {
println!("SENDING: {}", msg);
}
} else {
let err: MizeError = MizeError::new(11).extra_msg("error while serializing a json message").handle();
if let Ok(text) = serde_json::to_string(&err.to_json()){
socket_tx.send(Message::Text(text));
} else {
MizeError::new(11).extra_msg("not even being able to serialize an error, while failing to serializing a message").handle();
}
}
}
proto::MizeMessage::Bin(bin_msg) => {
socket_tx.send(Message::Binary(bin_msg.raw)).await;
}
proto::MizeMessage::Pong() => {
socket_tx.send(Message::Pong(Vec::new())).await;
}
proto::MizeMessage::Close((num, reason)) => {
println!("SENDING Close message");
socket_tx.send(Message::Close(Some(CloseFrame{code: num, reason: Cow::Owned(reason)}))).await;
}
}
}
});
// Reading messages
while let Some(result) = socket_rx.next().await {
let msg = if let Ok(msg) = result {msg} else {
let err = MizeError::new(115).extra_msg("TCP Connection Closed");
remove_peer(origin.clone(), mutexes.clone());
return;
};
match msg {
Message::Binary(b) => {
println!("Recieved a Binary Message. Those are not implemented yet.")
},
Message::Text(text) => {
println!("GOT: {}", text);
let json_msg: proto::JsonMessage = serde_json::from_str(&text).expect("error parsing json");
if let Err(err) = proto::handle_json_msg(json_msg, origin.clone(), mutexes.clone()).await {
let err_msg: MizeMessage = err.handle().into();
origin.send(err_msg).await;
};
},
Message::Close(inner) => {
match inner {
Some(CloseFrame{code, reason}) => {
origin.send(MizeMessage::Close((code, reason.into_owned()))).await;
},
None => {
origin.send(MizeMessage::Close((1000, "You didn't send a Close Reason".to_owned()))).await;
}
};
println!("Close from Client");
remove_peer(origin.clone(), mutexes.clone()).await;
}
Message::Ping(_) => {
msg_tx.send(MizeMessage::Pong());
},
Message::Pong(_) => {
let err = MizeError::new(11)
.extra_msg("unhandeld WebSocket-Message type: Pong")
.handle();
msg_tx.send(proto::MizeMessage::Json(err.to_json_message())).await;
},
};
};
}

View File

@@ -0,0 +1,30 @@
export async function init_mize(config) {
if ("mize" in window) {
window.mize.set("self/config", config)
return window.mize
}
let config_str = "";
if (typeof config == "string") {
config_str = config
} else if (typeof config == "object") {
config_str = JSON.stringify(config)
} else {
throw "Error: config passed to init_mize() is not of type 'object' or 'string'"
}
const { new_js_instance } = wasm_bindgen;
await wasm_bindgen(config.module_dir.mize + "/mize_bg.wasm")
window.mize = await new_js_instance(config_str)
window.mize.mod = {}
window.mize.init()
return window.mize
}

View File

@@ -0,0 +1,276 @@
use core::convert::From;
use core::option::Option;
use std::ptr::NonNull;
use std::panic;
use web_sys::js_sys::{self, eval, Promise};
use crate::id::MizeId;
use crate::item::{Item, ItemData};
use crate::platform::wasm::js_sys::Function;
use web_sys::{WorkerOptions, WorkerType};
use web_sys::Worker;
use crate::instance::{self, Instance};
use crate::error::MizeResult;
use crate::{mize_err, Module};
use crate::MizeError;
use crate::core::item::IntoItemData;
use crate::instance::module::EmptyModule;
// console_log macro
// that can be copied into other files for debugging purposes
#[cfg(feature = "wasm-target")]
use wasm_bindgen::prelude::*;
#[cfg(feature = "wasm-target")]
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[cfg(feature = "wasm-target")]
macro_rules! console_log {
// Note that this is using the `log` function imported above during
// `bare_bones`
($($t:tt)*) => (unsafe { log(&format_args!($($t)*).to_string())})
}
#[cfg(not(feature = "wasm-target"))]
macro_rules! console_log {
// Note that this is using the `log` function imported above during
// `bare_bones`
($($t:tt)*) => ()
}
//end of console_log macro
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
pub fn wasm_instance_init(instance: &mut Instance) -> MizeResult<()> {
console_log!("Hello world from wasm_instance_init!!!!!!!!!!");
Ok(())
}
pub fn load_module(instance: &mut Instance, name: &str, path: Option<String>) -> MizeResult<()> {
console_log!("loading module: {}", name);
wasm_bindgen_futures::spawn_local(load_module_async(instance.clone(), name.to_owned(), path));
Ok(())
}
async fn load_module_async(mut instance: Instance, name: String, path: Option<String>) -> () {
let name = name.as_str();
let module_url = fetch_module(&mut instance, name).unwrap();
let memory = wasm_bindgen::memory();
let mut empty_module: Box<dyn Module + Send + Sync> = Box::new(EmptyModule {});
let mut mize = instance.clone();
let empty_module_ptr = Box::into_raw(Box::new(empty_module));
let empty_module_usize = empty_module_ptr as usize;
let mize_usize = Box::into_raw(Box::new(mize)) as usize;
console_log!("testttttttttttttt in load_module_async...");
let function_str = format!(r#"
const promise = new Promise((resolve, reject) => {{
import("{module_url}/mize_module_{name}.js").then( module => {{
const wasm_bindgen = module.get_wasm_bindgen();
const {{ wasm_get_mize_module_mme }} = wasm_bindgen;
wasm_bindgen("{module_url}/mize_module_{name}_bg.wasm", memory).then( () => {{
console.log("empty_module_usize:", empty_module_usize)
console.log("mize_usize:", mize_usize)
//resolve()
resolve(wasm_get_mize_module_mme(empty_module_usize, mize_usize))
}})
}})
}})
return promise;
"#);
let function = Function::new_with_args("memory, empty_module_usize, mize_usize", &function_str);
let ret_value: JsValue = match function.call3(&JsValue::null(), &memory, &empty_module_usize.into(), &mize_usize.into()) {
Ok(val) => val,
Err(err) => {
console_log!("failed to load the wasm mize module....");
console_log!("{:?}", err);
return;
},
};
console_log!("got ret_value: {:?}", ret_value);
let ret_promise = Promise::from(ret_value);
let ret_result = match wasm_bindgen_futures::JsFuture::from(ret_promise).await {
Ok(val) => val,
Err(err) => {
console_log!("err awaiting ret_promise");
console_log!("{:?}", err);
return;
},
};
let mut filled_module = unsafe {
Box::from_raw(empty_module_ptr)
};
console_log!("before....");
let a = instance.get("0/config");
console_log!("after....");
//filled_module.init(&instance);
/*
console_log!("got ret_result: {:?}", ret_result);
let ret_f64 = ret_result.as_f64().unwrap();
console_log!("got ret_f64: {:?}", ret_f64);
let ret_usize = ret_f64.floor() as usize;
let mut module: Box<dyn Module + Send + Sync> = Box::new(EmptyModule {});
unsafe {
let get_mize_module_fn_ptr = ret_usize as *const fn(&mut Box<dyn Module + Send + Sync>, Instance) -> ();
let get_mize_module_fn: fn(&mut Box<dyn Module + Send + Sync>, Instance) -> () = unsafe { std::mem::transmute(get_mize_module_fn_ptr) };
get_mize_module_fn(&mut module, instance.clone());
}
*/
}
pub fn fetch_module(instance: &mut Instance, name: &str) -> MizeResult<String> {
let module_name_with_slashes = name.replace(".", "/");
if let Ok(module_path) = instance.get(format!("self/config/module_dir/{}", module_name_with_slashes))?.value_string() {
return Ok(module_path);
} else {
let module_url = instance.get("self/config/module_url")?.value_string()?;
return Ok(format!("{}/{}", module_url, module_name_with_slashes));
}
}
async fn download_module(instance: &mut Instance, name: &str) -> MizeResult<js_sys::ArrayBuffer> {
use wasm_bindgen_futures::JsFuture;
use web_sys::{Request, RequestInit, RequestMode, Response};
let opts = RequestInit::new();
opts.set_method("GET");
opts.set_mode(RequestMode::Cors);
let url = format!("{}/mize_module_{}_bg.wasm", fetch_module(instance, name)?, name);
let request = Request::new_with_str_and_init(&url, &opts).unwrap();
let window = web_sys::window().unwrap();
let resp_value = JsFuture::from(window.fetch_with_request(&request)).await.unwrap();
let resp: Response = resp_value.dyn_into().unwrap();
let wasm_bytes: js_sys::ArrayBuffer = JsFuture::from(resp.array_buffer().unwrap()).await.unwrap().dyn_into().unwrap();
Ok(wasm_bytes)
}
#[wasm_bindgen]
pub struct JsInstance {
inner: NonNull<Instance>,
}
#[wasm_bindgen]
pub struct JsItemHandle {
instance: NonNull<Instance>,
id: MizeId,
}
#[wasm_bindgen]
pub async fn new_js_instance(config_json_str: String) -> JsInstance {
panic::set_hook(Box::new(console_error_panic_hook::hook));
let config = ItemData::from_json(config_json_str).expect("parsing of json config failed");
let mut instance = Instance::empty().expect("Instance::empty() failed");
instance.set_blocking("0/config", config).expect("Failed to set the config at item 0");
let mut js_instance = JsInstance { inner: NonNull::from(Box::leak(Box::new(instance))) };
return js_instance;
}
#[wasm_bindgen]
impl JsInstance {
#[wasm_bindgen]
pub unsafe fn init(&mut self) -> MizeResult<()> {
self.inner.as_mut().init()
}
#[wasm_bindgen]
pub unsafe fn set(&mut self, id: String, value: String) -> () {
let data = value.into_item_data();
console_log!("data in set: {}", data);
self.inner.as_mut().set(id, data);
}
#[wasm_bindgen]
pub unsafe fn get_handle(&mut self, id: String) -> MizeResult<JsItemHandle> {
let item = self.inner.as_mut().get(id)?;
Ok(JsItemHandle { instance: self.inner, id: item.id()})
}
}
impl JsInstance {
pub fn inner(&mut self) -> &mut Instance {
unsafe {
self.inner.as_mut()
}
}
}
#[wasm_bindgen]
impl JsItemHandle {
#[wasm_bindgen]
pub unsafe fn value_string(&mut self) -> MizeResult<String> {
let item = self.instance.as_mut().get(self.id.clone())?;
let string = item.value_string()?;
Ok(string)
}
#[wasm_bindgen]
pub unsafe fn as_data_full(&mut self) -> MizeResult<JsValue> {
let item = self.instance.as_mut().get(self.id.clone())?;
let data_raw = item.as_data_full()?;
let jsvalue = serde_wasm_bindgen::to_value(data_raw.cbor())?;
Ok(jsvalue)
}
}
impl From<MizeError> for JsValue {
fn from(value: MizeError) -> Self {
let string = format!("{:?}", value);
string.into()
}
}

View File

View File

@@ -0,0 +1,31 @@
use crate::instance::MizeInstance;
#[test]
fn most_minimal_instance_usage() {
// so I'm actually starting the effort for a better code base here in the tests
// i want to write down how you would use the very important instance type
// and then implement it to make it usable in this way
// how every mize based program to run on an os should create a mize instance
// opens the instance the way the user has configured (with env vars, build args, or config
// files)
// and we just call it "new()" and not "default_system()" so that it is clear that this is the
// method that should be used
let mize = MizeInstance::new();
// then the most important things you do with your mize instance is to get/set stuff
let name: MizeItem = mize.get("1/namespace");
let id = mize.new_item();
mize.set(id.join("name"), "test-item");
// or register update callbacks and update
//mize.on_update("5/name", |delta| println!(delta.new_value()));
//mize.update("5/name", "other-item")
// the goal is that also most things about the MizeInstance itself is controlled by
// get/set/update -ing values at the 0/ paths
}

View File

@@ -0,0 +1,6 @@
[config]
namespace = "test.c2vi.dev"
test = "hello from test item"
[config.module_dir]