How to escape a closing tag in block comments /** */? - rust

I want to have an example regex in a comment:
/**
* Example = ".*/full/.*" }}`
*/
pub fn my_fun() {}
I get a broken comment since */ is considered as ending the comment.
What would be a proper way to escape this so that some autogenerated docs don't display the escape character?

You can do something like
/**
* Example = ".\*\/full/.*" }}
*/
pub fn my_fun(){}
It'll get rendered as
This should do.
But the recommended ways in rust are
https://doc.rust-lang.org/book/ch03-04-comments.html
https://doc.rust-lang.org/book/ch14-02-publishing-to-crates-io.html#making-useful-documentation-comments

Related

Quickly (un)comment blocks of code in Rust

In languages such as Java, I often use the following pattern for quickly commenting/uncommenting whole blocks of code:
/* *
hello.world();
/* */
// I just have to add one '/' and the block is uncommented:
/* */
hello.world();
/* */
However, in Rust, the above code creates a syntax error, as it is not allowed to have unequal numbers of /* and */ in a Rust file.
But is there a similar way for quickly commenting/uncommenting blocks in Rust that does not involve using editor macro-commands?
You can use single-line comments to activate/deactivate your multi-lines comment, e.g.
/*
commented_out();
// */
//*
not_commented();
// */

[Best practice for optional in proto or default value

We are setting up our rust services and using prost-build to bridge between proto <-> rust land. Our proto definitions are in proto3
Lets take the following proto message:
message Test {
string id = 1;
string body = 2;
string maybe_nullable_thing = 3;
}
This generates a struct like so:
pub struct Test {
#[prost(string, tag="1")]
pub id: ::prost::alloc::string::String,
#[prost(string, tag="2")]
pub body: ::prost::alloc::string::String,
#[prost(string, tag="3")]
pub maybe_nullable_thing: ::prost::alloc::string::String,
}
In other languagues where we have tried this, the fields of a proto message are optional by design and can be left out. In the example there can be cases where maybe_nullable_thing can not be set.
I can work around this issue by using the optional keyword. Altho I remember that it was not the best practice to do so(maybe I am mistaken?)
In terms of best practice with proto3 and rust in general, is it okay to use the optional keyword? If I use serde along with my Test struct I can see the default values of all the fields begin set to "".to_owned() (or empty string).
So I am not sure whats the best practice here? Would love to get some pointers on the best way forward here.
Looking at the readme for Tokio's PROST! tool, it appears their advice is to wrap any non-repeated and non-scalar fields, or any optional fields with Option<T>. This may or may not be different for prost-build, but it should give you a good reference to what's expected when using proto3 and Rust.
In general, however, you should wrap any value you want to be optional in Option<T>. This is not a bad practice, this is the default, standard way to represent "maybe nullable things" in Rust.

field started with % in struct in Specman

I am new to Specman, just don't know what the "%" prefix of a field in struct mean?
for example, the codes below.
struct packet_s like any_sequence_item {
%addr : uint(bits: 2);
%data : list of byte;
...
};
What's the difference if we removed the "%"?
Thanks.
https://support1.cadence.com/api/CosWeb/TechPubDocViewerCosLite/fc3d80347c2f211300916bfa538b076651c4801891f9ea50632a49a087be22bdb643c4344882eaa17a4f8911ffeae7ae4b135bd5c651280b198cdf0fd45653fbb58f41e83ef196b05bc14ce64bdbe65f0cf4d2da8a027d933155221675c881fba3d831ea161e8d008c25cb280d677e9451f5aa8a0451a1ef2b0946a87c48f62860c3700f80457adc49c9b0edc1cd04118c01e7fcfcf36e1518240c3117007cd3886d869848346b919165453ac5e2d13c/Docs/sn_eref/sn_eref21.09/sn_eref.pdf
To get all support\docs related Specman you should first register to their website

How do I return text from the MediaWiki SearchAfterNoDirectMatch hook?

I am trying to write a MediaWiki Search Hook that will list native files in the file system and then, eventually, allow a person to click on one of the files and view its content.
My extensions.json contains this:
"Hooks": {
"SearchAfterNoDirectMatch": "MediaWiki\\Extension\\NativeFileList\\Hooks::onSearchAfterNoDirectMatch"
},
My Hooks::onSearchAfterNoDirectMatch file looks like this:
namespace MediaWiki\Extension\NativeFileList;
class Hooks {
/**
* #see https://www.mediawiki.org/wiki/Manual:Hooks/SearchAfterNoDirectMatch
* #called from https://gerrit.wikimedia.org/g/mediawiki/core/+/master/includes/search/SearchNearMatcher.php
* #param $searchterm
* #param $title - array of titles
* Returns true if it found something, false is otherwise
*/
public static function onSearchAfterNoDirectMatch( $searchterm, &$title ) {
$title=Title::newFromText( "test", "bar");
return false;
}
}
My problem is that no text is returned. Well, it's worse than that. With the above code, I get an exception (but I don't know how to debug it, because I can't see the exception). If I take the line setting $title out, it returns. If i change the line to $title=undefined(); I get another error. If I set $title="foo"; I get no error, but no foo.
So how do I return a search hit or, even better, a set of search hits?
None of the existing search plug-ins use the modern search Hook api, which is documented in these locations:
https://www.mediawiki.org/wiki/Manual:Hooks/SearchAfterNoDirectMatch
https://gerrit.wikimedia.org/g/mediawiki/core/+/master/includes/search/SearchNearMatcher.php
https://doc.wikimedia.org/mediawiki-core/master/php/classSearchNearMatcher.html
That hook can't return text, you just can change the title in order to generate a match from the hook. $title has to be a Title object, if the code you posted above is the exact code you are using your exception is due to the second parameter not being one of the namespace constants like NS_MAIN
SearchAfterNoDirectMatch is used to return the title of a near-match, rather than to supplement the search results. For supplementing search results, use the onSpecialSearchResultsAppend. Here is code adds three lines to the search results:
class Hooks {
/**
* #see https://www.mediawiki.org/wiki/Manual:Hooks/SearchAfterNoDirectMatch
* #called from https://gerrit.wikimedia.org/g/mediawiki/core/+/master/includes/search/SearchNearMatcher.php
* #param $searchterm
* #param $title - array of titles
*/
public static function onSpecialSearchResultsAppend( $that, $out, $term ) {
$out->addHTML("<h3>Extra Search Results:</h3>");
$out->addHTML("<ul>");
$out->addHTML("<li>Extra Result #1</li>");
$out->addHTML("<li>Extra Result #2</li>");
$out->addHTML("<li>Extra Result #3</li>");
$out->addHTML("</ul>");
}
}
}
That should be enough to get most people going.

Can an object tell Twig not to escape output when it is {{ printed }}

I've got a situation where some variables sent to my Twig templates are plain old variables, so I want them to be html-escaped (as is the default behaviour). But other variables sent to my templates are really objects with __toString() renderers... and some of these objects send out raw HTML (e.g. from a WYSIWYG editor like TinyMCE or CKEditor).
Ideally I'd like for my template designers to not have to use the |raw filter on the objects, but instead somehow have the objects tell Twig that they're already escaped.
In other words, I'm trying to mimc the behavior of a Twig function that sets is_safe, but without requiring template designers to use a function.
E.g. I could write a Twig function using the is_safe parameter in its definition and be able to have this in my templates:
{{ figure_out_what_to_do(something) }}
(where the figure_out_what_to_do knows to inspect the "something" object to ascertain whether or not it needs to be escaped). BUT to me this is no better than having to remember to put |raw after every output of "something". So instead I'd like to be able to do this:
{{ something }}
...and have Twig recognize that something is an object and hence ask it whether or not it needs to be escaped.
I'm guessing the answer is "no", but figured I'd ask in case someone who knows more about Twig internals has any pointers for me.
Thanks.
In the __toString() method you could instead of return the html output, do this : return new Twig_Markup($html, 'UTF-8'); thus marking it as a safe and not to be escaped
Instead of returning a new \Twig_Markup object in __toString() (which causes a fatal error since it must return a string) you could extend \Twig_Markup:
class Something extends \Twig_Markup {
public function __toString() {
return $this->safeValue;
}
public function count() {
return mb_strlen($this->safeValue);
}
}
Twig looks at the object to see if it is an instance of \Twig_Markup when deciding to escape the string or not. Here’s the source for \Twig_Markup:
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig;
/**
* Marks a content as safe.
*
* #author Fabien Potencier <fabien#symfony.com>
*/
class Markup implements \Countable
{
protected $content;
protected $charset;
public function __construct($content, $charset)
{
$this->content = (string) $content;
$this->charset = $charset;
}
public function __toString()
{
return $this->content;
}
public function count()
{
return \function_exists('mb_get_info') ? mb_strlen($this->content, $this->charset) : \strlen($this->content);
}
}
class_alias('Twig\Markup', 'Twig_Markup');
As you can see in the source code, \Twig_Markup implements \Countable. That’s why I’ve overridden the implementation of public function count() in my example.

Resources